3

I have done something like:

NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];

NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];

then NSLog(@"%@", str);

However, if 'mystr' is English then the NSLog would print it as is, but if mystr is Arabic (for ex.) NSLog will not print anything, so how can I change the encoding of mystr to UTF8?

5 Answers 5

4

Your first line creates some data that is in cp1251 encoding. Your second line says "read this data into a string, assuming that the bytes represent a UTF8 encoded string". But because the bytes represent a cp1251 encoded string, that's not likely to work very well.

NSString represents an ordered collection of characters. Internally it uses some encoding to store these characters in memory, but its interface provides an encoding-independent access to the string and you can therefore consider NSString to be encoding-agnostic. If what you want is a collection of bytes that represent the string in UTF8 encoding, then you don't want an NSString. You want to get an NSString to emit such a collection of bytes, perhaps using the -dataUsingEncoding: method you've already found.

Sign up to request clarification or add additional context in comments.

Comments

4

Try this one

  NSString *s = @"Some string";
  const char *c = [s UTF8String];

1 Comment

gives syntax error: Array initializer must be an initializer or string literal
1

import

#import "NSString+URLEncoding.h" and
#import "NSString+URLEncoding.m" files

after that where u r doing encode write in .h file this method

-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;

after that write in .m file method implementation

-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding 
{
    return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                               (CFStringRef)self,
                                                               NULL,
                                                               (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                               CFStringConvertNSStringEncodingToEncoding(encoding)));
}

after that use like this

NSString *keyword=@"sample text"; 

here pass ur string whatever

NSString *url = [NSString stringWithFormat:@"%@",[keyword urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",url);

Comments

0

Did you try [mystr UTF8String] ? This returns a char *

5 Comments

whenever I do NSLog(@"%@", [mystr UTF8String]); it crashes the app
That is because [mystr UTF8String] does not return an NSObject, it returns a char *.
use NSLog(@"%s", [mystr UTF8String])
I tried NSLog("%c", [mystr UTF8String]); it displayed unreadable characters
mystr contain this arabic word: تجريب
-2

You can try this 1) NSString to NSData(NSWindowsCP1251StringEncoding

NSString *text=@"This is Sample Text Conversion.....";

NSData *data=[text dataUsingEncoding:NSWindowsCP1251StringEncoding];

2)Revers process.

NSString *textRev=[[NSString alloc]initWithData:data encoding:NSWindowsCP1251StringEncoding];

 NSLog(@" Actual String.. %@",textRev);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.