how to convert byte array into integer

M

msosno01

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.
 
P

pedagani

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]));
}
 
M

msosno01

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?
 
F

Frederick Gotham

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);
}
 
M

Michiel.Salters

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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top