Creating a Byte Buffer

Discussion in 'C Programming' started by PanJuHwa, Jun 30, 2003.

  1. PanJuHwa

    PanJuHwa Guest

    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!
    PanJuHwa, Jun 30, 2003
    #1
    1. Advertising

  2. PanJuHwa

    Simon Biber Guest

    "PanJuHwa" <> wrote:
    > 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};

    --
    Simon.
    Simon Biber, Jun 30, 2003
    #2
    1. Advertising

  3. 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 --
    John Tsiombikas (Nuclear / the Lab), Jun 30, 2003
    #3
  4. PanJuHwa

    PanJuHwa Guest

    "Simon Biber" <> wrote in message news:<3effa8e7$0$30568$>...
    > "PanJuHwa" <> wrote:
    > > 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};
    PanJuHwa, Jun 30, 2003
    #4
  5. PanJuHwa

    grobbeltje Guest

    PanJuHwa <> wrote:
    > 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.
    grobbeltje, Jul 1, 2003
    #5
  6. PanJuHwa

    Chris Torek Guest

    In article <>
    PanJuHwa <> writes:
    >... 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.)
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Jul 5, 2003
    #6
  7. PanJuHwa

    Joe Wright Guest

    Chris Torek wrote:
    >
    > In article <>
    > PanJuHwa <> writes:
    > >... 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.)


    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. :)
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jul 5, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,381
    John Harrison
    Jun 21, 2004
  2. Replies:
    2
    Views:
    411
    Karl Heinz Buchegger
    Jan 12, 2005
  3. Replies:
    2
    Views:
    602
    sergejusz
    Mar 26, 2007
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    410
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    271
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page