Thread Subject: fwrite and fread with uint64

Subject: fwrite and fread with uint64

From: Philip Arcuni

Date: 23 Aug, 2009 22:56:01

Message: 1 of 3

I am creating binary files for a camera where 3 by 21-bit data are stored in 64 bit words.

My code creates uint64 words which parse correctly.

However, when I write these uint64 words, and then read them, they are not correct.

For example, if I create a uint64 word (using bitshift and bitor) which reports as
q =

  9147373404693901452

(note that this *can't* naively be entered in Matlab as q = uint64(9147373404693901452) because of the precision limitations of double precision numbers)
then issue the following fwrite and fread:

fid = fopen('test.hex','w');fwrite(fid,q,'uint64'),fclose(fid);
fid = fopen('test.hex','r');t = fread(fid,'uint64=>uint64'),fclose(fid)

I get
t =

  9147373404693901312

Not sure if the error is in the write or the read (I fear the write), but it does appear related to an invisible intermediate double precision step.

Is there any advice as to a fix or an elegant solution/work around?

Subject: fwrite and fread with uint64

From: Sarah

Date: 17 Jan, 2012 23:36:07

Message: 2 of 3

I am running into this same problem now. Did anyone ever figure out a solution?

Thanks,
Sarah

"Philip Arcuni" wrote in message <h6she1$5h7$1@fred.mathworks.com>...
> I am creating binary files for a camera where 3 by 21-bit data are stored in 64 bit words.
>
> My code creates uint64 words which parse correctly.
>
> However, when I write these uint64 words, and then read them, they are not correct.
>
> For example, if I create a uint64 word (using bitshift and bitor) which reports as
> q =
>
> 9147373404693901452
>
> (note that this *can't* naively be entered in Matlab as q = uint64(9147373404693901452) because of the precision limitations of double precision numbers)
> then issue the following fwrite and fread:
>
> fid = fopen('test.hex','w');fwrite(fid,q,'uint64'),fclose(fid);
> fid = fopen('test.hex','r');t = fread(fid,'uint64=>uint64'),fclose(fid)
>
> I get
> t =
>
> 9147373404693901312
>
> Not sure if the error is in the write or the read (I fear the write), but it does appear related to an invisible intermediate double precision step.
>
> Is there any advice as to a fix or an elegant solution/work around?

Subject: fwrite and fread with uint64

From: James Tursa

Date: 18 Jan, 2012 01:22:09

Message: 3 of 3

"Sarah" wrote in message <jf50l7$mvf$1@newscl01ah.mathworks.com>...
> I am running into this same problem now. Did anyone ever figure out a solution?
>
> Thanks,
> Sarah

I think MATLAB fixed this with later versions. However, in the meantime you can try typecasting. E.g.

- typecast your variable to UINT32
- write that out as a UINT32
- read it in as a UINT32
- typecast it back to UINT64

James Tursa

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
fopen Sprinceana 24 Aug, 2009 03:11:28
reference Sprinceana 24 Aug, 2009 03:11:18
fread Sprinceana 24 Aug, 2009 03:11:18
uint64 Sprinceana 24 Aug, 2009 03:11:18
fwrite Sprinceana 24 Aug, 2009 03:11:18
rssFeed for this Thread

Contact us at files@mathworks.com