473,602 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Little Endian to big endian for a union

Hi,
I am sending this question again as new question rather than reply to old
question
Please refer below:
struct raw_data
{
unsigned char x;
unsigned char y;
};
union full_data
{
unsigned int actual_value; //int size is 2 bytes
struct raw_data A; //2 chars means 2 bytes
};

I have two compilers for two different controllers, one with little endian
and one with big endian support.
The number "actual_val ue" is different in both cases because of different
endian-ness.
The actual raw_data structure I use is bigger than this example(8 bytes) and
the data is
interpreted in a structure having 4 integers inside the union.
I would like to know fastest way to do little to big ENDIAN conversion or
vice versa.
I presently interchange two successive bytes (x and y here in example)
before
accessing actual_value for porting purpose.
Last but not the least, I am porting from C16X controller to X51 and using
Keil compiler
for both the versions.

Thank you for the help.
Regards,
Shailendra
Nov 14 '05 #1
2 9745
Mehta Shailendrakumar wrote:

I am sending this question again as new question rather than
reply to old question .... snip ...
I have two compilers for two different controllers, one with
little endian and one with big endian support.


You are asking the wrong question (I think). I believe you have a
defined data stream, with a defined byte order for 16 bit numbers.
That's all you need to know to create portable code. Lets assume
the transmitted numbers appear ms byte first (which is the normal
network order) and that those network bytes are 8 bits (which the
machine bytes need not be). Then we can write:

unsigned int net2uint(FILE *fp)
{
unsigned int tmp;
int ch;

/* ignoring the possibility of EOF for now */
ch = getc(fp);
tmp = ch & 0xff;
ch = getc(fp) & 0xff;
return 256 * tmp + ch;
}

which doesn't care a hoot about the byte sex of the host. If
things aren't coming from a file make suitable adjustments. You
can write a uint2net in a similar manner, using only arithmetic,
and again independent of endianess.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #2
Me
<snip about 16 bit unsigned ints>
I have two compilers for two different controllers, one with little endian
and one with big endian support.
The number "actual_val ue" is different in both cases because of different
endian-ness. <snip> Last but not the least, I am porting from C16X controller to X51 and using
Keil compiler
for both the versions.


I googled your compiler's manual and you can use the rotate intrinsic
functions to do this: _ror(actual_val ue, 8). This may (or may not) be
the fastest way to do this, but this newsgroup is definitely not the
right place to find out that kind of information.

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1905
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are at: http://dcs.nac.uci.edu/~strombrg/converting-binary.html So far, the page includes a variety of programs written in C or python to do:
8
17551
by: aling | last post by:
Given the bit field struct: int main() { union { struct { unsigned short s1 : 4; unsigned short s2 : 3;
18
26685
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
2
15843
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte IBM/370 float values into IEEE 754 'float' values that I can use directly with any modern C environment (that uses IEEE 754). I can handle endian conversion myself (the files are stored as big endian, and I'll process them on a little endian...
2
6543
by: bhatia | 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 }
13
27261
by: junky_fellow | last post by:
Hi guys, I need to convert a big endian integer to little endian integer. (the integer is 4 bytes in size on my implementation). I came up with the following code. I need your comments on this. Please suggest any improvements that can be done. #include <stdio.h> int main(void) {
3
2621
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian numbers (true) or little endian (false). I tried something like the following, but never did find out if it was correct. There are two assumptions here: First, big endian direction from small to large is ordered like a string. Second, endian is...
2
2879
by: John Fisher | last post by:
Hi Group, troubles with converting signed 32.32, little-endian, 2's complement back to floating point. I have been trying to brew it myself. I am running Python 2.5 on a Mac. Here is the C-code I have been trying to leverage: double FPuint8ArrayToFPDouble(uint8 *buffer, int startIndex) { uint32 resultDec = 0;
23
7023
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.