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 [2 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:01 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
Thomas J. Gritzan
*nix forums Guru Wannabe


Joined: 09 Oct 2005
Posts: 124

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

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Mon Dec 01, 2008 7:38 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++ 3 Thu Jul 20, 2006 9:07 pm

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
[ Time: 0.3045s ][ Queries: 16 (0.2128s) ][ GZIP on - Debug on ]