how to convert byte array into integer

Discussion in 'C++' started by msosno01@gmail.com, Jul 20, 2006.

  1. Guest

    I have Java client that connects to C++ server. The client sends
    integer in binary using DataOutputStream write function. I am reading
    these data into buffer. I have to convert this buffer back into
    integer, but I am not sure how to do it.
    This is my code:

    int32_t var1;
    uint8_t buf[4];


    soc = accept();
    while (true)
    {
    socket->recv(&buf, 4);
    var1 = htonl(buf);//here I have to do casting.
    }

    My supervisor said that I must use "void *". I tried different
    combinations like: (char*)(void *)buf, but everything failed in the
    best case, I've been getting some huge numbers (all I was sending was
    numerical one)
    Any help is appreciated.
     
    , Jul 20, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > I have Java client that connects to C++ server. The client sends
    > integer in binary using DataOutputStream write function. I am reading
    > these data into buffer. I have to convert this buffer back into
    > integer, but I am not sure how to do it.
    > This is my code:
    >
    > int32_t var1;
    > uint8_t buf[4];
    >
    >
    > soc = accept();
    > while (true)
    > {
    > socket->recv(&buf, 4);
    > var1 = htonl(buf);//here I have to do casting.
    > }
    >
    > My supervisor said that I must use "void *". I tried different
    > combinations like: (char*)(void *)buf, but everything failed in the
    > best case, I've been getting some huge numbers (all I was sending was
    > numerical one)
    > Any help is appreciated.

    Did you try searching for the solution before you posted? From the
    overwhelming info you have provided, here is what I've to offer.

    unsigned int Byte2Int(char *buff) //module to convert 4 bytes to an
    unsigned integer value
    {
    unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
    return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
    }
     
    , Jul 20, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > wrote:
    > > I have Java client that connects to C++ server. The client sends
    > > integer in binary using DataOutputStream write function. I am reading
    > > these data into buffer. I have to convert this buffer back into
    > > integer, but I am not sure how to do it.
    > > This is my code:
    > >
    > > int32_t var1;
    > > uint8_t buf[4];
    > >
    > >
    > > soc = accept();
    > > while (true)
    > > {
    > > socket->recv(&buf, 4);
    > > var1 = htonl(buf);//here I have to do casting.
    > > }
    > >
    > > My supervisor said that I must use "void *". I tried different
    > > combinations like: (char*)(void *)buf, but everything failed in the
    > > best case, I've been getting some huge numbers (all I was sending was
    > > numerical one)
    > > Any help is appreciated.

    > Did you try searching for the solution before you posted? From the
    > overwhelming info you have provided, here is what I've to offer.
    >
    > unsigned int Byte2Int(char *buff) //module to convert 4 bytes to an
    > unsigned integer value
    > {
    > unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
    > return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
    > }

    Thanks for the response. But the person for whom I am doing this
    project told me that I must use htonl() and void* to move bytes and to
    cast. I found out what htonl is and what void* is, but I cannot put
    them together to make the code work. Maybe you code rewrite the code
    using these two terms?
     
    , Jul 20, 2006
    #3
  4. Pedagani posted:

    > unsigned int Byte2Int(char *buff)
    > {
    > unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
    > return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
    > }



    Ever heard of const?

    I've only written the following code in the last half hour, so it is by no
    means perfect. I've checked over it, but not thoroughly, so it may still
    contain bugs. Feel free to scrutanise:


    #include <climits>
    #include <limits>

    /* Amalg
    -----

    Amalgamates an array of "char unsigned" into
    a different unsigned integer type.

    The boolean template parameter, "MSB_first",
    should be "true" if the first array element
    is the MSB, otherwise it should be false if
    the LSB comes first.

    If the quantity of value representation bits in
    the unsigned integer type is not a multiple of
    CHAR_BIT, then the extraneous bits are retrieved
    from the last byte in the array. For example,
    if CHAR_BIT were to be 8, and if an "unsigned"
    were to consist of 30 value representation bits,
    then the array must consist of at least four bytes.
    Any remaining bits (6 in this example) will be
    retrieved from the fourth "char unsigned".

    The quantity of value representation bits does not
    include the sign bit, and this algorithm does
    not produce the desired result when used with
    signed integer types. Nonetheless, the behaviour
    is well-defined if used with signed integer types.

    Undefined behaviour if "p" does not point to
    an array of sufficient length.
    */


    template<bool MSB_first, class T>
    T Amalg(char unsigned const *p)
    {
    typedef std::numeric_limits<T> I;

    /* The following line should optimise away */
    if(MSB_first) p += I::digits / CHAR_BIT + !!(I::digits % CHAR_BIT);

    T val(MSB_first ? *p-- : *p++);

    for(unsigned shift_by = CHAR_BIT;
    shift_by < T(I::digits);
    shift_by += CHAR_BIT)
    val |= T(MSB_first ? *p-- : *p++) << shift_by;

    /* 1st cast: Suppress warning for
    signed/unsigned comparison. */

    /* 2nd cast: In case "char unsigned"
    promotes to "int" rather than
    "unsigned". */

    return val;
    }

    template<bool MSB_first, class T>
    inline T Amalg(char const *const p)
    {
    return Amalg<MSB_first,T>(p);
    }



    --

    Frederick Gotham
     
    Frederick Gotham, Jul 21, 2006
    #4
  5. Guest

    wrote:
    > wrote:
    > > wrote:
    > > > This is my code:
    > > >
    > > > int32_t var1;
    > > > uint8_t buf[4];
    > > >
    > > >
    > > > soc = accept();
    > > > while (true)
    > > > {
    > > > socket->recv(&buf, 4);
    > > > var1 = htonl(buf);//here I have to do casting.
    > > > }
    > > >
    > > > My supervisor said that I must use "void *".

    ....
    > Thanks for the response. But the person for whom I am doing this
    > project told me that I must use htonl() and void* to move bytes and to
    > cast.


    He's wrong. Simple as that. Now, dealing with a boss that's stubborn
    and wrong is off-topic, but that's a problem for another group. The
    basics
    are simple; you basically have a 4-digit number in base 256. Now take
    the rules you learnt for base-10 arithmetic and apply them to base-256.

    HTH,
    Michiel Salters
     
    , Jul 24, 2006
    #5
    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. Spendius
    Replies:
    16
    Views:
    21,536
    Jon Skeet
    Sep 8, 2003
  2. Replies:
    20
    Views:
    9,799
    licebmi
    Sep 7, 2009
  3. Polaris431
    Replies:
    8
    Views:
    760
    SM Ryan
    Dec 4, 2006
  4. Replies:
    1
    Views:
    483
    Thomas J. Gritzan
    Jul 20, 2006
  5. Godzilla
    Replies:
    15
    Views:
    510
    jigloo
    Jul 15, 2007
Loading...

Share This Page