Discussion:
printf and conversion of negative numbers (Hex2Dec)
(too old to reply)
Markus
2010-07-01 14:13:20 UTC
Permalink
Hi,

I am facing a problem when interpreting negative decimal number in
hexadecimal format using 'printf' in bash script.

This example command:
printf "%X\n" -2147476746
Does produce this result: FFFFFFFF80001AF6.

I learned that the 'FFFFFFFF' is an interpretation of the minus sign.
Now I would like to do the opposite conversion, Hex2Dec
printf "%d\n" 0xFFFFFFFF80001AF6
Result is "-bash: printf: warning: 0XFFFFFFFF80001AF6: Numerical
result out of range" plus some long number. But I am awaiting the
original number -2147476746.
So the question is - how do I convert the negative hexadecimal value
back to the original value?
DennisW
2010-07-01 15:53:29 UTC
Permalink
Post by Markus
Hi,
I am facing a problem when interpreting negative decimal number in
hexadecimal format using 'printf' in bash script.
printf "%X\n" -2147476746
Does produce this result: FFFFFFFF80001AF6.
I learned that the 'FFFFFFFF' is an interpretation of the minus sign.
Now I would like to do the opposite conversion, Hex2Dec
printf "%d\n" 0xFFFFFFFF80001AF6
Result is "-bash: printf: warning: 0XFFFFFFFF80001AF6: Numerical
result out of range" plus some long number. But I am awaiting the
original number -2147476746.
So the question is - how do I convert the negative hexadecimal value
back to the original value?
printf "%d\n" "$((16#FFFFFFFF80001AF6))"

or

base=16
num=FFFFFFFF80001AF6
printf "%d\n" "$(($base#$num))"

Loading...