Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert signed integer in hex/bin (2's complement) into integer in dec

Status
Not open for further replies.

ghung

Technical User
Oct 24, 2003
7
FI
Hello Folks,

How can I convert a signed integer in hex or bin into integer in dec? hex() seems to treat number as non-signed only.

Many thanks!

Gary





Gary Hung
ASIC Design Engineer
 
I''m afraid I do not know what a signed integer is - but this might help

print hex 0x1AB

this returns [red]1063[/b]


Kind Regards
Duncan
 
Thanks Duncan and Andrew. For example,

dec hex bin
24 18 0001_1000
-24 E8 1110_1000 (2's complement of 0001_1000)

printf( "num in dec : %d \n", hex(18) );
num in dec : 24

printf( "num in dec : %d \n", hex(e8) );
num in dec : 232 (which I actually want to get -24)

I am checking the pack/unpack function now... wish me luck..

Gary


Gary Hung
ASIC Design Engineer
 
hi ghung

can you give the same example but with decimal 23 and decimal 22

many thanks


Kind Regards
Duncan
 
Code:
print unpack('c',pack('H2','e8'));

Pack the hex as hex first (big H means high order nibble first), then unpack it as a signed char, which is a byte.

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top