NSString to char my[16] - ios

I am having NSString like :
NSString *str = #"225,0,190,169,236,236,113,97,43,102,217,8,113,201,50,45";
how can i convert it to char array as shown below:
char keyPtr[16] = {225,0,190,169,236,236,113,97,43,102,217,8,113,201,50,45}
can anyone help!
Thanks

NSString *input = #"225,0,190,169,236,236,113,97,43,102,217,8,113,201,50,45";
char output[16];
NSArray *elements = [input componentsSeparatedByString:#","];
NSAssert([elements count] == 16, #"Input has wrong number of elements");
unsigned index = 0;
for (NSString *element in elements) {
int value = [element intValue];
NSAssert(value >= 0 && value <= 255, #"Element is invalid");
output[index++] = (char)value;
}
Note: You probably should be using a int[], and not char[], for output.

NSString *str = #"225,0,190,169,236,236,113,97,43,102,217,8,113,201,50,45";
NSArray *array = [str componentsSeparatedByString:#","];
Then use this in a for loop or something to add to your char array:
char* myLilChar = [[array objectAtIndex:i] UTF8String];
Cheers :)

If you want to be as you said, use below
NSString *str = #"225,0,190,169,236,236,113,97,43,102,217,8,113,201,50,45";
NSArray *array = [str componentsSeparatedByString:#","];
unsigned char keyPtr[array.count];
for(int i=0;i<array.count;i++)
keyPtr[i] = (unsigned char)[[array objectAtIndex:i] intValue];

Related

Something goes wrong when I convert NSString to float

Something goes wrong when I convert NSString to float. I got zero value, but why?
NSString *polylineStr = #"51.351305,-0.024239;51.374855,-0.092412";
NSArray *strPolylineArr = [polylineStr componentsSeparatedByString:#";"];
for ( NSString *str in strPolylineArr) {
NSArray *arr = [str componentsSeparatedByString:#","];
NSString *firstString = [arr firstObject];
NSLog(#"__str %# float %f", [arr firstObject], [firstString floatValue]);
}
my output is
__str 51.351305 float 0.000000
__str 51.374855 float 51.374855
I found that something wrong with first string.
HI in your string some character add : '' you can't see but copy character which is on single quote or use below code:
NSString *polylineStr = [NSString stringWithFormat:#"51.351305,-0.024239;51.374855,-0.092412"];
NSArray *strPolylineArr = [polylineStr componentsSeparatedByString:#";"];
for ( NSString *str in strPolylineArr) {
NSArray *arr = [str componentsSeparatedByString:#","];
NSString *firstString = [arr firstObject];
NSLog(#"__str %# float %f", [arr firstObject], [firstString floatValue]);
}

Convert HEX NSString to an ASCII NSString

I got an NSString containing a hex value which I would like to convert in ASCII. How can I do this?
NSString * hexString = // some value
NSString * asciiString = // ? convert hexString to ASCI somehow
INCORRECT APPROACH THAT I TRIED:
I tried the following approach that I found on a similar question but it did not work for me:
NSData *_data = [hexString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableString *_string = [NSMutableString stringWithString:#""];
for (int i = 0; i < _data.length; i++) {
unsigned char _byte;
[_data getBytes:&_byte range:NSMakeRange(i, 1)];
if (_byte >= 32 && _byte < 127) {
[_string appendFormat:#"%c", _byte];
} else {
[_string appendFormat:#"[%d]", _byte];
}
}
asciiString = _string; // Still shows the same as before..
this works for me:
NSString * str = #"312d4555";
NSMutableString * newString = [[NSMutableString alloc] init];
int i = 0;
while (i < [str length]){
NSString * hexChar = [str substringWithRange: NSMakeRange(i, 2)];
int value = 0;
sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
[newString appendFormat:#"%c", (char)value];
i+=2;
}
NSLog(#"%#", newString);
output is: hello

How to count characters inside a NSString

I have a NSString like this
#"1,2,3,4,5,6,7,"
I want to count how many numbers are there in this string. How can I do that.
pls help me
Thank you
NSString *numberString = #"1,2,3,4,5,6,7";
NSArray *numberArray = [numberString componentsSeparatedByString:#","];
NSInteger count = numberArray.count;
Use the below code to get Number count in your string as below.
NSString *str = #"1,2,3,5,6,7,3";
BOOL valid;
int count = 0;
for (int i = 0; i < str.length; i++)
{
char chr = [str characterAtIndex:i];
NSString* string = [NSString stringWithFormat:#"%c" , chr];
NSCharacterSet *alphaNums = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:string];
valid = [alphaNums isSupersetOfSet:inStringSet];
if (!valid)
{
}
else{
count ++;
}
}
NSLog(#"=%d",count);
Your Output is :

How to substring an NSString in iOS

I have an
NSString *str=#"123456789123456789123456";
My new string should be
NSString *newStr =#"1234 5678 9123 4567 8912 3456";
Can any one help me
Thanks in Advance
You can use this..
NSMutableArray *subStrings = [NSMutableArray array];
NSRange range = {0,subStrLength};
for(int i=0;i< [str length]; i+= subStrLength)
{
range.location = i;
[subStrings addObject:[str substringWithRange:range];
}
Update: NSString *strWithSpace = [subStrings componentsJoinedByString:#" "];
NSString *str=#"123456789123456789123456";
NSMutableString *mutableStr = [str mutableCopy];
for (int i = 4; i < mutableStr.length; i += 5) {
[mutableStr insertString:#" " atIndex:i];
}

How do I send an array of integers via GKSession bluetooth

I have a problem with sending an array of integers via GKSession.
This is how I did it:
To send it.
-(void)sendData {
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:[NSNumber numberWithInteger:snakeHead.position.x]];
[myArray addObject:[NSNumber numberWithInteger:snakeHead.position.y]];
[myArray addObject:[NSNumber numberWithInteger:direction.x]];
[myArray addObject:[NSNumber numberWithInteger:direction.y]];
[myArray addObject:[NSNumber numberWithInteger:bodyOffsetX]];
[myArray addObject:[NSNumber numberWithInteger:bodyOffsetY]];
[myArray addObject:[NSNumber numberWithInteger:amountBodies]];
NSData* encodedArray = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[snakeSession sendData:encodedArray toPeers:snakePeers withDataMode:GKSendDataReliable
error:nil];
[encodedArray release];
}
The sendData function is called from a scheduler.
To receive it.
-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
context:(void *)context {
NSArray *hisArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
snakeHead2.position = ccp([[hisArray objectAtIndex:0] integerValue],
[[hisArray objectAtIndex:1] integerValue]);
direction2 = ccp([[hisArray objectAtIndex:2] integerValue],
[[hisArray objectAtIndex:3] integerValue]);
bodyOffsetX2 = [[hisArray objectAtIndex:4] integerValue];
bodyOffsetY2 = [[hisArray objectAtIndex:5] integerValue];
amountBodies2 = [[hisArray objectAtIndex:6] integerValue];
}
This does not work and the game crashes.
So what am I doing wrong here?
heres what I would do. it even sends less bytes! (faster)
// to send
int snakeHead_position_x = 0;
int snakeHead_position_y = 0;
int direction_x = 0;
int direction_y = 0;
int bodyOffsetX = 0;
int bodyOffsetY = 0;
int amountBodies = 0;
NSString *package = [NSString stringWithFormat:#"%i|%i|%i|%i|%i|%i|%i",
snakeHead_position_x, snakeHead_position_y, direction_x,
direction_y, bodyOffsetX, bodyOffsetY, amountBodies];
NSData *data = [package dataUsingEncoding:NSASCIIStringEncoding];
assert(data); // this is critical don't remove!
//printf("sending %u bytes...\n", data.length);
[snakeSession sendData:data toPeers:snakePeers withDataMode:GKSendDataReliable error:nil];
// to retrieve
NSArray *intArray = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] componentsSeparatedByString:#"|"];
int snakeHead_position_x = [intArray[0] intValue];
int snakeHead_position_y = [intArray[1] intValue];
int direction_x = [intArray[2] intValue];
int direction_y = [intArray[3] intValue];
int bodyOffsetX = [intArray[4] intValue];
int bodyOffsetY = [intArray[5] intValue];
int amountBodies = [intArray[6] intValue];

Resources