Creating a Byte Buffer

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

  1. PanJuHwa

    PanJuHwa Guest


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

    More specifically, I have a series of values


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

    How can I do that?
    PanJuHwa, Jun 30, 2003
    1. Advertisements

  2. PanJuHwa

    Simon Biber Guest

    Are you sure your byte can hold 32 bits? This is unusual - the
    only case I have heard of is a DSP embedded system.
    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,
    Simon Biber, Jun 30, 2003
    1. Advertisements

  3. oops...
    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
  4. PanJuHwa

    PanJuHwa Guest

    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;

    //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;


    PanJuHwa, Jun 30, 2003
  5. PanJuHwa

    grobbeltje Guest

    strtol seems to do what you want.
    it can convert a string in just about any useful base to an integer.

    have fun!
    grobbeltje, Jul 1, 2003
  6. PanJuHwa

    Chris Torek Guest

    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,
    Chris Torek, Jul 5, 2003
  7. PanJuHwa

    Joe Wright Guest

    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, Jul 5, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.