0

The following code:

$tmp1 = $_;
print "tmp1 is $tmp1";
$tmp1_hex = hex($tmp1);
print "tmp1_hex is $tmp1_hex\n";
$Lat_tmp1 = ($tmp1_hex >> 8) &0x00ff;

prints:

tmp1 is 0018
tmp1_hex is 24

The text file I'm reading the data from contains the string 0018, but when I convert it to the hex value I shouldn't be receiving 24.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Igor
  • 5,620
  • 11
  • 51
  • 103
  • That code doesn't output that. – brian d foy Mar 09 '20 at 21:29
  • Also watch out that `0018` is interpreted as octal number: `print hex(0018)` will output: `Illegal octal digit '8'`. ALso `hex` does not convert *to* hex, but convert *from* hex. – U. Windl Mar 09 '20 at 21:53
  • Before typing a code you need possess an understanding what you typing if in doubt read documentation [hex](https://perldoc.perl.org/functions/hex.html). – Polar Bear Mar 09 '20 at 23:40
  • Is `0018` representation of the digit in decimal or hex form? Number `0x0018` is `24` in decimal form, decimal number `0018` is `0x12` in hex form, base of digit's representation must be know to get a correct answer. – Polar Bear Mar 09 '20 at 23:52
  • This was at the very top of a search result on 2022-05-19. There isn't a need to keep answering the same basic questions over and over. – Peter Mortensen May 19 '22 at 22:08
  • For output in hexadecimal, see e.g. *[How do I convert decimal to hexadecimal in Perl?](https://stackoverflow.com/questions/10481001/)* (though there is probably a more canonical one as this one is from 2012) – Peter Mortensen May 19 '22 at 22:13
  • For the other way around, hexadecimal to decimal, see *[How can I convert hex strings into numbers in Perl?](http://stackoverflow.com/questions/1531993)* – Peter Mortensen May 19 '22 at 22:14

3 Answers3

2

If you want to convert to hex rather than from hex, use sprintf:

my $tmp1_hex = sprintf '%x', $tmp1;
choroba
  • 231,213
  • 25
  • 204
  • 289
1

The hex function merely interprets the string as a number in hexadecimal form. Beyond that, it's just a number and its original representation doesn't matter.

When you print a number, Perl uses its internal format (%g) to show it. That's a normal, decimal float.

If you want to output the number as something other than Perl's internal format, use printf and the appropriate specifier:

 printf '%x', $number;

You probably want something like:

my $tmp1 = '0018';
print "tmp1 is $tmp1\n";
my $tmp1_hex = hex( $tmp1 );
printf "tmp1_hex is %x\n", $tmp1_hex;

Note that the bitwise operators don't need you to convert the number to any particular base. The number is the same number no matter how you display it.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • kind of a follow-up question - do I have to convert it to hex (well, represent it as hex()) in order to do the bitshifting? Or I can use the original "0018" string that's being read from the file? – Igor Mar 09 '20 at 21:41
  • If you use "0018", Perl will convert it to a number using its default conversion, which is base-10. – brian d foy Mar 09 '20 at 21:45
  • @Igor, Re "*do I have to convert it to hex (well, represent it as hex()) in order to do the bitshifting?*", Quite the opposite. The shift operators work on numbers, but hex is a text representation of a number. They won't work on hex. – ikegami Mar 10 '20 at 07:17
-1

The function hex() converts from hex to decimal. Try something like this instead

$tmp=$_; $tmp1_hex=sprintf("%X",$tmp);

  • That's not what you want. He needs to convert a string to a number. Once it's a number, it doesn't have a base. Only representations of numbers have a base. – brian d foy Mar 09 '20 at 21:46