Converting byte to integer string

K

kerry.baker

I have a strange problem. I have a need to convert the value of a byte
read from a file to its integer string counterpart. (e.g. the byte
contains 0x1d and it prints out 29.)
I use this to do it:
$value = unpack ("c",substr($_, 259, 1));

This seems to work just fine for all values except 0x0a which returns
32 instead of 10.
Why does this happen?
I wrote a similar script using awk which also returned 32 instead of
10.
It seems so odd to me. 0x0b returns 11 which is correct as does all
other values.
Any ideas greatfully accepted!
 
A

Anno Siegel

I have a strange problem. I have a need to convert the value of a byte
read from a file to its integer string counterpart. (e.g. the byte
contains 0x1d and it prints out 29.)
I use this to do it:
$value = unpack ("c",substr($_, 259, 1));

This seems to work just fine for all values except 0x0a which returns
32 instead of 10.
Why does this happen?
I wrote a similar script using awk which also returned 32 instead of
10.
It seems so odd to me. 0x0b returns 11 which is correct as does all
other values.

perl -le 'print unpack "c", chr 0x0a'
10

Have you checked the character you're accessing?

Anno
 
C

Chris Richmond - MD6-FDC ~

(s)printf sort of works:

perl -e '{printf(%c,'0x0a');print"\n";}'
10
perl -e '{printf(%c,'0x0b');print"\n";}' 11
perl -e '{printf(%c,'0x0c');print"\n";}' 12
perl -e '{printf("%c",'0x0c');print"\n";}'


perl -e '{$val = '0x0b';printf(%c, $val);print"\n";}'
11


quoting the format screws it up.

Chris
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top