473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Signed 'endian' ; Byte order conversion

1
Dear friends,

( Suppose we change byte orders. For example, our program
reads a file as a character string and converts the byte order. )
// a simple code

#define INTEL_CPU
#ifdef INTEL_CPU
/* *************************** FlipLong() ******************************
* Converts a long in "Big Endian" format (ie, Motorola 68000) to Intel
* reverse-byte format, or vice versa if originally in Big Endian.
************************************************** *******************
*/
void FlipLong(unsigned char * ptr) {
register unsigned char val;

/* Swap 1st and 4th bytes */
val = *(ptr);
*(ptr) = *(ptr+3);
*(ptr+3) = val;

/* Swap 2nd and 3rd bytes */
ptr += 1;
val = *(ptr);
*(ptr) = *(ptr+1);
*(ptr+1) = val;
}
#endif
----------------------------------------****** OR *****-------------------------------------------------

A more efficient function can be implemented using bitwise operations as
shown below:
unsigned long ByteSwap2 (unsigned long nLongNumber)
{
return (((nLongNumber&0x000000FF)<<24)+((nLongNumber&0x00 00FF00)<<8)+
((nLongNumber&0x00FF0000)>>8)+((nLongNumber&0xFF00 0000)>>24));
}

(Both the above samples are declares val/nLongNumber as unsigned char. )

---

Tell me how to deal with sign, or give me sample code
to convert byte orders with signed char.


please any one help me out.


regards,
velpur
Sep 3 '07 #1
3 4907
weaknessforcats
9,208 Expert Mod 8TB
There's no special handling of the sign bit.

For little endian, the sign bit is in the highest memory address byte whereas for big endian it is in the lowest memory address byte.

In both cases, the sign bit is bit 7.

Just keep the byte together.
Sep 3 '07 #2
JosAH
11,448 Expert 8TB
There are macros/functions for that might be useful: htonl, htons, ntohl and ntohs.
The hton* functions convert a long or short from host to network order; network
order is big endian order. The ntoh* convert from network order to host order.

kind regards,

Jos
Sep 3 '07 #3
There are macros/functions for that might be useful: htonl, htons, ntohl and ntohs.
The hton* functions convert a long or short from host to network order; network
order is big endian order. The ntoh* convert from network order to host order.

kind regards,

Jos

-------------

Hi Jos,

I tried also to use macros: htonl and ntohl as the variables exchanged between client and server are defined in double. But it does not work ... and here is a peace of code I wrote:

-------- client.cpp---
...
typedef struct SIM_DATA_IN{
// char svar_instg;
double svar_integ[2];
double svar_indl[5];
};
SIM_DATA_IN stus;
...
stus.svar_integ[0]=htonl(var);
stus.svar_integ[2]=htonl(mr);
...
stus.svar_indl[1]=htonl(GH);
....
stus.svar_indl[5]=htonl(DR);
....

if(sendto(sd,&stus,sizeof(&stus),0,(struct sockaddr *) &pin,sizeof(pin))) {
perror("write data");
exit(1);
}
.....

--------server.cpp-------
...
#define MAXBF 1024
....

typedef struct SIM_DATA_IN{
// char svar_instg;
double svar_integ[2];
double svar_indl[5];
};
SIM_DATA_IN stus;


if(recvfrom(sd,&stus,MAXBF,0,(struct sockaddr *) &pin,&fromSize)) {
perror("read data");
exit(1);
}
.....
...
*spn=ntohl(stus.svar_integ[0]);
*spm=ntohl(stus.svar_integ[1]);
...
*MB=ntohl(stus.svar_indl[0]);
...
*SB=ntohl(stus.svar_indl[5]);
....

================
Is this correct? if yes, it does not work ... I don't get the same data at the server side ... What is the matter then?

Regards and thanks for your reply,
Azwaw
Sep 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. ...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
4
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO...
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
4
by: senthilrag | last post by:
Provided a unsigned integer say (32bits b31-b0 bits) and its required to get (b19-b10) . One approach is have a mask and right shift it #define BIT_MASK 0x000FFC00 uint32 variable; ...
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
33
by: raghu | last post by:
Is it possible to know whether a system is little endian or big endian by writing a C program? If so, can anyone please give me the idea to approach... Thanks a ton. Regards, Raghu
8
by: ma740988 | last post by:
Data stored on a storage device is byte swapped. The data is big endian and my PC is little. At issue: There's a composite type ( a header ) at the front of the files that I'm trying to read in....
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.