[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Getting floating point numbers from binary files



At 22.46 -0400 1998.08.31, Christopher F. Blanford wrote:
>For example, if the byte sequence is 43 4E 56 41 it'll give me "CNVB"
>rather than the numerical values. Can someone tell me how I can force it
>to give me the integer values, or better yet, read in a four-byte sequence
>and convert it directly into floating-point.

Well, you read it in ASCII, so you would have to manually convert it.
Since these are hex values, try:

  @vals = map {sprintf "%lX", $_} map {ord} split //, 'CNVB';
  print join "\n", @vals;

Or, for your code specifically:

  open(FILE, $filename);
  read(FILE, $byte[0], 1);
  read(FILE, $byte[1], 1);
  read(FILE, $byte[2], 1);
  read(FILE, $byte[3], 1);

  @byte = map {sprintf "%lX", $_} map {ord} split //, @byte;

  # if you have onle one value:
  # $byte = sprintf("%lX, ord($byte));

Basically, this takes the decimal value of the letter with (ord) and passes
it to sprintf to get the hex value.

Good luck,

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch