• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
re: Converting four char code
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: Converting four char code


  • Subject: re: Converting four char code
  • From: Diggory Laycock <email@hidden>
  • Date: Tue, 11 Feb 2003 12:10:20 +0000

If you want the code to become an NSString - the easiest way is the Foundation Function: NSString *NSFileTypeForHFSTypeCode(OSType hfsFileTypeCode)

http://developer.apple.com/techpubs/macosx/Cocoa/Reference/Foundation/ ObjC_classic/Functions/FoundationFunctions.html#BCIFDJBB


On Tuesday, February 11, 2003, at 04:27 am, Shawn Erickson wrote:

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.


Diggory Laycock
----------------------
http://www.monkeyfood.com


Diggory Laycock
----------------------
http://www.monkeyfood.com
_______________________________________________
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.

  • Follow-Ups:
    • Re: Converting four char code
      • From: Michael Tsai <email@hidden>
  • Prev by Date: Re: Communication with StartupItem
  • Next by Date: Re: NSDirectoryEnumerator memory problem
  • Previous by thread: Re: Converting four char code
  • Next by thread: Re: Converting four char code
  • Index(es):
    • Date
    • Thread