473,394 Members | 1,755 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,394 software developers and data experts.

Working with Endianness


Let's say you want to write fully portable code that will be writing files
or sending data, and the data is text encoded using Unicode 16-Bit.

Endianness comes into play. I'm writing code at the moment which determines
the Endianness of the architecture, and then converts values to Bigendian if
they need to be converted.

At the moment my code is based around:

typedef unsigned short uint16;

union {
uint16 entire_integral_value;
unsigned char bytes[2];
};
I set the value of each individual byte, and then test the value of
"entire_integral_value" to see what way it's arranged in memory.

I realise that it's Undefined Behaviour to store a value in a union via one
type, and the access it via another type, but it's the only idea I have at
the moment.

And other ways of determining Endianess?

(By the way, I think it's bullshit to note be able to access an unsigned
integral type's bytes via a union...)

-Tomás
Mar 14 '06 #1
6 5281
Tomás <NU**@NULL.NULL> wrote:
typedef unsigned short uint16;

union {
uint16 entire_integral_value;
unsigned char bytes[2];
};
I set the value of each individual byte, and then test the value of
"entire_integral_value" to see what way it's arranged in memory.

I realise that it's Undefined Behaviour to store a value in a union
via one type, and the access it via another type, but it's the only
idea I have at the moment.

And other ways of determining Endianess?


Copying the contents of an int into a large enough array of unsigned
char (or char?) using memcpy (and accessing the char array afterwards)
should be valid. Don't quote me on that tho .. it's just in my mind
having read about this before.

hth
--
jb

(reply address in rot13, unscramble first)
Mar 14 '06 #2
Tomás wrote:

Let's say you want to write fully portable code that will be writing files
or sending data, and the data is text encoded using Unicode 16-Bit.
Do you mean utf-16 or ucs-2 or something else?
Endianness comes into play. I'm writing code at the moment which
determines the Endianness of the architecture, and then converts values to
Bigendian if they need to be converted.
Text that uses encodings with two or more bytes should start with a byte
order mark.
At the moment my code is based around:

typedef unsigned short uint16;

union {
uint16 entire_integral_value;
unsigned char bytes[2];
};
I set the value of each individual byte, and then test the value of
"entire_integral_value" to see what way it's arranged in memory.

I realise that it's Undefined Behaviour to store a value in a union via
one type, and the access it via another type, but it's the only idea I
have at the moment.

And other ways of determining Endianess?
I think there is not much you can do other than rely on the byte order mark
or let the user be able to specify the endianness somehow.
(By the way, I think it's bullshit to note be able to access an unsigned
integral type's bytes via a union...)


Well, a union was never meant for something like this. And there is always
reinterpret_cast.

Mar 14 '06 #3
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> wrote:

typedef unsigned short uint16;

union {
uint16 entire_integral_value;
unsigned char bytes[2];
};
I set the value of each individual byte, and then test the value of
"entire_integral_value" to see what way it's arranged in memory.

I realise that it's Undefined Behaviour to store a value in a union
via one type, and the access it via another type, but it's the only
idea I have at the moment.

And other ways of determining Endianess?

Copying the contents of an int into a large enough array of unsigned
char (or char?) using memcpy (and accessing the char array afterwards)
should be valid. Don't quote me on that tho .. it's just in my mind
having read about this before.


Yep, I see no problem with that either. The union trick OTOH is illegal.

V
--
Please remove capital As from my address when replying by mail
Mar 14 '06 #4

Tomás wrote:
Let's say you want to write fully portable code that will be writing files
or sending data, and the data is text encoded using Unicode 16-Bit.


Why don't choose utf-8 then? This way you don't have to deal with
byte-sex here. BTW, utf-8 is a defacto standard for Linux and I guess
other unixes, so that your fully portable code is portable across nixes
from the start.

Mar 14 '06 #5
The following code snippet may help to find Endianness :

int x = 1;
if(*(char *)&x == 1)
printf("little-endian\n");
else printf("big-endian\n");

Thanx,
Rama

Mar 16 '06 #6
ljk
#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1

int machineEndianness()
{
int i = 1;
char *p = (char *) &i;
if (p[0] == 1) // Lowest address contains the least significant byte
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;
}

It's not very efficient, but still works fine.
Mar 18 '06 #7

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

Similar topics

3
by: kelvSYC | last post by:
Are there any endianness concerns in C++, or does the compiler take care of those details? I ask because I'm not sure if code such as the following have consistent behavior on all platforms. ...
26
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
2
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
72
by: gamehack | last post by:
Hi all, I was thinking today, suppose we have the number n = 0xAB 0xFF which is equivalent to 44031 in decimal. In big endian it will be stored as 10101011 11111111 but in little endian...
18
by: friend.05 | last post by:
Code to check endianness of machine
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
18
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.