Prev: find command
Next: Parse a line?
From: Markus on 1 Jul 2010 10:37 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?
From: Wayne on 1 Jul 2010 11:21 On 7/1/2010 10:37 AM, Markus wrote: > 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? $ dec2hex() { if test "$1" -lt 0 then printf -- '-0x%X\n' $((-$1)) else printf '0x%X\n' "$1" fi } $ hex2dec() { printf '%d\n' "$1"; } $ dec2hex -2147476746 -0x7FFFE50A $ hex2dec -0x7FFFE50A -2147476746 -- Wayne
From: Markus on 1 Jul 2010 11:48 On Jul 1, 5:21 pm, Wayne <nos...(a)all.invalid> wrote: > On 7/1/2010 10:37 AM, Markus wrote: > > > > > 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? > > $ dec2hex() { > if test "$1" -lt 0 > then printf -- '-0x%X\n' $((-$1)) > else printf '0x%X\n' "$1" > fi > > } > > $ hex2dec() { printf '%d\n' "$1"; } > > $ dec2hex -2147476746 > -0x7FFFE50A > > $ hex2dec -0x7FFFE50A > -2147476746 > > -- > Wayne Well - problem with your script is that "printf "%X\n" -2147476746" is not the same like "printf "%X\n" 2147476746". And I do not need "some" value, I need exact value. Thanx! Markus
From: Ben Bacarisse on 1 Jul 2010 12:39 Markus <markus.mj(a)gmail.com> writes: > 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. Not really no. %X prints and unsigned number and you did not give it one. If the shell's printf follows C's rules for sorting this out it will reduce the result modulo one more than the largest unsigned number that can be represented. This is what it has done, but the result is a positive number: the decimal equivalent of 0xFFFFFFFF80001AF6 is 18446744071562074870 not -2147476746. The other answer you got (so far) is correct in that the hexadecimal of -2147476746 is -0x7FFFE50A (or -7FFFE50A if the base can be assumed) not OxFFFFFFFF80001AF6. However, I will offer another solution. > 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. 0xFFFFFFFF80001AF6 is indeed out of range for your printf (it seems to be using 64-bit arithmetic) but you can get what I think you want: function hex2dec { echo $(((0x$1 + 1) - 1)) } It relies on the overflow behaviour of the internal arithmetic so I would be surprised if it is portable but you never know. Do consider using a signed notation for negative numbers (as suggested already by someone else -- sorry I can't see the name right now). If that is possible you will get fewer surprises. > 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? -- Ben.
|
Pages: 1 Prev: find command Next: Parse a line? |