|
|
|
|
|
|
| Author |
Message |
msosno01@gmail.com *nix forums beginner
Joined: 20 Jul 2006
Posts: 3
|
Posted: Thu Jul 20, 2006 9:01 pm Post subject:
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. |
|
| Back to top |
|
 |
Thomas J. Gritzan *nix forums Guru Wannabe
Joined: 09 Oct 2005
Posts: 124
|
Posted: Thu Jul 20, 2006 10:53 pm Post subject:
Re: how to convert byte array into integer
|
|
|
msosno01@gmail.com schrieb:
| 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
|
Must be ntohl (since you are converting from _n_etwork to _h_ost byte order.
Try this:
socket->recv((void*)&var1, 4);
var1 = ntohl(var1);
Actually, you don't need the cast (conversion to void* is done
automatically), and an C++ style cast (reinterpret_cast) would be preferred.
| Quote: | 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.
|
--
Thomas |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Mon Dec 01, 2008 7:38 pm | All times are GMT
|
|
Debt Consolidation | Tax | Free Advertising | Electrical Shops | Personal Loans
|
|
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
|
|