2012-02-15 6 views
4

私はCILを教えていて、これまでずっと昨日始まったばかりですが、私は本当に理解できない問題にぶつかりました。私はint(int32)のためにユーザーにプロンプ​​トを出し、それを格納してフロートに変換して表示します。しかし、私が何を入力してもフロートとしては違う。ここに私のコードです: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が返されます。

誰かが私が間違っていることにいくつかの光を当ててください!


EDIT: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 Unicodeコードポイントを返し、または-1 EOFのために:興味がある人のために、ここで正しいコードです。 53は、文字(整数ではない)'5'のコードポイントです。

おそらくConsole.ReadLineint.Parseを使用できます。

+0

ブリリアント!私は答えを感謝します。私はそれを8分で受け入れるとマークします。 – Jetti

+0

@ジェッティ新鮮な目:p –

+0

私はより多くの.NET学習があるように見えます。それが私の鉱山を越えたことはないという事実は: – Jetti

関連する問題