Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi all.

In my Iphone application, i successfully converted NSString to const char* using
const char* cString = [nsStringObject cStringUsingEncoding:ASCIIEncoding];
and i made use of the printf("%s",cString) to display it in console...
But its printing only the first letter, not the entire string..
(I am a little weak in pointers)
Is it because of any null termination problems??
Kindly help me in resolving this..

Also i want to know "Why NSLog(nsStringObject) is not printing anything in console?

I wnat to see my NSException reason (which is of NSString type)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I can't say immediately what might cause this, but that encoding doesn't look valid.

The list of NSStringEncoding types is here:
http://developer.apple.com/document...g.html#//apple_ref/doc/c_ref/NSStringEncoding

I would expect an error if you used an encoding that is not supported, but maybe ASCIIEncoding is set to some value that matches up to a Unicode or other 16-bit encoding, and so the result you are getting would be:
Code:
cString[0] = 'M'
cString[1] = '\0'
cString[2] = 'y'
cString[3] = '\0'
cString[4] = ' '
cString[5] = '\0'
cString[6] = 't'
cString[7] = '\0'
cString[8] = 'e'
cString[9] = '\0'
cString[10] = 's'
cString[11] = '\0'
cString[12] = 't'
cString[13] = '\0'

for an NSString containing "My test". Try UTF8String instead of cStringUsingEncoding, or pick a different encoding from that list that you know is 8-bit per character. Otherwise, you cannot use any of the standard C functions that deal with "character strings", due to the reliance on a null terminator, etc.

-Lee
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
My question is: are you really using the value ASCIIEncoding as the encoding type? If so, it isn't what you want. You want NSASCIIEncoding instead.
 

white89gt

macrumors regular
Jan 18, 2006
133
0
I believe you'll have better luck converting you NSString to UTF8. I don't have my Cocoa book in front of me, but I'll post up what it recommends when I get home this afternoon.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hi all.

In my Iphone application, i successfully converted NSString to const char* using
const char* cString = [nsStringObject cStringUsingEncoding:ASCIIEncoding];

There is no identfier "ASCIIEncoding" in any header file, so this shouldn't compile. Do you have a variable named "ASCIIEncoding" anywhere? Or did you use for example kCFStringEncodingASCII (which is very wrong for NSString)?
 

white89gt

macrumors regular
Jan 18, 2006
133
0
I just checked my Cocoa Programming for Mac OS X book and Hillegas recommends the following way to convert NSStrings to c style strings.

NSString *foo = @"your text here";
const char *bar = [foo UTF8String];
 

TeeJayEm

macrumors regular
Mar 28, 2008
104
0
I just checked my Cocoa Programming for Mac OS X book and Hillegas recommends the following way to convert NSStrings to c style strings.

NSString *foo = @"your text here";
const char *bar = [foo UTF8String];

I can confirm, you should be using UTF8String
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I think the OPs question has been answered many times over, it just happens to be 3:15am in Bangalore at the moment. Hopefully we've led them in the right direction.

-Lee
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
My question is: are you really using the value ASCIIEncoding as the encoding type? If so, it isn't what you want. You want NSASCIIEncoding instead.



Sorry...
yeah.. i made use of NSASCIIEncoding...

okie.. now i will try wityh UTF8Encoding .

Thanks to all....

I think the OPs question has been answered many times over, it just happens to be 3:15am in Bangalore at the moment. Hopefully we've led them in the right direction.

-Lee

Thank You Lee..

I got it....
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Sorry...
yeah.. i made use of NSASCIIEncoding...

okie.. now i will try wityh UTF8Encoding .

You can't be using NSASCIIEncoding because there is no NSASCIIEncoding. By asking for help and being too careless to give actual correct information you are just wasting everyone's time.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hi all.

In my Iphone application, i successfully converted NSString to const char* using
const char* cString = [nsStringObject cStringUsingEncoding:ASCIIEncoding];
and i made use of the printf("%s",cString) to display it in console...
But its printing only the first letter, not the entire string..
(I am a little weak in pointers)
Is it because of any null termination problems??
Kindly help me in resolving this..

Also i want to know "Why NSLog(nsStringObject) is not printing anything in console?

I wnat to see my NSException reason (which is of NSString type)

Why are you using ASCII encoding? This will prevent your code from working with lots and lots of possible data. If you need a char*, I would really recommend that you go for UTF8.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.