Convert UTF-16/32 unicode NSString into UTF16/32Char - unicode

I've got strings that store UTF-16 and UTF-32 unicode value :
NSMutableString *utf16String = [[NSMutableString alloc] init];
[utf16String setString: #"F000"]; //U+F000
NSMutableString *utf32String = [[NSMutableString alloc] init];
[utf32String setString: #"F0000"]; //U+F0000
I want to convert these strings into UTF16Char and UTF32Char in hexadecimal value to obtain the same result as :
UTF16Char utf16Char = 0xF000;
UTF32Char utf32Char = 0xF0000;
Thanks in advance for your help

Related

Conversion of Hex octets to Unicode

NSString* code = #"\x03\x7e";
const char *cString = [code cStringUsingEncoding:NSUnicodeStringEncoding];
NSData* unicodeData = [NSData dataWithBytes:cString length:strlen(cString)];
NSString* convertedString = [[NSString alloc] initWithData:unicodeData encoding:NSUnicodeStringEncoding];
I’d like the convertedString to be the unicode value of \x03\x7e which is a greek question mark (looks kind of like a semicolon). My converted string ends up just as an empty string…
Any idea how I can do this?
Thanks!
Sample Code:
NSString *code = #"\x03\x7e";
NSData *data = [code dataUsingEncoding:NSUnicodeStringEncoding];
NSString *codeNew = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];

NSString unichar from int

I have an int value which I obtained from the character 爸, which is 29240. I can convert this number to hex, but I have no clue how to write the chinese character out in an NSString with only the int 29240.
Basically, what I did was:
NSString * s = #"爸";
int a = [s characterAtIndex:0];
NSLog(#"%d", a);
What it gave as output was 29240.
However, I don't know how to create an NSString that just contains 爸 from only the int 29240.
I converted 29240 into binary which gave me 7238, but I can't seem to create a method which allows me to input any integer and NSLog the corresponding character.
I can hard code it in, so that I have
char cString[] = "\u7238";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"result string: %#", string);
But I'm not sure how to do it with any int.
Thanks to anyone who can help me!
To create a string from one (or more) Unicode characters use initWithCharacters:
unichar c = 29240;
NSString *string = [[NSString alloc] initWithCharacters:&c length:1];
NSString uses UTF-16 characters internally, so
this works for all characters in the "Basic Multilingual Plane", i.e. all characters up to U+FFFF. The following code works for arbitrary characters:
uint32_t ch = 0x1F60E;
ch = OSSwapHostToLittleInt32(ch); // To make it byte-order safe
NSString *s1 = [[NSString alloc] initWithBytes:&ch length:4 encoding:NSUTF32LittleEndianStringEncoding];
NSLog(#"%#", s1);
// Output: 😎
Try out this code snippet to get you started in the right direction:
NSString *s = #"0123456789";
for (int i = 0; i < [s length]; i++) {
NSLog(#"Value: %d", [s characterAtIndex:i]);
}
Just pass in the character as an integer:
unichar decimal = 12298;
NSString *charStr = [NSString stringWithFormat:#"%C", decimal];

Multiple strings in one NSString

i am getting multiple string from HTMl to one NSString, every time the string is replaced by a new string. i tried NSArray it says NSString is strong for array. i want all the strings store in NSString one by one. how can i do that ?
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:nsData];
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p/span ";
// NSString *tutorialsXpathQueryString = #"//div[#class='entry']/ul/li/a";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
// 4
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
// 5
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
// 6
tutorial.title = [[element firstChild] content];
You should use NSMutableString for appending string
NSMutableString aString = [[NSMutableString alloc]initWithString:yourHTMLString];
[aString appendString:parsedHTMLString];

how to store the data into NSMutableString?

hi all how to appends total data to NSMutableString what i am getting data from the
Webservices *details=[[Webservices alloc] init];
[details getMobileNumberDetails:phnotextfield.text];
NSLog(#"longitudes Arrays from %#",details.resultData);
"-91.57696007",
"10.343234",
"74.982343",
"76.464844",
"76.464844",
"2.256",
so help me to store the data into NSMutableString by using for loop in iphone.
thanq for your replaying my another problem is i have to store all data from nsmutablestring into float values. according to your ans, when convert the nsmutablestring values into float as like
float datalist= [ myStr floatValue]; it showing 0.000 value. how to store the total result into float values in iphone
try this code
NSString *XMLStr = [[NSString alloc]initWithBytes:[webData mutableBytes] length:[webData length]encoding:NSUTF8StringEncoding ];
NSLog(#"the xml product is %#",XMLStr);
Use this code i hope it helps You.
I am assuming that tge array has NSNumber objects,
NSMutableString *myStr = [[NSMutableString alloc] init];
for (NSNumber *temp in details.resultData) {
[myStr appendString:#"\""];
[myStr appendFormat:#"%f", temp.floatValue];
[myStr appendFormat:#"\","];
}

NSData bytes to string

I get NSData of bytes that look like this:
2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d353731 35343039 37373139
34383437 34303832 30333533 30383232 380d0a43 6f6e7465 6e742d44 6973706f 73697469 6f6e3a20
666f726d 2d646174 613b206e 616d653d 2266696c 65223b20 66696c65 6e616d65
3d224265 61636820 426f7973 202d2047 6f6f6420 56696272 6174696f 6e732e6d
and i want to convert it to NSString, i tried this method but it give me a nil to the string:
NSString* postInfo = [[NSString alloc] initWithBytes:[postDataChunk bytes] length:[postDataChunk length] encoding:NSUTF8StringEncoding];
You can use,
NSString* newStr = [[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding];
If the data is null-terminated, you should instead use
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
for further reference see these links:
Convert UTF-8 encoded NSData to NSString
NSString class reference
http://homepage.mac.com/mnishikata/objective-c_memo/convert_nsdata_to_nsstring_.html
If you're looking to trace the actual hex values of the NSData object, I use this approach:
uint8_t *bytes = (uint8_t*)myNSDataObject.bytes;
NSMutableString *bytesStr= [NSMutableString stringWithCapacity:sizeof(bytes)*2];
for(int i=0;i<sizeof(bytes);i++){
NSString *resultString =[NSString stringWithFormat:#"%02lx",(unsigned long)bytes[i]];
[bytesStr appendString:resultString];
}