Re: Converting four char code
Re: Converting four char code
- Subject: Re: Converting four char code
- From: Shawn Erickson <email@hidden>
- Date: Mon, 10 Feb 2003 20:27:11 -0800
On Monday, February 10, 2003, at 07:37 PM, Greg Hulands wrote:
Hi,
Can anyone tell me how to convert a four-char code into 4 chars?
eg.
'APPL' into 'A' 'P' 'P' 'L'
(the following is not testing nor was a compile attempted...)
Shift and mask is one way...
int fourCharCode = 'APPL';
char fourCharArray[5];
for (int i = 0; i < 4; i++) {
fourCharArray[3-i] = (fourCharCode >> (8*i)) & 0x000000FF;
}
fourCharArray[4] = 0;
or casting is another...
char* fourCharArray;
int fourCharCode = 'APPL';
fourCharArray = (char*) &fourCharCode;
firstChar = fourCharArray[0];
secondChar = fourCharArray[1];
thirdChar = fourCharArray[2];
fourthChar = fourCharArray[3];
or a union...
union {
int fourCharCode;
char fourCharArray[4];
} foo;
foo.fourCharCode = 'APPL';
firstChar = foo.fourCharArray[0];
secondChar = foo.fourCharArray[1];
thirdChar = foo.fourCharArray[2];
fourthChar = foo.fourCharArray[3];
or buffer copying.
The above assumes you are talking about 1 byte characters and not
caring about endian issues.
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.