Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reading Binary files

Subject: Reading Binary files

From: V

Date: 15 Jun, 2009 11:00:18

Message: 1 of 9

Hi,
I am trying to read a binary file. However i want to output the data not as a decimal or any other class but in binary format as in : 1 0 1 0.
For example,
Name of binay file is binary_eg.bin

fid = fopen(binary_eg.bin,'r');
data = fread(fid,1,'int8','b'); % b - for big endian

Depending on the value in the file, say it will read as '10'.
Does anyone how i can read the output as binary value and not as decimal if i want to read one byte at a time.

Thanks,
V

Subject: Reading Binary files

From: us

Date: 15 Jun, 2009 11:12:02

Message: 2 of 9

"V" <javiar@rediffmail.com> wrote in message <h159k2$gij$1@fred.mathworks.com>...
> Hi,
> I am trying to read a binary file. However i want to output the data not as a decimal or any other class but in binary format as in : 1 0 1 0.
> For example,
> Name of binay file is binary_eg.bin
>
> fid = fopen(binary_eg.bin,'r');
> data = fread(fid,1,'int8','b'); % b - for big endian
>
> Depending on the value in the file, say it will read as '10'.
> Does anyone how i can read the output as binary value and not as decimal if i want to read one byte at a time.
>
> Thanks,
> V

a hint:

% use (the vectorized)
     help dec2bin;
% to convert your int8 DATA...
% eg,
     r=dec2bin([1,2,3],8)-'0'
%{
% r =
          0 0 0 0 0 0 0 1
          0 0 0 0 0 0 1 0
          0 0 0 0 0 0 1 1
%}

us

Subject: Reading Binary files

From: Rune Allnor

Date: 15 Jun, 2009 11:17:00

Message: 3 of 9

On 15 Jun, 13:00, "V" <jav...@rediffmail.com> wrote:
> Hi,
> I am trying to read a binary file. However i want to output the data not as a decimal or any other class but in binary format as in : 1 0 1 0.
> For example,
> Name of binay file is binary_eg.bin
>
> fid = fopen(binary_eg.bin,'r');
> data = fread(fid,1,'int8','b'); % b - for big endian
>
> Depending on the value in the file, say it will read as '10'.
> Does anyone how i can read the output as binary value and not as decimal if i want to read one byte at a time.

First of all, you might find that your program runs
a lot faster if you write a block of data every time.

However, while not absolutely necessary, you will find
it immensely helpful to know the format of the binary
file. That is, the format of the binary data.

As for your specific question, you can *display* the
value of a number as a binary string as

>> dec2bin(4)

ans =

100

But I doubt this is what you want.

Rune

Subject: Reading Binary files

From: V

Date: 15 Jun, 2009 11:34:01

Message: 4 of 9

Rune Allnor <allnor@tele.ntnu.no> wrote in message <701c55ec-0ec7-441d-81c8-2c2aa90b0985@g19g2000yql.googlegroups.com>...
> On 15 Jun, 13:00, "V" <jav...@rediffmail.com> wrote:
> > Hi,
> > I am trying to read a binary file. However i want to output the data not as a decimal or any other class but in binary format as in : 1 0 1 0.
> > For example,
> > Name of binay file is binary_eg.bin
> >
> > fid = fopen(binary_eg.bin,'r');
> > data = fread(fid,1,'int8','b'); % b - for big endian
> >
> > Depending on the value in the file, say it will read as '10'.
> > Does anyone how i can read the output as binary value and not as decimal if i want to read one byte at a time.
>
> First of all, you might find that your program runs
> a lot faster if you write a block of data every time.
>
> However, while not absolutely necessary, you will find
> it immensely helpful to know the format of the binary
> file. That is, the format of the binary data.
>
> As for your specific question, you can *display* the
> value of a number as a binary string as
>
> >> dec2bin(4)
>
> ans =
>
> 100
>
> But I doubt this is what you want.
>
> Rune

Thanks guys .. i am aware that 'dec2bin' will convert a decimal number into a binary format. However dec2bin will only work for +ve values of the number.
I am told the binary files are written as :"The data format is 16-bit two’s complement (short integer, big-endian*)". However when i read the first two bytes, i am getting rather large values which suggests to me that something is not right when i am reading the files. Code used is:
>> fread(fid,1,'int16','b')
ans
=25574 % not expecting such large values

