Converting hex to decimal when printed?

F

faren451

Hi, ok, trying this again, since my first post seems to be non-
existent...

I am reading in 2-digit hex values from a file and would like to
output them as decimals. Through online surfing, it seems like the
easiest way to do this is to use printf. However, when I played
around with printf, it seems like all hex values need to have 0x in
front to make it work. So I tried using variables to essentially
concatenate 0x to my 2-digit hex values--but they all print as 0's!
My script is below:

print "Enter filename to parse: ";
chomp($filename = <>);
$FilePath = "$filename";
sysopen(HANDLE, $FilePath, O_RDONLY) or die "Cannot open file";
@events = <HANDLE>;
close(HANDLE);
$Outfile = "out_$filename";
sysopen(OHANDLE, $Outfile, O_RDWR|O_CREAT, 0755) or die "Cannot create
file";
foreach $events (@events)
{
while($events =~ /(\s.*?\s)/g)
{
# hex data fields
$tmp = $1;
$tmp =~ s/^\s+//;
$tmp =~ s/\s+$//;
$hexval = "0x$tmp";
printf OHANDLE "$hexval=>%d,", $hexval;
printf "$hexval=>%d,", $hexval;
}
close(OHANDLE);


Sample input file is:

EB 70 07 8A 51 C5 98 1B 01 00 00 00 00 00 00 02 00 00
00 00 00 00 00 00 00 01 01 00 01 00 00 00 00 00 00
00 02 00 00 00 01 00 01 01 00 01 02 00 00 00 00 00
01 00 01 00 00 93 10 60 00


I appreciate any and all help! I am a complete Perl newbie and have
no idea why this does not work as advertised. I also tried the built-
in hex() function, but it has the same problem.

Thanks!
 
B

Brian McCauley

I am reading in 2-digit hex values from a file and would like to
output them as decimals. Through online surfing, it seems like the
easiest way to do this is to use printf.

No, there's no need to use printf to convert a number into a string
containing the decimal representation of the number. In Perl simply
using a number in a string context will do this implicitly. So if I
try to print, or perform string concatenation on the number thirteen
it is implicitly converted to the string '13' first.
However, when I played
around with printf, it seems like all hex values need to have 0x in
front to make it work.

I suspect you are confusing code and data. In Perl program source code
I can represent the number thirteen as 13 or 1_3 or 0xd or 015 or
0b1101.
So I tried using variables to essentially
concatenate 0x to my 2-digit hex values--but they all print as 0's!

When you use a string value in a numeric context (like in arithmetic
expression) the rules are different. Anything at the start of the
string that looks like it may be a decimal (and only decimal) number
will be interpreted as such. So '13'+0 will be thirteen, '1_3'+0 will
be one, '0xd'+0 will be zero and '015'+0 will be fifteen.
I also tried the built- in hex() function,

Yes, hex('d') or hex('0xd') would both return thirteen. And if you
print the value of an expression that returned thirteen it will print
as the string '13'.
but it has the same problem.

Without seeing your code I can't guess why
 
P

Paul Lalli

Hi, ok, trying this again, since my first post seems to be non-
existent...

I am reading in 2-digit hex values from a file and would like to
output them as decimals. Through online surfing, it seems like the
easiest way to do this is to use printf. However, when I played
around with printf, it seems like all hex values need to have 0x in
front to make it work. So I tried using variables to essentially
concatenate 0x to my 2-digit hex values--but they all print as 0's!
My script is below:

print "Enter filename to parse: ";
chomp($filename = <>);
$FilePath = "$filename";
sysopen(HANDLE, $FilePath, O_RDONLY) or die "Cannot open file";
@events = <HANDLE>;
close(HANDLE);
$Outfile = "out_$filename";
sysopen(OHANDLE, $Outfile, O_RDWR|O_CREAT, 0755) or die "Cannot create
file";
foreach $events (@events)
{
while($events =~ /(\s.*?\s)/g)
{
# hex data fields
$tmp = $1;
$tmp =~ s/^\s+//;
$tmp =~ s/\s+$//;
$hexval = "0x$tmp";
printf OHANDLE "$hexval=>%d,", $hexval;
printf "$hexval=>%d,", $hexval;

$hexval is a string. Perl has no way of knowing that that string
happens to contain a human being's representation of a hexadecimal
number. When you say to use the %d format specifier, Perl knows you
want to convert that string to a number. And it does this by reading
the string from left to right, stopping at the first character that
makes this string not a valid decimal number. In this case, that's
the "x" you appended. So this string converts to the number 0.
I also tried the built-in hex() function, but it has the same
problem.

No it doesn't. hex() is the correct way to solve this problem. If
you aren't getting correct results using it, you're doing something
wrong. Unfortunately, you've only shown your code for the incorrect
way to solve the problem, and not for the correct way. So there's no
way to show you what you've done wrong. But here's your proof that
hex() works correctly:

$ perl -le'
for my $hexval (qw/EB 70 07 8A 51 C5/) {
my $decval = hex($hexval);
print "$hexval => $decval";
}
'
EB => 235
70 => 112
07 => 7
8A => 138
51 => 81
C5 => 197


Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top