LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hi

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.
Thanks
Nancy
 
Old 11-26-2008, 04:14 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Did you look in /usr/include/bits/byteswap.h file ?
 
Old 11-26-2008, 10:06 AM   #3
rstewart
Member
 
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.
 
  


Reply


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 04:45 PM
problem in understanding little endian/big endian machine program indian Programming 6 04-19-2006 02:50 PM
big endian little endian? blackzone Linux - Hardware 4 09-23-2004 06:04 AM
What is all this big endian-little endian stuff about? vdemuth Linux - Newbie 1 04-28-2004 02:16 AM
YMF724F PPC Big-Endian ravvar Linux - Hardware 0 10-25-2003 04:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:12 AM.

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