十六进制和无符号有符号小数 [英] hex and unsigned and signed decimal
问题描述
我正在编写一个名为base.c的基本转换实用程序。
这就是基数所做的。
$ base -127
签名小数:-127
无符号十进制:4294967169
十六进制:0xffffff81
八进制:O37777777601 >
二进制:1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
$ base 127
带符号十进制:127
无符号十进制:127
十六进制:0x7f
八进制:O177
二进制:1098 7654 3210 9876 5432 1098 7654 3210
$ b $ 00 0000 0000 0000 0000 0000 0000 0111 1111
但是,当一个输入符号扩展为十六进制或八进制时,base有一个错误
数字,像这样。
$ base 0xffffff81
有符号小数:2147483647
无符号十进制:2147483647
十六进制:0x7fffffff
八进制:O17777777777
二进制:1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base就像这样。我使用sscanf%i将argv [1]转换为int。然后
我将int转换为unsigned int并执行其余的操作。
但是,如上例所示,当我运行时它无法正常工作输入
a符号扩展的八进制或十六进制数。
0xffffff81的小数应该是4294967169,但它不是。为什么?
谢谢
I am writing a little base conversion utility called base.c.
This is what base does.
$ base -127
Signed decimal: -127
Unsigned decimal: 4294967169
Hexidecimal: 0xffffff81
Octal: O37777777601
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
$ base 127
Signed decimal: 127
Unsigned decimal: 127
Hexidecimal: 0x7f
Octal: O177
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0000 0000 0000 0000 0000 0000 0111 1111
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
$ base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base works like this. I use sscanf %i to convert argv[1] to an int. Then
I cast the int to an unsigned int and do the rest of the manipulation.
However, as the above example shows, it doesn''t work properly when I input
a sign extended octal or hex number.
The decimal of 0xffffff81 should be 4294967169, but it isn''t. Why ?
Thanks
推荐答案
base -127
签名小数: -127
无符号十进制:4294967169
十六进制:0xffffff81
八进制:O37777777601
二进制:1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
base -127
Signed decimal: -127
Unsigned decimal: 4294967169
Hexidecimal: 0xffffff81
Octal: O37777777601
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
1111 1111 1111 1111 1111 1111 1000 0001
base 127
有符号十进制:127
无符号十进制:127
十六进制:0x7f
八进制:O177
二进制:1098 7654 3210 9876 5432 1098 7654 3210
$ b $ 00 0000 0000 0000 0000 0000 0000 0111 1111
但是,当一个输入符号扩展为十六进制或八进制时,base有一个错误
数字,像这样。
base 127
Signed decimal: 127
Unsigned decimal: 127
Hexidecimal: 0x7f
Octal: O177
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0000 0000 0000 0000 0000 0000 0111 1111
However, base has a bug when one inputs sign extended hexadecimal or octal
numbers, like this.
base 0xffffff81
有符号小数:2147483647
无符号十进制: 21474 83647
十六进制:0x7fffffff
八进制:O17777777777
二进制:1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base就像这样。我使用sscanf%i将argv [1]转换为int。然后
我将int转换为unsigned int并执行其余的操作。
但是,如上例所示,当我运行时它无法正常工作输入
a符号扩展的八进制或十六进制数。
0xffffff81的小数应该是4294967169,但它不是。为什么?
谢谢
base 0xffffff81
Signed decimal: 2147483647
Unsigned decimal: 2147483647
Hexidecimal: 0x7fffffff
Octal: O17777777777
Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0111 1111 1111 1111 1111 1111 1111 1111
Base works like this. I use sscanf %i to convert argv[1] to an int. Then
I cast the int to an unsigned int and do the rest of the manipulation.
However, as the above example shows, it doesn''t work properly when I input
a sign extended octal or hex number.
The decimal of 0xffffff81 should be 4294967169, but it isn''t. Why ?
Thanks
这篇关于十六进制和无符号有符号小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!