Discussion:
Can fscanf read a double in "hex" format?
(too old to reply)
Brian Borchers
2004-05-02 04:52:08 UTC
Permalink
I have a file containing the line:

C05380394201332C

This is the hexadecimal representation of a particular IEEE double
precision number that was output from another program. I would like to
get this number into MATLAB. The program that produced this number
could also have output it in decimal, but when the first program outputs
a number in decimal using the C format "%.20e", and I then read it in to
MATLAB, I don't get precisely the same value.

I presume that this is because the C runtime library isn't doing the
conversion (on output) to decimal properly, although I suppose that
the problem could also be in MATLAB's conversion on the input.
In any case, the most robust solution would seem to be using the
hexadecimal representation.

I've got the first program producing output in hex without any trouble.
However, I can't seem to figure out a way to get MATLAB to read it in.
I've thought that perhaps '%x' might do the right thing, but it doesn't
seem to work.

As you can probably guess, my actual problem involves rather large
output files with many (millions in some cases) of numbers. Thus
I'd also prefer the quickest possible solution to this.

I've considered using MATLAB's binary format, but since these output
files are used primarily by programs other than MATLAB, that really
isn't appropriate.
--
Brian Borchers ***@nmt.edu
Department of Mathematics http://www.nmt.edu/~borchers/
New Mexico Tech Phone: 505-835-5813
Socorro, NM 87801 FAX: 505-835-5366


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
per isakson
2004-05-02 11:16:43 UTC
Permalink
Post by Brian Borchers
C05380394201332C
This is the hexadecimal representation of a particular IEEE double
precision number that was output from another program. I would
like to
get this number into MATLAB. The program that produced this number
could also have output it in decimal, but when the first program outputs
a number in decimal using the C format "%.20e", and I then read it in to
MATLAB, I don't get precisely the same value.
I presume that this is because the C runtime library isn't doing the
conversion (on output) to decimal properly, although I suppose that
the problem could also be in MATLAB's conversion on the input.
In any case, the most robust solution would seem to be using the
hexadecimal representation.
I've got the first program producing output in hex without any
trouble.
However, I can't seem to figure out a way to get MATLAB to read it in.
I've thought that perhaps '%x' might do the right thing, but it doesn't
seem to work.
As you can probably guess, my actual problem involves rather large
output files with many (millions in some cases) of numbers. Thus
I'd also prefer the quickest possible solution to this.
I've considered using MATLAB's binary format, but since these
output
files are used primarily by programs other than MATLAB, that really
isn't appropriate.
--
Department of Mathematics <http://www.nmt.edu/~borchers/>
New Mexico Tech Phone: 505-835-5813
Socorro, NM 87801 FAX: 505-835-5366
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
<http://www.newsfeeds.com> - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
hex2num('C05380394201332C')
ans =
-78.00349474064996
Rune Allnor
2004-05-02 12:54:26 UTC
Permalink
Post by Brian Borchers
C05380394201332C
This is the hexadecimal representation of a particular IEEE double
precision number that was output from another program. I would like to
get this number into MATLAB.
According to the fscanf online help, matlab implements the C transforms
on ASCII input. My copy of Kerningham and Ritchie's standard book on C,
table 7-2, lists "x" as a format indicator to read hexadecimal integers.
I can find no mention of hexadecimal floating point.

So the way to go about this, then, would be something like

- Read the hex number into an integer variable
- Do a type conversion to double float

The difficult part is how to do the latter. If I worked in C, I'd try
to do a type cast of the contents of the memory location where the
integer representation is stored. A naive attempt would be something
along the lines of

/*********************/
/*C code, not matlab!*/

double A;
fscanf(input,"%x",(long int*) &A);

/*********************/

and then hope that the binary representation stored in A works out OK
as a binary representation of the double float number.

I don't know if this would work in C. If it does, it should
be possible to include this transform in your matlab program
as a mex file.

Rune
per isakson
2004-05-02 13:42:20 UTC
Permalink
Post by Brian Borchers
C05380394201332C
This is the hexadecimal representation of a particular IEEE double
precision number that was output from another program. I would
...
Post by Brian Borchers
As you can probably guess, my actual problem involves rather large
output files with many (millions in some cases) of numbers. Thus
I'd also prefer the quickest possible solution to this.
cssm.txt is a file with 0.88 million lines

400921fb54442d18
C05380394201332C
400921fb54442d18
400921fb54442d18
C05380394201332C
etc.
Post by Brian Borchers
tic
z=textread( 'cssm.txt', '%s', 'delimiter', '\n' );
z0 = hex2num( z( 1 : 100000 ) );
z1 = hex2num( z( 100001 : 200000 ) );
z2 = hex2num( z( 200001 : 300000 ) );
z3 = hex2num( z( 300001 : 400000 ) );
z4 = hex2num( z( 400001 : 500000 ) );
z5 = hex2num( z( 500001 : 600000 ) );
z6 = hex2num( z( 600001 : 700000 ) );
z7 = hex2num( z( 700001 : 800000 ) );
toc
elapsed_time =
23.1060
Post by Brian Borchers
z0(1:5)
ans =
3.1416
-78.0035
3.1416
3.1416
-78.0035

/ per
Lars Gregersen
2004-05-05 19:17:09 UTC
Permalink
Post by Brian Borchers
C05380394201332C
This is the hexadecimal representation of a particular IEEE double
[snip]
Post by Brian Borchers
As you can probably guess, my actual problem involves rather large
output files with many (millions in some cases) of numbers. Thus
I'd also prefer the quickest possible solution to this.
I've considered using MATLAB's binary format, but since these output
files are used primarily by programs other than MATLAB, that really
isn't appropriate.
In order to read files like this efficiently then you have to write a
mexfile (in C) that reads the file. You'll have great difficulty doing
it in plain MATLAB.

If your files only consist of numbers why not output the information
as a binary file. That'll give you much smaller files.

You could also consider using the HDF format.

Lars

Lars Gregersen
COMSOL A/S
http://www.comsol.dk
Peter J. Acklam
2004-05-10 19:49:52 UTC
Permalink
Post by Brian Borchers
C05380394201332C
[...]
I'm entering this thread very late, but one way to do this is to
let Perl convert the ASCII data into binary data which you can
read with MATLAB's "fread" with "double" as the precision argument

perl -wne 'print pack "H16", $_' cssm.txt > cssm.bin

Peter
--
If I made a copy of the The Digital Millennium Copyright Act,
would that be a violation of it?
Loading...