The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-07-2008
geet geet is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Red face ntohl

my machine is little endian, and binary data that i need to convert is big endian. hence when i try to convert to text, it gives weird values.
does ntohl function in endian.h help??
where can i get endian.h from?

please someone help!!!!
  #2 (permalink)  
Old 09-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,790
If your binary data is integers ntohl() will help.

WHAT OS? endian.h is not standard for this see:
htons(), htonl(), ntohs(), ntohl()
  #3 (permalink)  
Old 09-08-2008
geet geet is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
ntohl

Quote:
Originally Posted by jim mcnamara View Post
If your binary data is integers ntohl() will help.

WHAT OS? endian.h is not standard for this see:
htons(), htonl(), ntohs(), ntohl()

no my binary data is float?? what to use???
  #4 (permalink)  
Old 09-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
Quote:
Originally Posted by geet View Post
no my binary data is float?? what to use???
Float's a little more tricky. The binary representation may or may not be recognized by the receiving end, even after changing endianness. Your best bet is to convert to BCD of fixed length, or a string using sprintf and scanf.
  #5 (permalink)  
Old 09-08-2008
geet geet is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
ntohl

no my binary data is float!
what to use then if not ntohl???
  #6 (permalink)  
Old 09-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,790
By float do you mean IEEE-754 (4 bytes)?
  #7 (permalink)  
Old 09-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,790
try something like this:

Code:
#include <stdlib.h>
#include <netinet/in.h>

float bin2flt(void *src)
{
	typedef union 
                {float flt; long lng; }
             flt_t;
	flt_t val;
	val.lng=ntohl( *(int*)src);
	return val.flt;
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 -4. The time now is 08:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0