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

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;
}

Related

Convert string from special character [duplicate]

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);

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];

how to convert string to hex in iPhone Development

I'm new to iPhone development, I want to convert a string to hex format.
For example 00A400024F01 to 0x00,0xA4,0x00,0x02,0x4F,0x01 I guess I should start by dividing the string and then convert the grouped value. I don't know how to do that.
u just found answer from this stackoverflow question:-
How to convert an NSString to hex values
+ (NSString *) stringToHex:(NSString *)str
{
NSUInteger len = [str length];
unichar *chars = malloc(len * sizeof(unichar));
[str getCharacters:chars];
NSMutableString *hexString = [[NSMutableString alloc] init];
for(NSUInteger i = 0; i < len; i++ )
{
// [hexString [NSString stringWithFormat:#"%02x", chars[i]]]; /*previous input*/
[hexString appendFormat:#"%02x", chars[i]]; /*EDITED PER COMMENT BELOW*/
}
free(chars);
return [hexString autorelease];
}
UPDATE
You can divide one sting on to two string using below method:-
NSString * mystring = #"Hello,How are you";// suppos your string like that
NSArray * array = [mystring componentsSeparatedByString:#","];
NSString * str1 = [array objectAtIndex:0]; //Hello
NSString * str2 = [array objectAtIndex:1]; //How are you
and if you want to murge two string in to one string like :-
NSString *str1=#"hi Sweet Lady";
NSString *str2=#"How are you";
NSString *mainstr=[NSString stringWithFormat:#"%# %#",str1,str2];
Output is ==== hi Sweet Lady How are you

How to convert NSString to hexadecimal string of fixed block size

In my application I am converting a NSString to HexString. But I always require a fixed size(16 bytes) hex string e.g. if the length of my hex string is 15 bytes, I want it to be 16 bytes. I know that I can add zeros at the beginning of the hex string, but how to add that because simply adding a "0" is not working while I am converting it back into NSString.
You can try this code....
+ (NSString *) stringToHex:(NSString *)str
{
NSUInteger len = [str length];
unichar *chars = malloc(len * sizeof(unichar));
[str getCharacters:chars];
NSMutableString *hexString = [[NSMutableString alloc] init];
for(NSUInteger i = 0; i &lt len; i++ )
{
// [hexString [NSString stringWithFormat:#"%02x", chars[i]]]; //previous input
[hexString appendFormat:#"%02x", chars[i]]; //EDITED PER COMMENT BELOW
}
free(chars);
return [hexString autorelease];
}
I hope this will help you.
happy coding.

Converting NSString, data type expression, to actual NSData

NSString *string1 = #"<616263>";
I want to make this into NSData *data1 = <616263>;
so that when I
NSString *string2 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(#"%#", string2);
Result: abc
would come out
p.s.
<616263>, this is data expression of #"abc"
The trick is converting 616263 to abc. Since you are starting with the ASCII representation of the character codes, you need to convert your NSString to an array of bytes (or your original data source to an array instead of saving it as an NSString in the first place).
NSString *string1 = #"616263";
// Make sure that buffer is big enough!
char sourceChars[7];
[string1 getCString:sourceChars maxLength:7 encoding:NSUTF8StringEncoding];
char destBuffer[3];
char charBuffer[3];
// Loop through sourceChars and convert the ASCII character groups to char's
// NOTE: I assume that these are always two character groupings per your example!
for (int index = 0; index < [string1 length]; index = index + 2) {
// Copy the next two digits into charBuffer
strncpy(charBuffer, &sourceChars[index], 2);
charBuffer[2] = '\0';
// convert charBuffer (ie 61) from hex to decimal
destBuffer[index / 2] = strtol(charBuffer, NULL, 16);
}
// destBuffer is properly formatted: init data1 with it.
NSData *data1 = [NSData dataWithBytes:destBuffer length:[string1 length]/2];
// Test
NSString *string2 = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(#"%#", string2); // Prints abc