Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I read a hex byte value from a file?

Status
Not open for further replies.

joncoles

Technical User
Feb 2, 2004
2
CA
getc is supposed to return a char. I find that it doesn't return the correct value for non-printable characters. For example:

open(FILE, $ARGV[$1]) || die("can't open: $!");
$namlen=getc(FILE);
$len=read(FILE,$fname,$namlen);

The first byte contains the length of the string that follows. In my test file its value is hex 0B. But $namlen is 0, not 11.

read(FILE,$namlen,1); doesn't work any better

In the final line of my example, I can change $namlen to a constant value of 11 and the string is read just fine.

In C, reading a byte value would be trivial. What is the trick to make it work in Perl?
 
You want the ASCII value of the character returned by getc.

[tt]$namlen = ord(getc(FILE));[/tt]
 
I came here to report that I'd found the solution.
Thanks anyway, rosenk. That was a very quick response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top