Creating a Byte Buffer

P

PanJuHwa

Hi,

How do I store a 32-bit binary string into a byte?

More specifically, I have a series of values

eg.,
00010001000100010001000100010001
00010001000100010001000100010001

that I wish to store in a byte buffer, with each entry representing each value.

How can I do that?
Thanks!
 
S

Simon Biber

PanJuHwa said:
Hi,

How do I store a 32-bit binary string into a byte?

Are you sure your byte can hold 32 bits? This is unusual - the
only case I have heard of is a DSP embedded system.
More specifically, I have a series of values

eg.,
00010001000100010001000100010001
00010001000100010001000100010001

that I wish to store in a byte buffer, with each entry
representing each value.

What is a byte buffer?

Do you want a string with '0' and '1' characters?
char buf1[] = "00010001000100010001000100010001";

or an array of 4 8-bit bytes with the binary values?
unsigned char buf2[] = {0x11, 0x11, 0x11, 0x11};

or does your byte really hold 32 bits?
unsigned char buf3 = 0x11111111;

or an array of 32 bytes with the values 0 and 1?
unsigned char buf4[] = {0,0,0,1,0,0,0,1,
0,0,0,1,0,0,0,1,
0,0,0,1,0,0,0,1,
0,0,0,1,0,0,0,1};
 
J

John Tsiombikas (Nuclear / the Lab)

oops...
typedef unsigned long uint32;

int Bin32ToArray(unsigned long num, byte *buffer) {
this ^^^^^^^^^^^^^ should be uint32 of course, that's why
I typedefed it so you can just change the typedef on a platform that has
different size of variables...

-- Nuclear / the Lab --
 
P

PanJuHwa

Simon Biber said:
PanJuHwa said:
Hi,

How do I store a 32-bit binary string into a byte?

Are you sure your byte can hold 32 bits? This is unusual - the
only case I have heard of is a DSP embedded system.
More specifically, I have a series of values

eg.,
00010001000100010001000100010001
00010001000100010001000100010001

that I wish to store in a byte buffer, with each entry
representing each value.

What is a byte buffer?

Do you want a string with '0' and '1' characters?
char buf1[] = "00010001000100010001000100010001";

or an array of 4 8-bit bytes with the binary values?
unsigned char buf2[] = {0x11, 0x11, 0x11, 0x11};

Suppose the above is what I want. How can I convert a string
"00010001" to 0x11? Is there a predefined function to do that? Or
should I do this:

unsigned char tmp;
char* str;

while(!feof(inputFile))
{
//sme code to scan in str of 8 char in len. i.e. "00010001"
if(str == "00000001")
buf[0] = 0x01;

else if(str == "00010001")buf[0] = 0x11;

//etc
}



etc
or does your byte really hold 32 bits?
unsigned char buf3 = 0x11111111;

or an array of 32 bytes with the values 0 and 1?
unsigned char buf4[] = {0,0,0,1,0,0,0,1,
0,0,0,1,0,0,0,1,
0,0,0,1,0,0,0,1,
0,0,0,1,0,0,0,1};
 
G

grobbeltje

PanJuHwa said:
Suppose the above is what I want. How can I convert a string
"00010001" to 0x11? Is there a predefined function to do that?
strtol seems to do what you want.
it can convert a string in just about any useful base to an integer.

have fun!
joost.
 
C

Chris Torek

... How can I convert a string "00010001" to 0x11? Is there a
predefined function to do that?

As someone else already noted, strtol() (with base set to 2) will
do the trick.
Or should I do this:

while(!feof(inputFile))
{

You should never, ever do this.

C's feof() function does not attempt to predict the future. (This
is wise, since such predictions eventually fail.) Instead, feof()
"predicts" the past, i.e., "post-dicts". So first, you must invoke
some input operation that fails. The feof() function will then
"post-dict" whether the past failure was due to EOF.

(The other possible reason for a past input failure is, of course,
"error", which one tests with ferror(). For instance, attempting
to read a floppy that has been partially erased by sticking it onto
a refrigerator with a magnet might produce an input failure that
is not an EOF.)

(Incidentally, someday, someone will ask you "what's a floppy"...
you will then know that you, too, are old. :) Personally, I
remember 14-inch disk drives, and was even familiar with punched
cards and paper tape, but never used either of those media myself.
I did wire-wrap a lot of hardware in the 1970s when I was a teenager,
though.)
 
J

Joe Wright

Chris said:
As someone else already noted, strtol() (with base set to 2) will
do the trick.


You should never, ever do this.

C's feof() function does not attempt to predict the future. (This
is wise, since such predictions eventually fail.) Instead, feof()
"predicts" the past, i.e., "post-dicts". So first, you must invoke
some input operation that fails. The feof() function will then
"post-dict" whether the past failure was due to EOF.

(The other possible reason for a past input failure is, of course,
"error", which one tests with ferror(). For instance, attempting
to read a floppy that has been partially erased by sticking it onto
a refrigerator with a magnet might produce an input failure that
is not an EOF.)

(Incidentally, someday, someone will ask you "what's a floppy"...
you will then know that you, too, are old. :) Personally, I
remember 14-inch disk drives, and was even familiar with punched
cards and paper tape, but never used either of those media myself.
I did wire-wrap a lot of hardware in the 1970s when I was a teenager,
though.)

The 'floppy' was the IBM Flexible Disk which was eight inches in
diameter. Its raison d'etre (in the late 1960's or early 1970's) was as
media for a microcode loader for the IBM-360. I remember disk drives
with six platters 36 inches in diameter (Brown circa 1963). Paper tape
with 5, 6, 7 and 8 channels. Seven, Nine and Sixteen track mag tape.
Punch cards with round holes.

But I'm a little older you. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top