objective-chexnsscanner

8 bit hex string to signed integer


I have an 8 bit Hex Value like: "FB" (-5). Now I need an signed integer value for this hex. The NSScanner don't work on this place, because you need an 4 Byte hex value, to get it negative. Before I start some bit manipulation, maybe someone of you know the better way to do that?


Solution

  • Type casting is not safe but you really need it.

    NSString *tempNumber = @"FB";
    NSScanner *scanner = [NSScanner scannerWithString:tempNumber];
    unsigned int temp;
    [scanner scanHexInt:&temp];
    int actualInt = (char)temp; //why char because you have 8 bit integer
    NSLog(@"%@:%d:%d",tempNumber, temp, actualInt);
    

    Console output: FB:251:-5