473,411 Members | 2,019 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,411 software developers and data experts.

Cast from int to unsigned char in powerpc arch

Hi all,
after all I have to say that I'm not a native english speaker so I pray
you to excuse for my poor english.

I got a trouble writing an application in a powerpc enviroment, that's
the problem:
A long time ago I studied that in powerpc we have got a big-endian
rappresentation and in x86 we got a little-endian.

For example an int like 0x2122 will be 0x00002122 in ppc (big-endian)
and 0x22210000 in x86 (little-endian). I think that's this theory is
right, I tested it with this code:

int main()
{
int i=0x11223344, e=0;
char *p;
for(e=0; e != 4; e++) {
p=(void *)&i+e;
printf("-%x- ",*p);
}
printf("\n");
return 0;
}

It obviously print different value in a powerpc or x86 arch, like
theory says.

So, if I write this piece of code on a x86:

int tmp;
tmp = 0x2122;
printf("casted tmp = %x\n" (unsigned char) tmp);

it returns 0x22, that seems ok, because 22 is the first byte in memory
(little-endian order).
But if I write the same code and compile it on powerpc arch I get the
*same* value!! it's strange, I should get 0x21, shouldn't I?

I don't know, manual says that htons and htonl point to a null macros
and that's ok, we don't need to convert a big-endian into a big-endian,
but with a cast I get this strange behaviour.. probably I mistake in
theory, expecially about a cast..
Any suggestion?

Thanks in advance,
Antonio

Nov 14 '05 #1
3 7141
"nightolo" <ni******@gmail.com> wrote in
news:11**********************@c13g2000cwb.googlegr oups.com:

[snip]
For example an int like 0x2122 will be 0x00002122 in ppc (big-endian)
and 0x22210000 in x86 (little-endian). I think that's this theory is
right, I tested it with this code:

int main()
{
int i=0x11223344, e=0;
char *p;
for(e=0; e != 4; e++) {
p=(void *)&i+e;
printf("-%x- ",*p);
}
printf("\n");
return 0;
}

It obviously print different value in a powerpc or x86 arch, like
theory says.

So, if I write this piece of code on a x86:

int tmp;
tmp = 0x2122;
printf("casted tmp = %x\n" (unsigned char) tmp);

it returns 0x22, that seems ok, because 22 is the first byte in memory
(little-endian order).
But if I write the same code and compile it on powerpc arch I get the
*same* value!! it's strange, I should get 0x21, shouldn't I?


Of course, an no you shouldn't get the wrong answer. The people who write
compilers for big-endian machines know how to make the C langauge work the
same as it does on little-endian machines. C is a portable language. You
might be thinking of pointers, there, the platform-specificness of
endianness can be visible to the programmer.

--
- Mark ->
--
Nov 14 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

nightolo wrote:
Hi all,
after all I have to say that I'm not a native english speaker so I pray
you to excuse for my poor english.

I got a trouble writing an application in a powerpc enviroment, that's
the problem:
A long time ago I studied that in powerpc we have got a big-endian
rappresentation and in x86 we got a little-endian.

For example an int like 0x2122 will be 0x00002122 in ppc (big-endian)
and 0x22210000 in x86 (little-endian). I think that's this theory is
right, I tested it with this code:

int main()
{
int i=0x11223344, e=0;
char *p;
for(e=0; e != 4; e++) {
p=(void *)&i+e;
printf("-%x- ",*p);
}
printf("\n");
return 0;
}

It obviously print different value in a powerpc or x86 arch, like
theory says.

So, if I write this piece of code on a x86:

int tmp;
tmp = 0x2122;
printf("casted tmp = %x\n" (unsigned char) tmp);

it returns 0x22, that seems ok, because 22 is the first byte in memory
(little-endian order).
Actually, endian order has nothing to do with the results you get from /that/
code fragment.
But if I write the same code and compile it on powerpc arch I get the
*same* value!! it's strange, I should get 0x21, shouldn't I?


No. From /that/ code fragment, you should get 0x22 when CHAR_BITS == 8

What you want to try is this...

{
int tmp = 0x2122;

printf("tmp trimmed = %x\n", *((char *)&tmp));
}

The reason your code always prints 22 is that the printf() statement is
evaluating the numerical value of the tmp variable, not it's arrangement in storage.

- - tmp contains the number 0x2122
- - casting tmp to unsigned char causes the value to undergo high-order
truncation, retaining only as much of the value as can fit in one unsigned
char. In your case, the 0x22 part fits in the unsigned char, and the rest
(the overflow) is discarded
- - you then print this value.

What you need to do is to write code to evaluate the individual char-sized
components of the tmp variable. You can do this through a pointer, an array, or
a union. The example above uses a pointer.

[snip]
- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBvcSiagVFX4UWr64RAiXfAJkBphoUyRu9LyQV9+nX6K 89Y/ImjgCeOMmw
UZQsEKentwfGq7bNDoYmOOs=
=hR+f
-----END PGP SIGNATURE-----
Nov 14 '05 #3

Lew Pitcher wrote:
What you need to do is to write code to evaluate the individual char-sized components of the tmp variable. You can do this through a pointer, an array, or a union. The example above uses a pointer.

Ok, thanks a lot, it's my mistake =)

Antonio

Nov 14 '05 #4

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

Similar topics

2
by: wenmang | last post by:
Hi, all: I try to understand whether it is a portable to cast a pointer type, e.g., char *ptr = char array; -- a buffer unsigned char *uptr = ptr; : : function(uptr); -- execute a function...
7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
6
by: Tim | last post by:
I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to pass to a native function call in a bit of managed c++ code like this: Byte field __gc = dynamic_cast<Byte...
4
by: kernelxu | last post by:
Hi,folks. I got some suggestion about bitwise shift from <The C Book, second edition>(written by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This...
1
by: zl2k | last post by:
hi, I am trying to use std hash set to contain the 3D points (x, y, z are of unsigned char type). The easy way comes up is to change the 3 unsigned char to char* so that the std hash set can...
10
by: Neil | last post by:
Question should this cast be necessary? The Compiler is 8 Bits the ints are 16 bits. The Code is for a ring buffer It does not work without the cast in this compiler. I have seen it work...
18
by: Felix Kater | last post by:
I haven't been thinking about it for years but recently I've stumbled on the fact that 'casting' is actually doing (at least) two different things: On the one hand 'casting' means: 'Change...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.