473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 41443

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
5
by: Andrew Inwards | last post by:
Can anyone tell me how to convert a byte array to a stream.? Thanks Andrew
4
by: Dan | last post by:
I need to convert a byte array to a string in order to upload a binary file with an httpWebRequest. What's the most efficient way to do such a conversion?
2
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns...
5
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
14
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
1
by: Chintan Shah | last post by:
I have a byte array which looks something like 01 00 02 00 73 45 69 A5 So i have to read first 2 bytes and convert it into integer to get its value. Same for next 2 bytes and then next 4...
1
by: Sharma Ravi | last post by:
I want to convert byte to integer. I use abyte = (int).tobyte(object) but its give the conversion error. Anybody help me.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.