Hence i want to read one byte at a time and swap the two bytes (because it is big endian) and add them logically to get the final value.

Any ideas??

Cheers,
V

Subject: Reading Binary files

From: us

Date: 15 Jun, 2009 11:57:01

Message: 5 of 9

"V" <javiar@rediffmail.com> wrote in message <h15bj9$ho6$1@fred.mathworks.com>...
> Rune Allnor <allnor@tele.ntnu.no> wrote in message <701c55ec-0ec7-441d-81c8-2c2aa90b0985@g19g2000yql.googlegroups.com>...
> > On 15 Jun, 13:00, "V" <jav...@rediffmail.com> wrote:
> > > Hi,
> > > I am trying to read a binary file. However i want to output the data not as a decimal or any other class but in binary format as in : 1 0 1 0.
> > > For example,
> > > Name of binay file is binary_eg.bin
> > >
> > > fid = fopen(binary_eg.bin,'r');
> > > data = fread(fid,1,'int8','b'); % b - for big endian
> > >
> > > Depending on the value in the file, say it will read as '10'.
> > > Does anyone how i can read the output as binary value and not as decimal if i want to read one byte at a time.
> >
> > First of all, you might find that your program runs
> > a lot faster if you write a block of data every time.
> >
> > However, while not absolutely necessary, you will find
> > it immensely helpful to know the format of the binary
> > file. That is, the format of the binary data.
> >
> > As for your specific question, you can *display* the
> > value of a number as a binary string as
> >
> > >> dec2bin(4)
> >
> > ans =
> >
> > 100
> >
> > But I doubt this is what you want.
> >
> > Rune
>
> Thanks guys .. i am aware that 'dec2bin' will convert a decimal number into a binary format. However dec2bin will only work for +ve values of the number.
> I am told the binary files are written as :"The data format is 16-bit two’s complement (short integer, big-endian*)". However when i read the first two bytes, i am getting rather large values which suggests to me that something is not right when i am reading the files. Code used is:
> >> fread(fid,1,'int16','b')
> ans
> =25574 % not expecting such large values
>
> Hence i want to read one byte at a time and swap the two bytes (because it is big endian) and add them logically to get the final value.
>
> Any ideas??
>
> Cheers,
> V

well, maybe this is (more) helpful to dissect your data into manageable bytes...

     d=int16(25574); % <- your data
     b=typecast(d,'uint8')
% b = 230 99
% now, you can swap, dec2bin, and use a logical op...

just a thought...
us

Subject: Reading Binary files

From: Rune Allnor

Date: 15 Jun, 2009 12:15:44

Message: 6 of 9

On 15 Jun, 13:34, "V" <jav...@rediffmail.com> wrote:

> Thanks guys .. i am aware that 'dec2bin' will convert a decimal number into a binary format. However dec2bin will only work for +ve values of the number.
> I am told the binary files are written as :"The data format is 16-bit two’s complement (short integer, big-endian*)". However when i read the first two bytes, i am getting rather large values which suggests to me that something is not right when i am reading the files. Code used is:>> fread(fid,1,'int16','b')

Then open the file in big-endian mode. Check the
documentation for FOPEN.

Rune

Subject: Reading Binary files

From: V

Date: 15 Jun, 2009 13:10:04

Message: 7 of 9

