468,513 Members | 1,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

how to convert byte array into integer

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 '06 #1
4 38810

msosn...@gmail.com 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 '06 #2

pe******@gmail.com wrote:
msosn...@gmail.com 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 '06 #3
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<TI;

/* 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
Jul 21 '06 #4
ms******@gmail.com wrote:
pe******@gmail.com wrote:
msosn...@gmail.com 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 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew Inwards | last post: by
4 posts views Thread by Dan | last post: by
2 posts views Thread by Dave | last post: by
5 posts views Thread by Terry Olsen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.