1. 程式人生 > >IOS中常用的字串、十進位制、二進位制、十六進位制之間互相轉換及簡單算術和

IOS中常用的字串、十進位制、二進位制、十六進位制之間互相轉換及簡單算術和

//普通字串轉換為十六進位制的。
+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 轉換為16進位制。
    NSString *[email protected]"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString
stringWithFormat:@"%x",bytes[i]&0xff];///16進位制數 if([newHexStr length]==1) hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; else hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; } return hexStr; }
// 十六進位制轉換為普通字串的。 
+ (NSString
*)convertHexStrToString:(NSString *)hexString{ if (!hexString || [hexString length] == 0) { return nil; } NSLog(@"line : %d, func: %s ",__LINE__, __func__); char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1); bzero(myBuffer, [hexString length] / 2 + 1); for
(int i = 0; i < [hexString length] - 1; i += 2) { unsigned int anInt; NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)]; NSScanner * scanner = [[[NSScanner alloc] initWithString:hexCharStr] init]; [scanner scanHexInt:&anInt]; myBuffer[i / 2] = (char)anInt; } NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4]; NSLog(@"------字串=======%@",unicodeString); return unicodeString; }
// 十六進位制轉二進位制
+(NSString *)getBinaryByhex:(NSString *)hex
{
    NSMutableDictionary  *hexDic = [[NSMutableDictionary alloc] init];
    hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];    
    [hexDic setObject:@"0000" forKey:@"0"];    
    [hexDic setObject:@"0001" forKey:@"1"];
    [hexDic setObject:@"0010" forKey:@"2"];    
    [hexDic setObject:@"0011" forKey:@"3"];    
    [hexDic setObject:@"0100" forKey:@"4"];    
    [hexDic setObject:@"0101" forKey:@"5"];    
    [hexDic setObject:@"0110" forKey:@"6"];    
    [hexDic setObject:@"0111" forKey:@"7"];    
    [hexDic setObject:@"1000" forKey:@"8"];    
    [hexDic setObject:@"1001" forKey:@"9"];    
    [hexDic setObject:@"1010" forKey:@"A"];    
    [hexDic setObject:@"1011" forKey:@"B"];    
    [hexDic setObject:@"1100" forKey:@"C"];    
    [hexDic setObject:@"1101" forKey:@"D"];    
    [hexDic setObject:@"1110" forKey:@"E"];    
    [hexDic setObject:@"1111" forKey:@"F"];    
    NSMutableString *binaryString=[[NSMutableString alloc] init];    
    for (int i=0; i<[hex length]; i++) {
        NSRange rage;        
        rage.length = 1;        
        rage.location = i;        
        NSString *key = [hex substringWithRange:rage];            
        binaryString = [NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];       
    }   
    return binaryString;    
}
//十進位制轉十六進位制
+ (NSString *)ToHex:(uint16_t)tmpid
{
    NSString *nLetterValue;
    NSString *str [email protected]"";
    uint16_t ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue [email protected]"A";break;
            case 11:
                nLetterValue [email protected]"B";break;
            case 12:
                nLetterValue [email protected]"C";break;
            case 13:
                nLetterValue [email protected]"D";break;
            case 14:
                nLetterValue [email protected]"E";break;
            case 15:
                nLetterValue [email protected]"F";break;
            default:
                nLetterValue = [NSString stringWithFormat:@"%u",ttmpig];                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }      
    }
    return str;
}
//十六進位制簡單算術和
+ (NSString *)hexsumstring:(NSString *)string{
    NSString *hexstring =string;
    int sum = 0;
    // 將16進位制字串,每兩位一組擷取並轉換為10進位制
    for (int i=0; i<hexstring.length-1; i=i+2) {
        long charint = strtoul([[hexstring substringWithRange:NSMakeRange(i, 2)] UTF8String], 0, 16);
        sum = sum+charint;
    }
    if (sum>256) {
        sum = sum-256;
    }
    hexstring = [self ToHex:sum];
    return hexstring;
}