"us " <us@neurol.unizh.ch> wrote in message <h15cud$be5$1@fred.mathworks.com>...
> "V" <javiar@rediffmail.com> wrote in message <h15bj9$ho6$1@fred.mathworks.com>...
> > Rune Allnor <allnor@tele.ntnu.no> wrote in message <701c55ec-0ec7-441d-81c8-2c2aa90b0985@g19g2000yql.googlegroups.com>...
> > > On 15 Jun, 13:00, "V" <jav...@rediffmail.com> wrote:
> > > > Hi,
> > > > I am trying to read a binary file. However i want to output the data not as a decimal or any other class but in binary format as in : 1 0 1 0.
> > > > For example,
> > > > Name of binay file is binary_eg.bin
> > > >
> > > > fid = fopen(binary_eg.bin,'r');
> > > > data = fread(fid,1,'int8','b'); % b - for big endian
> > > >
> > > > Depending on the value in the file, say it will read as '10'.
> > > > Does anyone how i can read the output as binary value and not as decimal if i want to read one byte at a time.
> > >
> > > First of all, you might find that your program runs
> > > a lot faster if you write a block of data every time.
> > >
> > > However, while not absolutely necessary, you will find
> > > it immensely helpful to know the format of the binary
> > > file. That is, the format of the binary data.
> > >
> > > As for your specific question, you can *display* the
> > > value of a number as a binary string as
> > >
> > > >> dec2bin(4)
> > >
> > > ans =
> > >
> > > 100
> > >
> > > But I doubt this is what you want.
> > >
> > > Rune
> >
> > Thanks guys .. i am aware that 'dec2bin' will convert a decimal number into a binary format. However dec2bin will only work for +ve values of the number.
> > I am told the binary files are written as :"The data format is 16-bit two’s complement (short integer, big-endian*)". However when i read the first two bytes, i am getting rather large values which suggests to me that something is not right when i am reading the files. Code used is:
> > >> fread(fid,1,'int16','b')
> > ans
> > =25574 % not expecting such large values
> >
> > Hence i want to read one byte at a time and swap the two bytes (because it is big endian) and add them logically to get the final value.
> >
> > Any ideas??
> >
> > Cheers,
> > V
>
> well, maybe this is (more) helpful to dissect your data into manageable bytes...
>
> d=int16(25574); % <- your data
> b=typecast(d,'uint8')
> % b = 230 99
> % now, you can swap, dec2bin, and use a logical op...
>
> just a thought...
> us

Cheers ..thanks for that and correct me if i am wrong .. but if there are negative values in the data then i am forced to use 'int8' rather 'uint8'. And dec2bin will only work for unsigned integers.

V

Subject: Reading Binary files

From: V

Date: 15 Jun, 2009 13:12:01

Message: 8 of 9

Rune Allnor <allnor@tele.ntnu.no> wrote in message <7f52f350-4acb-4781-bc0a-0f2b25e7f4d7@b9g2000yqm.googlegroups.com>...
> On 15 Jun, 13:34, "V" <jav...@rediffmail.com> wrote:
>
> > Thanks guys .. i am aware that 'dec2bin' will convert a decimal number into a binary format. However dec2bin will only work for +ve values of the number.
> > I am told the binary files are written as :"The data format is 16-bit two’s complement (short integer, big-endian*)". However when i read the first two bytes, i am getting rather large values which suggests to me that something is not right when i am reading the files. Code used is:>> fread(fid,1,'int16','b')
>
> Then open the file in big-endian mode. Check the
> documentation for FOPEN.
>
> Rune

Thanks .. i guess you can either use 'ieee-be' or 'b' at 'fopen' or 'fread'. Both give the same answer ..

V

Subject: Reading Binary files

From: V

Date: 15 Jun, 2009 13:34:02

Message: 9 of 9

"V" <javiar@rediffmail.com> wrote in message <h15hb1$klg$1@fred.mathworks.com>...
> Rune Allnor <allnor@tele.ntnu.no> wrote in message <7f52f350-4acb-4781-bc0a-0f2b25e7f4d7@b9g2000yqm.googlegroups.com>...
> > On 15 Jun, 13:34, "V" <jav...@rediffmail.com> wrote:
> >
> > > Thanks guys .. i am aware that 'dec2bin' will convert a decimal number into a binary format. However dec2bin will only work for +ve values of the number.
> > > I am told the binary files are written as :"The data format is 16-bit two’s complement (short integer, big-endian*)". However when i read the first two bytes, i am getting rather large values which suggests to me that something is not right when i am reading the files. Code used is:>> fread(fid,1,'int16','b')
> >
> > Then open the file in big-endian mode. Check the
> > documentation for FOPEN.
> >
> > Rune
>
> Thanks .. i guess you can either use 'ieee-be' or 'b' at 'fopen' or 'fread'. Both give the same answer ..
>
> V

Sorry .. i didnt mean to suggest .. i found the answer .. when i use:
fread(fid,1,'int16','b') or fopen(filename.bin,'r','b') and then use % b- for big endian
i get large values which is incorrect eg 25574.
Basically i am asking how can i read binary files that will output the data which is stored as 16 bit twos complement (short integer, big endian) and display as binary numbers: as in 00000010 00010100.

Cheers guys for looking,
V

Tags for this Thread

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.

Contact us