Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-26-2008, 03:09 AM   #1
LQ Newbie
Registered: Nov 2008
Posts: 15

Rep: Reputation: 0
small-endian to big-endian conversion of data to store in a structure


I transfer 7 kb binary data from a windows OS to a linux OS(uST-SH2 cpu, which is big-endian).
I am trying to store the data in some structure at the linux side. At the sender end I write the data stored in structure to a file and then read data from the file to transfer finally.
At the receiver end, first of all complete data is received in a large size buffer and then data from buffer is used to assign to the variables of the structure.
On the windows side variables of structure are defined as ULONG
On the linux side variables of structure are defined as unsigned long

I am not able to store the data in the structure in a correct sequence, though I have written a small code to convert the small-endian format data into big-endian format.

here is the small portion of the code, which I am using to convert small endian to big-endian

BYTE local_val ;
size // this parameter specifies the total number of data bytes received

// convert the data from small endian to big endian
for(k=0;k<size;k=k+4){ local_val = MUXDataBuff[k] ;
MUXDataBuff[k] = MUXDataBuff[k+3] ;
MUXDataBuff[k+3] = local_val ;
local_val = MUXDataBuff[k+1];
MUXDataBuff[k+1] = MUXDataBuff[k+2] ;
MUXDataBuff[k+2] = local_val ;

Kindly help to solve this issue.
Old 11-26-2008, 05:14 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,396

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
Did you look in /usr/include/bits/byteswap.h file ?
Old 11-26-2008, 11:06 AM   #3
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Or convert between host and network byte order using ntohl() and htonl() for longs.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to know the Endianness (BIG or LITTLE ENDIAN) of the processor venkatam Linux - Newbie 1 02-29-2008 05:45 PM
problem in understanding little endian/big endian machine program indian Programming 6 04-19-2006 03:50 PM
big endian little endian? blackzone Linux - Hardware 4 09-23-2004 07:04 AM
What is all this big endian-little endian stuff about? vdemuth Linux - Newbie 1 04-28-2004 03:16 AM
YMF724F PPC Big-Endian ravvar Linux - Hardware 0 10-25-2003 05:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration