Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Converting byte to integer string

Reply
Thread Tools

Converting byte to integer string

 
 
kerry.baker@orcon.net.nz
Guest
Posts: n/a
 
      04-03-2006
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!

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-03-2006
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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

--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
Chris Richmond - MD6-FDC ~
Guest
Posts: n/a
 
      04-03-2006
(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
--
Chris Richmond | I don't speak for Intel & vise versa

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
How to convert a 4 byte character string to its equivalent 4 byte integer value? Polaris431 C Programming 8 12-04-2006 07:00 AM
Converting a 4 byte field to integer. Shashank Khanvilkar Perl Misc 2 07-29-2005 07:41 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
byte[] > string > byte[] problem Peter Java 3 08-05-2004 10:55 AM



Advertisments