View Full Version : Converting NSString to Char
sinclair44
2002.05.27, 03:32 PM
For reasons I won't go into, I would like to put an NSString into a Char array. The code I have written so far (see below) gets a type mismach error.
unsigned int lengthOfMessage = [message length];
char temp[lengthOfMessage + 1];
char temp2[lengthOfMessage + 1];
temp = (char*) [message cString];
temp2 = (char*) [message cString];
'message' is an NSString* containing the data I want into a char array.
-- sinclair44
Feanor
2002.05.27, 05:16 PM
your temp has to be a const char * (check the C rules, but I'm pretty sure that means a pointer to a const char as opposed to a const * to char -- which wouldn't be much use).
So redeclare temp and temp2 and then copy them to another char array if you need to edit them.
Cheers,
B
sinclair44
2002.05.27, 05:54 PM
I was hoping I could use a cast (no need for a million variables) but oh well.
Feanor
2002.05.27, 07:54 PM
The point of a const char is so that the data is protected by the compiler. IF you could just type-cast it then it wouldn't have a whole lot of value, because then it would no longer be read only.
Also, the docs don't mention it but I'd suspect that the string only lives as long as the NSString you get it from, so you'd want to copy the data immediately.
As you'll be copying the data anyway, you really only need one more variable than otherwise. Make a single temp const char * and as many char * variables as strings you need to hold at the same time. The temp is the only extra weight, although the copying is (a tiny bit) more bother.
B
swcrissman
2002.05.27, 08:47 PM
You could also set up a double casting, once as neeed to get the compiler to not complain, and a second const_cast to get the data into the form you want. This of course would be ugly, but if you really want to do it, and know you arent going to forget and break something later, const_cast is available...er, actually, I dunno if you're using objective c++ or not...so I'll revise, and say that it is available if you're using objective c++, but maybe not under plain objective c.
Spencer
OneSadCookie
2002.05.27, 09:18 PM
temp is a char[lengthOfMessage + 1]; [message cString] is a const char*.
I suspect what you want is:
unsigned int lengthOfMessage = [message length];
char temp[lengthOfMessage + 1];
char temp2[lengthOfMessage + 1];
strcpy(temp, [message cString]);
strcpy(temp2, [message cString]);
If you want to be really safe you should really use strncpy instead.
OneSadCookie
2002.05.27, 09:19 PM
Also, remember that cString may return NULL. You might want to consider lossyCString instead.
sinclair44
2002.05.28, 02:53 PM
Originally posted by OneSadCookie
temp is a char[lengthOfMessage + 1]; [message cString] is a const char*.
I suspect what you want is:
unsigned int lengthOfMessage = [message length];
char temp[lengthOfMessage + 1];
char temp2[lengthOfMessage + 1];
strcpy(temp, [message cString]);
strcpy(temp2, [message cString]);
If you want to be really safe you should really use strncpy instead.
That is what I ended up doing.
What is the difference in cString and lossyCString? Also, what is strncpy?
Thanks for the help!
OneSadCookie
2002.05.28, 03:24 PM
cString will only succeed if the string only contains characters from the default C string encoding (either ASCII or MacRoman). Any other characters (such as Japanese ones, for example) will cause cString to return NULL.
lossyCString will convert as much of the source string as it can into a C string, and use question marks to represent characters that can't be converted. It won't ever return NULL AFAIK.
strncpy is like strcpy, but it won't copy more than n characters, so you can guarantee you don't fall off the end of an array. The general pattern for using it is:
char buffer[N + 1];
buffer[N] = '\0';
strncpy(buffer, string, N);
The reason to set the last char in the buffer to '\0' is that if string is >= N characters long, strncpy won't zero-terminate it.
Clear?
sinclair44
2002.05.28, 04:52 PM
Thanks for clarifying!
Now I have another problem in the program that I am making. I am not the most proficient in Obj-C (bascially stumbling in the dark) so I cannot track the problem in my program.
I said I won't go into the details, but I must a little:
I want to make a program that takes to mask numbers, and does an XOR with them (every-other character) to encrypt the text. I have what I think are the functions written correctly but the program crashes with a SIGBUS error (message 10), but only sometimes.
You can download the project at http://homepage.mac.com/jevandyke/FileSharing.html (it's called 'XOR Graphical (Cocoa).sit).
First, what does SIGBUS this mean? Second, how can I fix it?
Whenever the program DOES work, the text you typed in changes into the encrypted text. Can I fix this, too?
Thanks! I really want this to work out.
Codemattic
2002.05.29, 01:00 AM
your setter is wrong. Change setMessage: to
- (void)setMessage:(NSString*)toMessage
{
[message autorelease];
message = [toMessage retain];
}
or,
- (void)setMessage:(NSString*)toMessage
{
[toMessage retain];
[message release];
message = toMessage;
}
or,
- (void)setMessage:(NSString*)toMessage
{
if (toMessage!=message)
{
[message release];
message = [toMessage retain];
}
}
depending on your style.
then at the end of doXorMessage put
// [message dealloc]; //this is wrong
// message = [[NSString alloc] initWithCString:temp2]; //this is wrong
[self setMessage:[NSString stringWithCString:temp2]];
hope this helps,
Codemattic
sinclair44
2002.05.29, 02:21 PM
:eek: /me wonders how much money codemattic wants
THANK YOU! The code (needless to say) works perfectly now.
I still have 1 question: What did the error I was getting mean (SIGBUS, message 10)?
Never mind. I have re-learned a lesson: You should always look in other threads before asking a question.
Thanks again!
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.