ok, i guess i'm just confused about all this automatic type stuff in perl.

when you read from a binary file like:

open ( FD, $filename );
binmode ( FD );
read(FD, $buf, 2);

now $buf has 2 bytes of your file.  what kind of data type is this?  why
does:

printf '%x', $buf;

not print out the hex value of the binary data?

i figured out what i needed to about this, but i'm just not fully
understanding why.  i solved my problem by doing:

$un = unpack("H*", $buf);

now i can manipulate the value of $un however i want... is $un now what is
referred to as a 'string literal'?  and what kind of data type is $buf?

thanks,
paul



> -----Original Message-----
> From: LoBue, Mark [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, May 02, 2002 1:18 PM
> To: [EMAIL PROTECTED]
> Subject: RE: what kind of data type?
>
>
> $x = 0xA;
> printf "%x\n", $x;
>
> result: a
>
> -Mark
> -----Original Message-----
> From: bob ackerman [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, May 02, 2002 8:55 AM
> To: [EMAIL PROTECTED]
> Subject: Re: what kind of data type?
>
>
>
> On Thursday, May 2, 2002, at 02:45  AM, Paul Weissman wrote:
>
> >
> > what i'm trying to do is open a binary file and read from it...
> >
> > ---\
> >
> > open ( FD, $filename );
> > binmode ( FD );
> >
> > #read two bytes
> > while (read(FD, $buf, 2)) {
> >  # hopefully print the hex value of two bytes
> >  print hex($buf);
> > }
>
> i think you are confused about what hex() does.
> it takes an ascii string and interprets it as if the characters
> repesent a
> hex number.
> see perldoc -f hex.
> binary data would need to be handled differently.
> if i assume you want to see a dump to the terminal of  your data,
> you would have to do the opposite of what hex() does. turn a hex number
> into a string.
> program 'hexdump' does this.
> if you still want to do this in perl and are stuck, you could repost your
> problem.
>
> > ---/
> >
> > and what i'm going for here is to read some binary data in and
> print out
> > the
> > hexadecimal value of that binary data.
> >
> > what's happening is very different.  for whatever reason, $buf is not
> > being
> > treated like an ordinary number... code that does what i want is:
> >
> > ---\
> >
> > $x = 0xA;
> > print hex($x);
> >
> > ---/
> >
> > which prints the value '10'.
> >
> > what kind of datatype is $buf in the first code snippet?  and can i
> > convert
> > it so that i can use it like $x in the second code snippet?
> >
> > i'm using ActivePerl (newest version) on Win2k.
> >
> > thanks tons!
> >
> > paul
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to