2012-02-15 3 views
4

나는 CIL을 가르치고 있고, 지금까지 (어제 정말로 시작했다) ok를하고있다. 그러나 나는 내가 정말로 이해할 수없는 문제에 부딪쳤다. 사용자에게 int (int32)를 묻는 메시지를 저장하고이를 float로 변환하여 표시합니다. 그러나 내가 입력 한 것은 무엇이든 부유물처럼 나온다. 여기 내 코드입니다 :Int32를 Float64로 캐스팅하여 데이터가 변경되는 이유는 무엇입니까?

.assembly variables {} 
.method public static void main() cil managed 
{ 
    .entrypoint 
    .maxstack 8 
    .locals init (float64) 

    ldstr "Enter a digit: " 
    call void [mscorlib]System.Console::WriteLine(string) 
    call int32 [mscorlib]System.Console::Read() 
    conv.r8 
    stloc.0 
    ldstr "as a float: " 
    call void [mscorlib]System.Console::WriteLine(string) 
    ldloc.0 
    dup 
    call void [mscorlib]System.Console::Write(float64) 
    stloc.0 
    ldstr "Stored in location 0" 
    call void [mscorlib]System.Console::WriteLine(string) 
    ldloc.0 
    conv.i4 
    call void [mscorlib]System.Console::WriteLine(int32) 
    call int32 [mscorlib]System.Console::Read() // to pause before closing window 
    pop 
    ret 
} 

난 그냥 CIL와 장난했지만, 난이 명확하게 내 전체 예를 던질 거라고 생각. 그것은 잘 컴파일하지만 5 입력하면 53 float 및 변환 된 int32 반환합니다.

누군가 내가 뭘 잘못하고 있는지 밝힐 수 있습니까?


편집 : Marc Gravell 덕분에 내가 그것을 알아낼 수 있었다.

.assembly variables {} 
.method public static void main() cil managed 
{ 
    .entrypoint 
    .maxstack 8 
    .locals init (float64) 

    ldstr "Enter a digit: " 
    call void [mscorlib]System.Console::WriteLine(string) 
    call string [mscorlib]System.Console::ReadLine() 
    call int32 [mscorlib]System.Int32::Parse(string) 
    conv.r8 
    stloc.0 
    ldstr "as a float: " 
    call void [mscorlib]System.Console::WriteLine(string) 
    ldloc.0 
    dup 
    call void [mscorlib]System.Console::Write(float64) 
    stloc.0 
    ldstr "Stored in location 0" 
    call void [mscorlib]System.Console::WriteLine(string) 
    ldloc.0 
    conv.i4 
    call void [mscorlib]System.Console::WriteLine(int32) 
    call int32 [mscorlib]System.Console::Read() // to pause before closing window 
    pop 
    ret 
} 

답변

7

Console.Read는 유니 코드 코드 포인트를 돌려줍니다 EOF를 위해 : 관심있는 사람들을 위해, 여기에 올바른 코드입니다. 53은 문자 (정수가 아님) '5'의 코드 포인트입니다.

아마도 Console.ReadLineint.Parse을 사용할 수 있습니다.

+0

Brilliant! 나는 그 대답에 감사 드린다. 8 분 안에 받아 들일 것입니다. 고맙습니다. – Jetti

+0

@Jetti 신선한 눈 : p –

+0

더 많은 .NET 학습이있는 것처럼 보입니다. 사실 그것이 유니 코드에 있었기 때문에 내 광산을 건널 수 없었습니다. ( – Jetti

관련 문제