 |
 |
String to FourCharCode
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status:
Offline
|
|
Does anyone have some sample code for making a string value (or char) such as "sysz" into the appropriate FourCharCode Mac OS type? I want to loop through a series of FourCharCodes, but I want to define them in an external file (and let others add new ones). What would be great is if I could create a function like:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>OSType myType = MakeFourCharCode(CFStringRef aString) </font>[/code]
I'm pretty new to C programming, so any guidance would be appreciated. I'm assuming that all I have to do is pack the CFStringRef bytes into a 32-bit word, but I'm not sure how to do that.
Many thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
The following code could be tighter and much more terse; however, I wanted it to be as clear as possible.
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
char *s = <font color = red>"WIND"</font>;
unsigned int mac_type = <font color = blue>0</font>;
unsigned int temp = <font color = blue>0</font>;
temp = (unsigned int)s[<font color = blue>0</font>] << <font color = blue>24</font>;
mac_type = mac_type | temp;
temp = (unsigned int)s[<font color = blue>1</font>] << <font color = blue>16</font>;
mac_type = mac_type | temp;
temp = (unsigned int)s[<font color = blue>2</font>] << <font color = blue>8</font>;
mac_type = mac_type | temp;
temp = s[<font color = blue>3</font>];
mac_type = mac_type | temp;
</font>[/code]
[ 11-09-2001: Message edited by: int69h ]
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status:
Offline
|
|
Originally posted by int69h:
<STRONG>The following code could be tighter and much more terse; however, I wanted it to be as clear as possible.]</STRONG>
Thanks for the code! I'll give it a whirl!
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
I'm sure there are much nicer ways of doing it, like for example just doing a memcpy on 4 bytes from a char * into a FourCharCode.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status:
Offline
|
|
Originally posted by Angus_D:
<STRONG>I'm sure there are much nicer ways of doing it, like for example just doing a memcpy on 4 bytes from a char * into a FourCharCode.</STRONG>
Is BlockMove() an analogous call to memcopy()?
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
Originally posted by Angus_D:
<STRONG>I'm sure there are much nicer ways of doing it, like for example just doing a memcpy on 4 bytes from a char * into a FourCharCode.</STRONG>
Angus is right. memcpy(3) or bcopy(3) would be a much nicer (and faster) way to do it. Silly me, bitwise operators were the first thing that came to mind.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status:
Offline
|
|
OK, thanks for the suggestions but I'm still slightly lost. I'm getting the string I want as a CFStringRef. I would assume that I'd first get the C string from the CFStringRef using something like CFStringGetCString() which copies the string bytes in a char *buf. Would I then just do:
OSType myType;
char *buffer;
memcpy(&buffer, &myType, sizeof(buffer));
Do I need to explicitly cast myType when calling memcpy?
Many thanks...
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Well actually, I think that this should work:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>FourCharCode myCode;
char *buffer;
memcpy(&myCode, buffer, sizeof(FourCharCode));</font>[/code]
Where did OSType come from? you shouldn't coy the sizeof(buffer) bytes because that is wrong (you don't want to copy a pointer, you want to copy a FourCharCode), and the function prototype of memcpy is:
void *
memcpy(void *dst, const void *src, size_t len)
So you got the argument order mixed up. For going the other way, you would probably want something like this:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>FourCharCode myCode;
char buffer[<font color = blue>4</font>];
memcpy(&buffer[<font color = blue>0</font>], &myCode, sizeof(FourCharCode));
buffer[<font color = blue>4</font>] = <font color = blue>0</font>;</font>[/code]
That was jus toff the top of my head so I haven't tested it 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
How about the super-turbo version?
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
char *original = 'WIND';
OSType fourCharValue;
<font color = brown>// fourCharValue uses four bytes of storage</font>
<font color = brown>// original is a c-string, so it's stored as</font>
<font color = brown>// 'W','I','N','D','\<font color = blue>0</font>'</font>
<font color = brown>// so just coerce it to the right type and copy it over</font>
fourCharValue = *(OSType *)original;
</font>[/code]
But this is also off the top of my head, so definitely check it first 
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Apr 2001
Status:
Offline
|
|
[blockquote]char *buffer;
memcpy(&myCode, buffer, sizeof(FourCharCode));[/blockquote]
You can't do that. buffer doesn't point to anything. You'll be trashing random memory.
Wade
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Location: London
Status:
Offline
|
|
You can't do that. buffer doesn't point to anything. You'll be trashing random memory.
True, I think that the original poster and subsequent repliers (is that a word?) took it as read that you would have to allocate the memory first.
I happened to need that code recently and this is the full code I used:
char *buffer;
OSType theCreatorCode;
.
.
.
.
buffer=malloc(sizeof(FourCharCode));
bundleSignature=[[someAppBundle infoDictionary] objectForKey:@"CFBundleSignature"];
[bundleSignature getCString: buffer];
memcpy(&theCreatorCode, buffer, sizeof(FourCharCode));
free(buffer);
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|