473,593 Members | 2,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to convert byte array into integer

I have Java client that connects to C++ server. The client sends
integer in binary using DataOutputStrea m 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 41537

msosn...@gmail. com wrote:
I have Java client that connects to C++ server. The client sends
integer in binary using DataOutputStrea m 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_cas t<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 DataOutputStrea m 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_cas t<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_cas t<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_li mits<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
15304
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 the another client in C#. Server side can not be changed :( So, I am using tcp/ip sockets. In the end I get byte on the client side, which originally was some C++ class object, converted to byte array.
5
134739
by: Andrew Inwards | last post by:
Can anyone tell me how to convert a byte array to a stream.? Thanks Andrew
4
33122
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
9605
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 "System.Byte". How do I do this with System.Convert.ToString()???
5
4187
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
29701
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 BitConverter, I get 04030201.
18
43022
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 byte array (byte). Can anyone tell me how to convert byte array to byte pointer in CSharp? Please show me an simple example about how to do it. Thank you for your help
1
4720
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 bytes. Here the value for first 2 bytes (01 00) is 1, next 2 bytes (02 00) is 2. So could some one help me on this.
1
3404
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
7936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8241
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8227
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3853
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1469
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.