Jan 22, 2004 #1 ghung Technical User Joined Oct 24, 2003 Messages 7 Location 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
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
Jan 22, 2004 #2 duncdude Programmer Joined Jul 28, 2003 Messages 1,979 Location GB 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 Upvote 0 Downvote
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
Jan 22, 2004 #3 icrf Programmer Joined Dec 4, 2001 Messages 1,300 Location US I suggest pack/unpack (the latter for yours needs, but the documentation is better in pack's pod): http://www.perldoc.com/perl5.8.0/pod/perlpacktut.htmlhttp://www.perldoc.com/perl5.8.0/pod/func/pack.htmlhttp://www.perldoc.com/perl5.8.0/pod/func/unpack.html ________________________________________ Andrew - Perl Monkey Upvote 0 Downvote
I suggest pack/unpack (the latter for yours needs, but the documentation is better in pack's pod): http://www.perldoc.com/perl5.8.0/pod/perlpacktut.htmlhttp://www.perldoc.com/perl5.8.0/pod/func/pack.htmlhttp://www.perldoc.com/perl5.8.0/pod/func/unpack.html ________________________________________ Andrew - Perl Monkey
Jan 22, 2004 Thread starter #4 ghung Technical User Joined Oct 24, 2003 Messages 7 Location FI 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 Upvote 0 Downvote
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
Jan 23, 2004 #5 duncdude Programmer Joined Jul 28, 2003 Messages 1,979 Location GB hi ghung can you give the same example but with decimal 23 and decimal 22 many thanks Kind Regards Duncan Upvote 0 Downvote
hi ghung can you give the same example but with decimal 23 and decimal 22 many thanks Kind Regards Duncan
Jan 23, 2004 #6 icrf Programmer Joined Dec 4, 2001 Messages 1,300 Location US 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 Upvote 0 Downvote
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
Jan 23, 2004 Thread starter #7 ghung Technical User Joined Oct 24, 2003 Messages 7 Location FI Thanks Andrew ! it's cool ! Duncan, below is a link about signed integer. http://www.evergreen.edu/biophysics/technotes/program/2s_comp.htm Gary Hung ASIC Design Engineer Upvote 0 Downvote
Thanks Andrew ! it's cool ! Duncan, below is a link about signed integer. http://www.evergreen.edu/biophysics/technotes/program/2s_comp.htm Gary Hung ASIC Design Engineer