views:

1166

answers:

2

I am trying to convert a unsigned char* to int * on in Objective-C on the iPhone. Is there any API that can help with the conversion?

Here's my best attempt:

-(BOOL)MyFunc:Version:(int *)nVer
{
   unsigned char * uszVerSize;
   //trying to assign string to int
   nVer =  uszVerSize[0] ;
}
+1  A: 

Dear Lord, I think you have bigger problems than the one stated above.

You need to convert the chars to an int and return that.

return [[NSNumber numberWithUnsignedChar:uszVerSize] intValue];

You should also learn about pointers and how ints and chars differ in memory. You can't just assign a pointer to a char to a pointer to an int.

edoloughlin
I just realised your return value was a BOOL, which I assume was success/failure. It might be a good exercise for you to work out how to check the result of NSNumber and return the value as intended originally.
edoloughlin
hello Thanks for your quick replying. By mistake i written wrong code that is "nVer = uszVerSize[0] ;" instead of that *nVer = uszVerSize[0] ;.Thanks KamalBhr
KamalBhr
Um, that's still not a reliable way of converting from a chars to ints.
edoloughlin
A: 
const char *asciiCString = [@"abc-zABC-Z" cString];
  int cStringLen = [@"abc-zABC-Z" length];
  for(i=0;i<cStringLen;i++) {
    [asciiMArray addObject: [[NSNumber alloc] initWithInteger: asciiCString[i]]];
    printf("%d\n",asciiCString[i]);
  }

  for(i=0;i<cStringLen;i++) {
    NSLog(@"%@",[asciiMArray objectAtIndex: i]);
    printf("%d\n",asciiCString[i]);
  }

This is a code i wrote yesterday, to test some code of my learning face

It may look naive...but if it helps you....

asciiCString[i] returns you ASCII value of the char referenced at the index.. asciiMArray is a NSMutableArray Object

Sumit M Asok