I have to convert unicode char to NSString
the text i have to convert is doc.title = "hello 64 \U00bf world sample"
my code is:
UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 250, 88)];
const char *cString = [doc.title UTF8String];//where doc.title is above text
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblName.text= string;
NSLog(#"Title is = %# \n",doc.title);
lblName.font=[UIFont boldSystemFontOfSize:16];
lblName.numberOfLines=100;
lblName.backgroundColor=[UIColor clearColor];
lblName.textColor=[UIColor colorWithRed:2.0/255.0 green:104.0/255.0 blue:170.0/255.0 alpha:1.0];
[cell.contentView addSubview:lblName];
[lblName release];
where am i doing wrong?
Tried the link but could not get it right.
+(NSString *)getTextFromChar:(unichar)character
{
NSString *string = [NSString stringWithFormat:#"%C",character];
return string;
}
Pass your cString to this method
try this
char *cString[] = [doc.title UTF8String];//where doc.title is above text
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblName.text= string;
or simply call
-[NSString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES].
Related
I have string
this string is encoded with euc-kr..
I try this code
NSString *str = MjAxObPitbUgx9C7/bq4yKPAzrfCKLnov/LFzcH2xbTAzCkguPDB/SCw+LDt
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingEUC_KR];
but result is null..
what is wrong..? and I try
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingDOSKorean];
and
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingMacKorean];
but result is same..
encoding:kCFStringEncodingMacKorean's result is not korean.;;;;;
Here is a working solution:
Starting with:
NSString *str = #"MjAxObPitbUgx9C7/bq4yKPAzrfCKLnov/LFzcH2xbTAzCkguPDB/SCw+LDt";
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
What you missed is that you can't really use kCFStringEncodingEUC_KR which is a CFStringEncodings with initWithData:encoding: with expects a NSStringEncoding.
You can use CFStringConvertEncodingToNSStringEncoding() to convert it:
NSString *result = [[NSString alloc] initWithData:decodedData encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_KR)];
NSLog(#"result: %#", result);
Output:
$>result: 2019년도 학생보호인력(배움터지킴이) 모집 공고
If you want to use "natively" the kCFStringEncodingEUC_KR, and all the CFDataRef, CFStringRef, and use the bridge then to convert CFStringRef to NSString:
CFStringRef ref = CFStringCreateWithBytes(nil, [decodedData bytes], [decodedData length], kCFStringEncodingEUC_KR, false);
NSString *result = (__bridge NSString *)ref;
NSLog(#"result: %#", result);
Output:
$>result: 2019년도 학생보호인력(배움터지킴이) 모집 공고
You might need to do some changes, I'm not really familiar with CFStringCreateWithBytes(), check the documentation.
I am trying this.
-(NSString *)convertEmojiIntoUnicode:(NSString *)emojiStr{
NSData *data = [emojiStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return goodValue;
}
For Decoding
-(NSString *)convertUnicodeToEmoji:(NSString *)unicodeStr{
NSString *newString = unicodeStr;
NSData *data1 = [newString dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue1 = [[NSString alloc] initWithData:data1 encoding:NSNonLossyASCIIStringEncoding];
return goodValue1;
}
for characters like è is converting to \350 with above method but I am getting unicode for è- \u00E8 from android and website.
I can decode \350 and \u00E8 both with above decode method(convertUnicodeToEmoji) to è
Simply try out this solution :
NSString *myString = #"Here is conversion of char ë, é";
NSData *mData = [myString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *strResult = [[NSString alloc] initWithData:mData encoding:NSASCIIStringEncoding];
NSLog(#"Result: %#", strResult);
Here is Output :
Result: Here is conversion of char e, e
Hope it will work for you.
i am not able to decode this encoded string to its unicode value:- %5Cu20A8
NSData *unicodedStringData =
[#"%5Cu20A8" dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *stringValue =
[[NSString alloc] initWithData:unicodedStringData encoding:NSUTF8StringEncoding];
I have tried NSASCIIStringEncoding and NSUnicodeStringEncoding as well but it doesnt work.
This Worked:-
NSString *unicodedString = [[dictMain objectForKey:#"currency"] stringByRemovingPercentEncoding];
NSData *unicodedStringData = [unicodedString dataUsingEncoding:NSUTF8StringEncoding];
NSString *currencyUnicode = [[NSString alloc] initWithData:unicodedStringData encoding:NSNonLossyASCIIStringEncoding];
Just removing percent escapes before decoding the string.
Just improved Ashish's answer
NSString *unicodedString = [#"%5Cu20A8" stringByRemovingPercentEncoding];
NSData *unicodedStringData = [unicodedString dataUsingEncoding:NSUTF8StringEncoding];
NSString *currencyUnicode = [[NSString alloc] initWithData:unicodedStringData encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",currencyUnicode);
Output - ₨
Im using the following code to convert base64 string to ordinary string.
NSError *localError = nil;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:myString options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"encoded string , %#",myString);
NSLog(#"Decode String Value: %#", decodedString);
Encoded string prints the base64 string but the decoded string is empty. Why so?
Avoid converting to a string before decoding:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
Some implementations of Base64 add line breaks every 64 characters. You should be able to address this by using this option: NSDataBase64DecodingIgnoreUnknownCharacters.
If you want to encode and decode data then you can use this code.
// Create NSData object
NSData *data1 =[#"My String" dataUsingEncoding:NSUTF8StringEncoding];
// Encoded NSString from NSData
NSString *base64Encoded = [data1 base64EncodedStringWithOptions:0];
NSLog(#"%#",base64Encoded);
// Encoding data
NSData *base64Data = [data1 base64EncodedDataWithOptions:0];
NSLog(#"%#",base64Data);
// Decoding data
NSData *nsdataDecoded = [base64Data initWithBase64EncodedData:base64Data options:0];
NSString *str = [[NSString alloc] initWithData:nsdataDecoded encoding:NSUTF8StringEncoding];
NSLog(#"%#", str);
You can use Base64 library to encode or decode,
https://github.com/dasdom/hAppy/tree/master/base64
Then you can use this code,
NSString *strEncoded = [Base64 encode:data];
I am sending URLRequest as mentioned below.
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *balanceString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
And I am getting #"23.0984 USD" in balanceString
Then I would like to display it on uilabel.tex= 23.09 USD Please help me
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *balanceString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
float amount = [balanceString floatValue]; // This will return 23.0984 as a float
NSString *formattedAmount = [NSString stringWithFormat:#"%01.2f USD", amount];
Then something like this should work:
NSString * inp = #"29.2994 USD";
// Initialize a scanner to process the input
NSScanner * scanner = [NSScanner scannerWithString:inp];
// Pull out a floating point value
float v;
[scanner scanFloat:&v];
NSCharacterSet * ws = [NSCharacterSet whitespaceAndNewlineCharacterSet];
// scan through the whitespace after the float
[scanner scanCharactersFromSet:ws intoString:nil];
NSString * currency;
[scanner scanUpToCharactersFromSet:ws intoString:¤cy];
// write out value:
uilabel.text= [NSString stringWithFormat:#"%.2f %#",v,currency];
There might be easier ways.. But this should work..
NSString *originalString = #"23.0984 USD";
NSString *numberString = [originalString stringByReplacingOccurrencesOfString:#" USD"
withString:#""];
float number = [numberString floatValue];
NSString *changedString = [NSString stringWithFormat:#"%.2f",number];
NSLog(#"ChangedString : %# USD",changedString);
EDIT: This answer is already accepted. But I think a better method to get number from string is to search for spaces rather than replacing hardcoded string #" USD".
NSString *originalString = #"23.0984 USD";
NSString *numberString = nil;
NSArray *parts = [originalString componentsSeparatedByString:#" "]; //space
if(parts && [parts lenght]){
numberString = [parts objectAtIndex:0];
}
float number = [numberString floatValue];
NSString *changedString = [NSString stringWithFormat:#"%.2f",number];
NSLog(#"ChangedString : %# USD",changedString);