From: Big Andy Nicol on
Hi everyone, I'm looking for some help converting a set of binary numbers to their decimal equivalent. The binary is both 16 and 32 bit long, stored in .dat files as 0's and 1's. Everything I have tried has resulted in Matlab reading in these numbers as double precision, which makes it think that what it is seeing is 1*10^14 or similar.
Each .dat file is a few hundred entries long and I'll need to convert all entries in a simple operation, if possible.

Any thoughts would be much appreciated,
Andy
From: Jan Simon on
Dear Andy!

> Hi everyone, I'm looking for some help converting a set of binary numbers to their decimal equivalent. The binary is both 16 and 32 bit long, stored in .dat files as 0's and 1's.

Please define the binary exactly: "Both 16 and 32 bit long" and "0's and 1's" is confusing. 16 *or* 32 bits? 0's and 1's as bits, bytes, int16 or int32?
There is not a unique definition of converting a sequence of bits to a decimal. A 32 bit number can be a SINGLE or a INT32 or a UINT32.

I assume FREAD to do the job directly. But without more details, this is an assumption only.

Kind regards, Jan
From: TravisC on
1. I'm assuming you're using fread. fread(fid,1,'int32') will read 1 int32 word and cast it as a double. By putting an asterisk (*) in front of the type, it will instead be kept as an int32: fread(fid,1,'*int32'). Some Matlab functions don't work on ints, so you may want to let Matlab cast it as a double, depending on how you're going to use it.
2. As far as reading the data in, it depends on how it's saved. You say it's mixed type. I would probably read it in one word at a time in an appropriate loop. Alternately, you could read in everything as an int16 and then do some binary math to put together the int32's, but that'd be a pain. It really depends on how the data is structured.

"Big Andy Nicol" <andy.nicol(a)selexgalileo.com> wrote in message <hl3dj2$ro3$1(a)fred.mathworks.com>...
> Hi everyone, I'm looking for some help converting a set of binary numbers to their decimal equivalent. The binary is both 16 and 32 bit long, stored in .dat files as 0's and 1's. Everything I have tried has resulted in Matlab reading in these numbers as double precision, which makes it think that what it is seeing is 1*10^14 or similar.
> Each .dat file is a few hundred entries long and I'll need to convert all entries in a simple operation, if possible.
>
> Any thoughts would be much appreciated,
> Andy
From: Big Andy Nicol on
My apologies, I appear to have explained this pretty poorly. By 16 and 32 bit long, I mean that a single file will be either 16 or 32 bit long, but I will require to read in both. An example of what I'm trying to read in is:

0000000000000000
0011010111011011
0011101000110010
0000100100001000
1100111110010001
1100001010100001
....

So far, I haven't found a way of using fread to be able to read this in correctly. The best suggestion I've had was to use fread(fid,'*char') and then perform bin2dec on that but I suspect I had a syntax problem because it said that character was too long.

Thanks,
Andy
From: TravisC on

> My apologies, I appear to have explained this pretty poorly. By 16 and 32 bit long, I mean that a single file will be either 16 or 32 bit long, but I will require to read in both.

So the entire file is composed solely of 16 or 32 bit words? That makes it easier and prevents you from needing a loop.

> So far, I haven't found a way of using fread to be able to read this in correctly. The best suggestion I've had was to use fread(fid,'*char') and then perform bin2dec on that but I suspect I had a syntax problem because it said that character was too long.

The next question is, how do you know that the result is incorrect? Do you have knowledge of what the answer should be, or are you getting syntax errors in Matlab? Is your goal to view the numbers in their decimal (e.g., 7) or binary (e.g., 0111) form?

It should be pretty straightforward either way:
fid = fopen(fileName); % open file
word1=fread(fid,1,'*int32'); % for a signed integer of 32 bits
words2_thru_5=fread(fid,4,'*int32'); % read the next 4 consecutive words into a vector
word1_str=dec2bin(word1); % Convert word 1 to a string of 0's and 1's

If you're still not getting a correct answer, you should figure out what endian format the data was saved in and use that when you call fopen.

I'm confused on why someone would tell you to read it in as *char and then use bin2dec. Are you reading a text file (can you edit the file in a text editor)? fread is not the appropriate function if that's the case.