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 ?
thank you in advance.
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.
Try this one
NSString *s = #"Some string";
const char *c = [s UTF8String];
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);
Did you try [mystr UTF8String] ? This returns a char *
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);
Related
i need to send smiley to other user through iphone app ,so i need to replace \ string with some unique string in obj c.
here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it ..
NSString *str = #"\'ud83d\'ude04";//// here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it
NSString *smileWithString = [str stringByReplacingOccurrencesOfString:#"\'" withString:#":)"];
[smileWithString retain];
NSLog(#"\n\n SmileString %# Str %#",smileWithString);
Update:
Here’s how to convert NSString to NSData – it’s really simple:
NSString *myString = #"Some String";
NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];
And what about the reverse conversion, i.e. how to convert NSData to NSString? Here’s one quick way:
NSString *myString = [NSString stringWithFormat:#"%.*s",[myData length], [myData bytes]];
Use encoding of NSString and when need to use or show string decode it.
Refer base64-encoding link.
Your looking for stringByReplacingOccurrencesOfString that should do the trick.
NSString *newString = [oldString stringByReplacingOccurrencesOfString:#"\" withString:#"uniqueString"];
When I get a string of the form \u043F\u043F (Unicode), how do I convert it to a readable NSUT8String? Here is my code (that fails when these are non English characters):
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *theStr = [[NSString alloc] initWithBytes:[receivedData bytes]
length:[receivedData length] encoding: NSUTF8StringEncoding];
NSLog(theStr);
}
When the string is in English characters everything is fine - but when it is in Unicode format it fails to give me a readable string (but remains in a Unicode format).
What do you think?
EDIT:
I realized I didn't give enough info on what I'm trying to do. I am trying to use youtube's way of getting auto-suggested keywords when you use the search box (nothing official, just used a sniffer to find out). Here it is:
http://suggestqueries.google.com/complete/search?hl=en&client=youtube&hjson=t&ds=yt&jsonp=window.yt.www.suggest.handleResponse&q=*******&cp=******
q is your query and cp is the length of q.
So basically when q is something in English it works fine. But when q has non English characters (Russian for example) this is what I get (from NSLog):
window.yt.www.suggest.handleResponse(["\u043F\u0440",[["\u043F\u0440\u0438\u043A\u043E\u043B\u044B","","0"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D","","1"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 87","","2"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 88","","3"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 86","","4"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 85","","5"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 89","","6"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 84","","7"],["\u043F\u0440\u0438\u043A\u043E\u043B\u044B \u0432 \u043F\u0440\u044F\u043C\u043E\u043C \u044D\u0444\u0438\u0440\u0435","","8"],["\u043F\u0440\u043E\u0436\u0435\u043A\u0442\u043E\u0440\u043F\u0435\u0440\u0438\u0441\u0445\u0438\u043B\u0442\u043E\u043D 90","","9"]],{}])
You can use:
#interface NSString
{
- (__strong const char *)UTF8String; // Convenience to return
// null-terminated UTF8 representation
}
I think this may help..
NSString *yourString = "\u043F\u0440\u0438\u043A\u043E\u043B\u044B";
NSArray *unicodeArray = [yourString componentsSeparatedByString:#"\\u"];
NSMutableString *finalString = [[NSMutableString alloc] initWithString:#""];
for (NSString *unicodeString in unicodeArray) {
if (![unicodeString isEqualToString:#""]) {
unichar codeValue;
[[NSScanner scannerWithString:unicodeString] scanHexInt:&codeValue];
NSString* betaString = [NSString stringWithCharacters:&codeValue length:1];
[finalString appendString:betaString];
}
}
//finalString should have encoded one
Does anyone knows hoe to get a NSString like "ÁlgeBra" to "Algebra", without the accent, and capitalize only the first letter?
Thanks,
RL
dreamlax has already mentioned the capitalizedString method. Instead of doing a lossy conversion to and from NSData to remove the accented characters, however, I think it is more elegant to use the stringByFoldingWithOptions:locale: method.
NSString *accentedString = #"ÁlgeBra";
NSString *unaccentedString = [accentedString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
NSString *capitalizedString = [unaccentedString capitalizedString];
Depending on the nature of the strings you want to convert, you might want to set a fixed locale (e.g. English) instead of using the user's current locale. That way, you can be sure to get the same results on every machine.
NSString has a method called capitalizedString:
Return Value
A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.
NSString *str = #"AlgeBra";
NSString *other = [str capitalizedString];
NSLog (#"Old: %#, New: %#", str, other);
Edit:
Just saw that you would like to remove accents as well. You can go through a series of steps:
// original string
NSString *str = #"ÁlgeBra";
// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
// take the data object and recreate a string using the lossy conversion
NSString *other = [[NSString alloc] initWithData:asciiEncoded
encoding:NSASCIIStringEncoding];
// relinquish ownership
[other autorelease];
// create final capitalized string
NSString *final = [other capitalizedString];
The documentation for dataUsingEncoding:allowLossyConversion: explicitly says that the letter ‘Á’ will convert to ‘A’ when converting to ASCII.
Here's a step by step example of how to do it. There's room for improvement, but you get the basic idea......
NSString *input = #"ÁlgeBra";
NSString *correctCase = [NSString stringWithFormat:#"%#%#",
[[input substringToIndex:1] uppercaseString],
[[input substringFromIndex:1] lowercaseString]];
NSString *result = [[[NSString alloc] initWithData:[correctCase dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease];
NSLog( #"%#", result );
I want to decode an UT8 encoded string.
The input string is "øæ-test-2.txt"
and after decoding it should become
"øæ-test-2.txt"
I found many API to encode the NSString or NSData to UT8 (NSUTF8StringEncoding) but was not able to find the way to decode it.
What I have tried until now:-
NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];
AND
[strToDecode stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
AND
[NSString stringWithUTF8String:[strToDecode cStringUsingEncoding:[NSString defaultCStringEncoding]]]
I have tried the same input string and I get the proper output in third party decoder.
But was not able to get success
Any hint in right direction would be highly appreciated.
I use this one.
NSString *encoded_string = #"ü";
const char *ch = [encoded_string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *decode_string = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(#"%#",decode_String)
[NSString stringWithUTF8String:]
*cipher.h, cipher.m all code : http://watchitlater.com/blog/2010/02/java-and-iphone-aes-interoperability
Cipher.m
-(NSData *)encrypt:(NSData *)plainText{
return [self transform:KCCEncrypt data:plainText;
}
step1.
Cipher *cipher = [[Cipher alloc]initWithKey:#"1234567890"];
NSData *input = [#"kevin" dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [cipher encrypt:input];
data variables NSLog print : <4d1c4d7f 1592718c fd588cec 84053e35>
step2.
NSString *changeVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
data variables NSLog print : null
NSData to NSString by changing the value null is returned. By converting NSString NSURLConnection want to transfer. I need you help
Kevin, I actually take the result of the cipher encrypt method and use an extension to NSData to convert it to a Base64 encoded string. The cipher text does NOT convert to a valid UTF8 string by itself.