Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-09-2006, 07:49 PM   #1 (permalink)
JJaech
Registered User
 
Join Date: Sep 2006
Posts: 1
JJaech is on a distinguished road
Endian Help

Ok, so here is the situation:

I recieved an integer from server with the code
int buf1[4];
recv(sock1, buf1, mxsz-1, 0);

I used cout<<buf1 to see that I had recieved "0xbffff520"

My question is how do I convert this from big to little endian. I have read around and believe I need to use ntohl(number); correct? But how would I implement this on my intarry (buf1[4]).

Thank you
JJaech is offline   Reply With Quote
Old 09-09-2006, 09:57 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I'm not sure whether your ints are longs or shorts, but I think int defaults to short, right? Anyway here's an example:
Code:
for( int i =0; i < BUFFLENGTH; i++){
   int tmp = ntohs(buf1[i]);
   buf1[i] = tmp;
}
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 09-10-2006, 05:41 AM   #3 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
Quote:
Originally Posted by teknomage1
I'm not sure whether your ints are longs or shorts, but I think int defaults to short, right? [/code]
int doesn't default to anything.

On x86, int has a size of 4 bytes, short of 2 bytes, and long of 4 bytes. On x86-64, long is 8 byte, int is 4 byte, and short is 2 byte. However, shorts are generally promoted to 4 bytes by many compilers.

Other architectures vary, but int's aren't "longs or shorts", they're ints, and even those numbers above can vary with the compiler.

As for the OP, that code should work, but are you storing 1 integer in 4 slots or 4 integers in that array? That is, is it a big int, because if it is, you may have to modify that code.
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Old 09-10-2006, 01:24 PM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Thanks for the clarification, AssKoala. That implies that it should be ntohl() then.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:52 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting