niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
how to convert byte array into integer
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
msosno01@gmail.com
*nix forums beginner


Joined: 20 Jul 2006
Posts: 3

PostPosted: Thu Jul 20, 2006 9:07 pm    Post subject: how to convert byte array into integer Reply with quote

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.
Back to top
pedagani@gmail.com
*nix forums beginner


Joined: 19 Nov 2005
Posts: 9

PostPosted: Thu Jul 20, 2006 9:14 pm    Post subject: Re: how to convert byte array into integer Reply with quote

msosn...@gmail.com wrote:
Quote:
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]<<Cool|(byte[3]));
}
Back to top
msosno01@gmail.com
*nix forums beginner


Joined: 20 Jul 2006
Posts: 3

PostPosted: Thu Jul 20, 2006 9:39 pm    Post subject: Re: how to convert byte array into integer Reply with quote

pedagani@gmail.com wrote:
Quote:
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]<<Cool|(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?
Back to top
Frederick Gotham
*nix forums Guru


Joined: 09 Jun 2006
Posts: 502

PostPosted: Fri Jul 21, 2006 4:44 am    Post subject: Re: how to convert byte array into integer Reply with quote

Pedagani posted:

Quote:
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<Cool|(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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:57 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Need to convert domain name before relaying jfinn Postfix 0 Tue Sep 16, 2008 12:51 pm
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am
No new posts determine pointer to point to array or single item during... yancheng.cheok@gmail.com C++ 5 Fri Jul 21, 2006 1:17 am
No new posts FAQ 4.41 How can I remove duplicate elements from a list ... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 am
No new posts how to convert byte array into integer msosno01@gmail.com C++ 1 Thu Jul 20, 2006 9:01 pm

Loans | Remortgages | Fast Loans | Credit Cards | Mobile Phones
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1545s ][ Queries: 16 (0.0662s) ][ GZIP on - Debug on ]