How to "concat" bytes from NSString and its length together? - objective-c

I have a NSString [WORD] that has some length [LEN]. What i need to do is to get bytes from this string and put them together with length in short (2 bytes), so i would have [WORD] [LEN].
E.g.
String "AB" in utf8 HEX is 4142. Length of this string is 2==> 0002 in HEX.
So everything together is 41420002. How to get this bytes together?

I think this code does what you want.
NSString *myString = #"AB";
const char *chars = [myString UTF8String];
NSMutableString * result = [NSMutableString string];
for (int i=0; i < [myString length]; i++) {
[result appendFormat:#"%X", chars[i]];
}
[result appendFormat:#"%04X", [myString length]];
NSLog(#"%#", result);
Hope it helps!

Related

How to handle 32bit unicode characters in a NSString

I have a NSString containing a unicode character bigger than U+FFFF, like the MUSICAL SYMBOL G CLEF symbol '𝄞'. I can create the NSString and display it.
NSString *s = #"A\U0001d11eB"; // "A𝄞B"
NSLog(#"String = \"%#\"", s);
The log is correct and displays the 3 characters. This tells me the NSString is well done and there is no encoding problem.
String = "A𝄞B"
But when I try to loop through all characters using the method
- (unichar)characterAtIndex:(NSUInteger)index
everything goes wrong.
The type unichar is 16 bits so I expect to get the wrong character for the musical symbol. But the length of the string is also incorrect!
NSLog(#"Length = %d", [s length]);
for (int i=0; i<[s length]; i++)
{
NSLog(#" Character %d = %c", i, [s characterAtIndex:i]);
}
displays
Length = 4
Character 0 = A
Character 1 = 4
Character 2 = .
Character 3 = B
What methods should I use to correctly parse my NSString and get my 3 unicode characters?
Ideally the right method should return a type like wchar_t in place of unichar.
Thank you
NSString *s = #"A\U0001d11eB";
NSData *data = [s dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
const wchar_t *wcs = [data bytes];
for (int i = 0; i < [data length]/4; i++) {
NSLog(#"%#010x", wcs[i]);
}
Output:
0x00000041
0x0001d11e
0x00000042
(The code assumes that wchar_t has a size of 4 bytes and little-endian encoding.)
length and charAtIndex: do not give the expected result because \U0001d11e
is internally stored as UTF-16 "surrogate pair".
Another useful method for general Unicode strings is
[s enumerateSubstringsInRange:NSMakeRange(0, [s length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSLog(#"%#", substring);
}];
Output:
A
𝄞
B

Convert text in an NSString to it's 8-byte ASCII Hex equivalent, and store back in an NSString

I want to convert the NSString #"2525" to the NSString #"0032003500320035". The 8-byte ASCII value for "2" in hex is "0032" and for "5" it's "0035". Just to get the c-string equivalent, I tried...
const char *pinUTF8 = [pin cStringUsingEncoding:NSASCIIStringEncoding];
...but as you can see I'm struggling with this and I knew it wasn't going to be that easy. Any tips?
Thanks so much in advance for your wisdom!
Try this:
NSString *str = #"2525";
const char *s = [str cStringUsingEncoding:NSASCIIStringEncoding];
size_t len = strlen(s);
NSMutableString *asciiCodes = [NSMutableString string];
for (int i = 0; i < len; i++) {
[asciiCodes appendFormat:#"%04x", (int)s[i]];
}
NSLog(#"%#", asciiCodes);

XOR'ing two hex values stored as an NSString?

here is yet another silly question from me!
NSString *hex1 = #"50be4f3de4";
NSString *hex2 = #"30bf69a299";
/* some stuff like result = hex1^hex2; */
NSString *result = #"6001269f7d";
I have a hex value as a string, stored in two diff. variables. i need to Xor them and the result should be in another string variables?
i tried them by converting string --> NSData --> bytes array --> xor'ing them ...but i have no success.....
thank you in advance...
You have to convert every character to Base16(for hexadecimal) format first.Then you should proceed with XORing those characters.You can use the strtol() function to achieve this purpose.
NSString *hex1 = #"50be4f3de4";
NSString *hex2 = #"30bf69a299";
NSMutableArray *hexArray1 = [self splitStringIntoChars:hex1];
NSMutableArray *hexArray2 = [self splitStringIntoChars:hex2];
NSMutableString *str = [NSMutableString new];
for (int i=0; i<[hexArray1 count]; i++ )
{
/*Convert to base 16*/
int a=(unsigned char)strtol([[hexArray1 objectAtIndex:i] UTF8String], NULL, 16);
int b=(unsigned char)strtol([[hexArray2 objectAtIndex:i] UTF8String], NULL, 16);
char encrypted = a ^ b;
NSLog(#"%x",encrypted);
[str appendFormat:#"%x",encrypted];
}
NSLog(#"%#",str);
Utility method that i used to split characters of the string
-(NSMutableArray*)splitStringIntoChars:(NSString*)argStr{
NSMutableArray *characters = [[NSMutableArray alloc]
initWithCapacity:[argStr length]];
for (int i=0; i < [argStr length]; i++)
{
NSString *ichar = [NSString stringWithFormat:#"%c", [argStr characterAtIndex:i ]];
[characters addObject:ichar];
}
return characters;
}
Hope it helps!!

NSString get -characters

I want to get the characters of an NSString. Like this:
NSString *data;
const char * typein = [[data characters] UTF8String];
But obviously NSString won't respond to -characters. How do I get the characters of NSString?
thanks,
Elijah
You can use this function:
for(int i =0 ;i<[myString length]; i++) {
char character = [myString characterAtIndex:i];
}
or
NSString *str = #"astring";
const char *cString = [str UTF8String];
If you just want to get a cString from the NSString just call UTF8String as you are already doing and then iterate the array.

How to display hexadecimal bytes using NSLog

How can I display the following bytes using NSLog?
const void *devTokenBytes = [devToken bytes];
Assuming that devToken is of type NSData * (from the bytes call), you can use the description method on NSData to get a string containing the hexadecimal representation of the data's bytes. See the NSData class reference.
NSLog(#"bytes in hex: %#", [devToken description]);
If you want a hex sequence:
NSMutableString *hex = [NSMutableString stringWithCapacity:[devToken length]];
for (int i=0; i < [devToken length]; i++) {
[hex appendFormat:#"%02x", [devToken bytes][i]];
}