Convert string from special character [duplicate] - iphone

This question already has answers here:
Converting escaped UTF8 characters back to their original form
(2 answers)
Closed 9 years ago.
I am getting info using social framework and getting facebook info, when i get username in another language i.e Denis then it give me like "\U00e6\U00f8\U00e5", how to convert this string to readable string i.e Denis?

I think these chracters are Unicode chracters, you can try something like this to convert them to UTF-8
http://effbot.org/zone/unicode-convert.htm

Try with following code
Your string that you got,
NSString *myStr = #".....;
And use this code,
const wchar_t *myResulr = (const wchar_t*)[myStr cStringUsingEncoding:NSUTF16StringEncoding];
NSLog(#"\n str2 = %S",myResulr);
This also may be Helpful in your case.

Try this
NSString *str = [NSString stringWithUTF8String:"\u00e6\u00f8\u00e5"];
NSLog(#"%#", str);

I found this usefull solution for my problem. Check this link
OR
int main (int argc, const char * argv[])
{
#autoreleasepool {
NSString *name2escaped = #"\U00e6\U00f8\U00e5";
NSString *name2 = [NSString
stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"name2 = %#", name2);
}
return 0;
}

Try:
NSString *str = #"\U00e6\U00f8\U00e5";
NSString *string = [NSString stringWithCString:[str UTF8String]
encoding:NSUTF8StringEncoding];
NSLog(#"string: %#", string);

Related

how to convert Nsstring value into Hexdecimal in ios [duplicate]

This question already has answers here:
How to convert an NSString to hex values
(8 answers)
Closed 9 years ago.
Hello i'm beginner in iOS In one of my activity I have NSString and want to convert into Hexdecimal format
NSString *str= 131003112444371;
long long decimalRepresentation = 131003112444371;
NSLog(#"date %llx",decimalRepresentation);
NSLog(#"Hex value of char is 0x%02llx",decimalRepresentation);
I am using this I then get result 0x772589fb51d3 and I want to extract this no 772589fb51d3
When I am using these line for this ....
NSString *str1=[NSString stringWithFormat:#"%llu",decimalRepresentation];
NSLog(#"str %#",str1);
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"0x"];
str1 = [[str1 componentsSeparatedByCharactersInSet:doNotWant]componentsJoinedByString:#""];
NSLog(#"%#", str1); // => foobarbazfoo
but this line again convert this hexdecimal value into string I don't extract this value
0x772589fb51d3 as 772589fb51d3 .please help me....
Thanks In advance
I have tried like this:-
NsString *str= 131003112444371;
long long decimalRepresentation = 131003112444371;
NSLog(#"date %llx",decimalRepresentation);
NSLog(#"Hex value of char is 0x%02llx",decimalRepresentation);
//just replaced llu to llx
NSString *str1=[NSString stringWithFormat:#"%llx",decimalRepresentation];
NSLog(#"str %#",str1);
And the output i got here is 772589fb51d3
One Possible solution of this question:
+(NSString*)hexFromStr:(NSString*)str
{
NSData* nsData = [str dataUsingEncoding:NSUTF8StringEncoding];
const char* data = [nsData bytes];
NSUInteger len = nsData.length;
NSMutableString* hex = [NSMutableString string];
for(int i = 0; i < len; ++i)[hex appendFormat:#"%02X", data[i]];
return hex;
}

Convert a hash to NSString?

Using the Evernote API, I have an object which has an NSUInteger property called hash. For the specific object I'm looking at, this is equal to:
<f5b5444b 33e740b7 f9d49a3b ddb6a39c>
I want to convert this into an NSString. Doing this:
[NSString stringWithFormat:#"%d", noteResource.hash]
Gives me this:
530049088
How could I correctly convert the hash value to an NSString?
When you see something output as "<" 8 hex digits space .... ">", it's the result of logging a NSData object (NSLog(#"%#", myDataObject);). So I believe what you have is not an NSUInteger, but a NSData * object.
There is no built in method to convert between strings and data, you need to do it in code:
- (NSString *)dataToString:(NSData *)data
{
NSUInteger len = [data length];
NSMutableString *str = [NSMutableString stringWithCapacity:len*2];
const uint8_t *bptr = [data bytes];
while(len--) [str appendFormat:#"%02.2x", *bptr++];
return str;
}
If this works, you can write your own stringToData method reversing the above, if needed.

I would like to ask how to convert hex NSString to long value [duplicate]

This question already has an answer here:
How to get an int in decimal from a Hex NSString
(1 answer)
Closed 9 years ago.
for example i want to covert this to long
NSString *str = #"FFFF00FF";
thanks!
Try to use this one
NSString *str = #"FFFF00FF";
NSScanner* scanner = [NSScanner scannerWithString: str];
unsigned long long longValue;
[scanner scanHexLongLong: &longValue];
NSLog(#"LongValue = %lld", longValue);
i hope it may help you.
long *longString = [YourString longLongValue];
NSString *str = #"FFFF00FF";
long longstring=[str longLongValue];
NSLog(#"%ld",longstring);
try this one.mightbe help ful to you.

Converting UTF8 Hex string to regular UTF8 encoded NSString

I am getting UTF-8 (hex): Hc3b8rt back from a server instead of the string "Hørt".
I need to convert this response to regular UTF-8.
What I have tried:
NSString *string = [dict objectForKey:#"suggest"];
const char *cfilename=[string UTF8String];
NSString *str = [NSString stringWithUTF8String:cfilename];
Thank you for your time!
There's no way you can decode this. As #JoachimIsaksson stated in the comments above, how can you tell if "abba" is exactly "abba" or two unicode chars?
use string encoding, NSISOLatin1StringEncoding
- (id)initWithCString:(const char *)nullTerminatedCString
encoding:(NSStringEncoding)encoding
Or shortly,
NSString *str = [NSString stringWithCString:cfilename
encoding:NSISOLatin1StringEncoding];
Edit after comments:
This is kind of strange. I have done some experiments after your comments and found some strange behaviour.
- (void) testStringEncodingOK {
NSString *string = #"h\u00c3\u00a5r";
const char *cfilename=[string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *cs = [NSString stringWithUTF8String:cfilename];
NSLog(#"String: %#", cs);
}
This output: hår
But if you get the \U in capital, not \u, then I replaced them to \u. And then it did not work. Seem the ,
- (void) testStringEncodingConfused {
NSString *string = #"h\\U00c3\\U00a5r";
string = [string stringByReplacingOccurrencesOfString:#"\\U" withString:#"\\u"];
NSLog(#"Original string:%#", string); // now string = #"h\u00c3\u00a5r"
const char *cfilename=[string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *cs = [NSString stringWithUTF8String:cfilename];
NSLog(#"String: %#", cs);
}
The output is, h\u00c3\u00a5r
Use below code..
const char *ch = [yourstring cStringUsingEncoding:NSISOLatin1StringEncoding];
 yourstring = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(#"%#",yourstring);
let me know it is working or not...
Happy Coding....
use this code
NSString *string = [dict objectForKey:#"suggest"];
const char *cfilename=[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *str = [NSString stringWithUTF8String:cfilename];
and tell if it is working or not.

How to convert unsigned char to NSString in iOS

Can anyone tell me how to convert an unsigned char to an NSString?
Here's the code I am using, but for some reason if I try to do anything with the NSString, like set a UITextView text, it gives me an error. The NSLog works correctly though. Thanks in advance.
- (void)onTagReceived:(unsigned char *)tag
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *myTag = [NSString stringWithFormat:#"%02x%02x%02x%02x%02x\n",tag[0],tag[1],tag[2],tag[3],tag[4]];
NSLog(#"currentTag: %#",myTag);
[displayTxt setText:myTag];
[pool release];
}
If tag is a C string (null-terminated, that is), then you can use [NSString stringWithUTF8String:(char *)tag]. If you want the hex values, then your code using %02x is fine.
#jtbandes: you are correct. The other way you can do this:
NSString *str = [NSString stringWithCString:tag length:strlen(tag)];