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

Endianness (again)

Hello all,

Sorry for asking too much but I've been playing with some code and just
wrote this function:

void reverse(uint32_t num)
{
uint8_t bytes[4];
printf("sizeof(uint8_t) is %d and bits per byte is: %d\n",
sizeof(uint8_t), CHAR_BIT);
bytes[0] = (uint8_t) ((num & 0x000000FF) >> 0);
bytes[1] = (uint8_t) ((num & 0x0000FF00) >> 8);
bytes[2] = (uint8_t) ((num & 0x00FF0000) >> 16);
bytes[3] = (uint8_t) ((num & 0xFF000000) >> 24);

printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
bytes[3], bytes[2], bytes[1], bytes[0], num);
}

And on my little endian machine it outputs:

sizeof(uint8_t) is 1 and bits per byte is: 8
Number in memory: 0xaa 0xbb 0xcc 0xdd
Actual num is: 0xaabbccdd

Surely the number in memory has to be 0xdd 0xcc 0xbb 0xaa... I'm doing
some really stupid mistake but I can't see it.

Thanks

Jan 22 '06 #1
6 1552

gamehack wrote:
Hello all,

Sorry for asking too much but I've been playing with some code and just
wrote this function:

void reverse(uint32_t num)
{
uint8_t bytes[4];
printf("sizeof(uint8_t) is %d and bits per byte is: %d\n",
sizeof(uint8_t), CHAR_BIT);
bytes[0] = (uint8_t) ((num & 0x000000FF) >> 0);
bytes[1] = (uint8_t) ((num & 0x0000FF00) >> 8);
bytes[2] = (uint8_t) ((num & 0x00FF0000) >> 16);
bytes[3] = (uint8_t) ((num & 0xFF000000) >> 24);

printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
bytes[3], bytes[2], bytes[1], bytes[0], num);
}

And on my little endian machine it outputs:

sizeof(uint8_t) is 1 and bits per byte is: 8
Number in memory: 0xaa 0xbb 0xcc 0xdd
Actual num is: 0xaabbccdd

Surely the number in memory has to be 0xdd 0xcc 0xbb 0xaa... I'm doing
some really stupid mistake but I can't see it.

Thanks


There is nothing in your code which relies on endianess!

You are explicity storing 8-bit chunks of the value in num to bytes and
then printing them out (in reverse order which accounts for your
confusion).

If you looked at the memory at &num then you would see 0xdd 0xcc 0xbb
0xaa.
uint8_t *bytes = (uint8_t*)#

printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
bytes[0], bytes[1], bytes[2], bytes[3], num);

Lucien Kennedy-Lamb

Jan 22 '06 #2

"gamehack" <ga******@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hello all,

Sorry for asking too much but I've been playing with some code and just
wrote this function:

void reverse(uint32_t num)
{
uint8_t bytes[4];
printf("sizeof(uint8_t) is %d and bits per byte is: %d\n",
sizeof(uint8_t), CHAR_BIT);
bytes[0] = (uint8_t) ((num & 0x000000FF) >> 0);
bytes[1] = (uint8_t) ((num & 0x0000FF00) >> 8);
bytes[2] = (uint8_t) ((num & 0x00FF0000) >> 16);
bytes[3] = (uint8_t) ((num & 0xFF000000) >> 24);

printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
bytes[3], bytes[2], bytes[1], bytes[0], num);
}

And on my little endian machine it outputs:

sizeof(uint8_t) is 1 and bits per byte is: 8
Number in memory: 0xaa 0xbb 0xcc 0xdd
Actual num is: 0xaabbccdd

Surely the number in memory has to be 0xdd 0xcc 0xbb 0xaa... I'm doing
some really stupid mistake but I can't see it.

Thanks


The second case in my reply to Abhishek "How to determine the data is stored
in memory" 1/22/06 12:52 pm, might help you solve your problem..

Rod Pemberton
Jan 22 '06 #3
Yeah! I got it :) I just didn't get the address of the actual bytes.

Thanks
Rod Pemberton wrote:
"gamehack" <ga******@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hello all,
[snip]
The second case in my reply to Abhishek "How to determine the data is stored
in memory" 1/22/06 12:52 pm, might help you solve your problem..

Rod Pemberton


Jan 22 '06 #4
gamehack wrote:
Yeah! I got it :) I just didn't get the address of the actual bytes.

Thanks

Please don't top-post.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jan 22 '06 #5
gamehack wrote:

Sorry for asking too much but I've been playing with some code and
just wrote this function:

void reverse(uint32_t num)
{
uint8_t bytes[4];
printf("sizeof(uint8_t) is %d and bits per byte is: %d\n",
sizeof(uint8_t), CHAR_BIT);
bytes[0] = (uint8_t) ((num & 0x000000FF) >> 0);
bytes[1] = (uint8_t) ((num & 0x0000FF00) >> 8);
bytes[2] = (uint8_t) ((num & 0x00FF0000) >> 16);
bytes[3] = (uint8_t) ((num & 0xFF000000) >> 24);

printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
bytes[3], bytes[2], bytes[1], bytes[0], num);
}

And on my little endian machine it outputs:

sizeof(uint8_t) is 1 and bits per byte is: 8
Number in memory: 0xaa 0xbb 0xcc 0xdd
Actual num is: 0xaabbccdd

Surely the number in memory has to be 0xdd 0xcc 0xbb 0xaa... I'm doing
some really stupid mistake but I can't see it.


Try:
printf("Number in memory: %#x %#x %#x %#x\nActual num is:
%#x\n",
bytes[0], bytes[1], bytes[2], bytes[3], num);

Better yet, use:

printf("Number in memory:");
for (i = 0; i < 4; i++) printf(" %x", bytes[i]);
printf("\nActual num is: %#x\n", num);

Long rambling statements are not conducive to accuracy.

--
"If you want to post a followup via groups.google.com, 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
More details at: <http://cfaj.freeshell.org/google/>

Jan 22 '06 #6
gamehack wrote:
Hello all,

Sorry for asking too much but I've been playing with some code and just
wrote this function:

void reverse(uint32_t num)
{
uint8_t bytes[4];
printf("sizeof(uint8_t) is %d and bits per byte is: %d\n",
sizeof(uint8_t), CHAR_BIT);
bytes[0] = (uint8_t) ((num & 0x000000FF) >> 0);
bytes[1] = (uint8_t) ((num & 0x0000FF00) >> 8);
bytes[2] = (uint8_t) ((num & 0x00FF0000) >> 16);
bytes[3] = (uint8_t) ((num & 0xFF000000) >> 24);

printf("Number in memory: %#x %#x %#x %#x\nActual num is: %#x\n",
bytes[3], bytes[2], bytes[1], bytes[0], num);
}

And on my little endian machine it outputs:

sizeof(uint8_t) is 1 and bits per byte is: 8
Number in memory: 0xaa 0xbb 0xcc 0xdd
Actual num is: 0xaabbccdd

Surely the number in memory has to be 0xdd 0xcc 0xbb 0xaa... I'm doing
some really stupid mistake but I can't see it.


You are writing a uint32 to a sequence of uint8_t in a portable(to the
implementations that provides these types) way in big endian
representation. It will output the same thing on a big endian
machine.

Great !
Jan 23 '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
6
by: Tomás | last post by:
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...
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.