Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > String to FourCharCode

String to FourCharCode
Thread Tools
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status: Offline
Reply With Quote
Nov 9, 2001, 08:58 AM
 
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
Reply With Quote
Nov 9, 2001, 10:49 AM
 
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>] &lt;&lt; <font color = blue>24</font>;
mac_type = mac_type | temp;

temp = (unsigned int)s[<font color = blue>1</font>] &lt;&lt; <font color = blue>16</font>;
mac_type = mac_type | temp;

temp = (unsigned int)s[<font color = blue>2</font>] &lt;&lt; <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 ]
     
SoClose  (op)
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status: Offline
Reply With Quote
Nov 9, 2001, 12:18 PM
 
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
Reply With Quote
Nov 9, 2001, 01:02 PM
 
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.
     
SoClose  (op)
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status: Offline
Reply With Quote
Nov 9, 2001, 01:56 PM
 
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
Reply With Quote
Nov 9, 2001, 02:25 PM
 
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.
     
SoClose  (op)
Forum Regular
Join Date: Jan 2001
Location: Boston, MA
Status: Offline
Reply With Quote
Nov 12, 2001, 08:05 AM
 
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
Reply With Quote
Nov 12, 2001, 12:44 PM
 
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
Reply With Quote
Dec 8, 2001, 03:26 AM
 
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
Reply With Quote
Dec 8, 2001, 01:56 PM
 
[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
Reply With Quote
Dec 9, 2001, 05:04 AM
 
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);
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:50 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2