OSType

From REALbasicWiki

Jump to: navigation, search

OSType is a string datatype for use with Mac OS external functions.

The C datatype OSType, declared in the Mac OS header file MacTypes.h, is a synonym for UInt32. For mnemonic purposes, an OSType is commonly specified as a four-byte string ( a four-char code) whose bytes are interpreted as a big-endian UInt32, on both PPC and Intel platforms.

'moof' = 1836019558

[edit] Conversion To and From String

REALbasic auto-converts between OSType and String. Given a String, REALbasic either pads it to four bytes or truncates as necessary before doing the conversion. Beware that in REALbasic, string literals are UTF-8 encoded. Thus, for example, the UTF-8 representation of the string literal 'mooĆ’' is five bytes in length, and so will be chopped before conversion. To correctly convert it to an OSType, the string must first be converted to MacRoman.


[edit] Conversion To and From UInt32

REALbasic does not convert OSType to or from UInt32. When declaring external functions, you may need to do it yourself from time to time.

Function Convert(x as OSType) As UInt32
dim char() as String = SplitB(x, "")
return ((AscB(char(0))*256 + AscB(char(1)))*256 + AscB(char(2)))*256 + AscB(char(3))
End Function

Function Convert(N as UInt32) As OSType
dim char(3) as String
char(0) = ChrB(N\&h01000000)
char(1) = ChrB((N and &h00ff0000)\&h00010000)
char(2) = ChrB((N and &h0000ff00)\&h00000100)
char(3) = ChrB((N and &h000000ff))
return Join(char, "")
End Function
Personal tools
related