OC IOS 如何将BCD码转换为IOS 的ACSII码

作者:朱克锋

邮箱:[email protected]

转载请注明出处:http://blog.csdn.net/linux_zkf


NSString* ASCIIConvertFromBCD(NSString *bcdString)

{

    int len = bcdString.length;

    char PDUpack[len/2+1], *PDUStr;

    bzero(PDUpack, len/2+1);


    PDUStr = (char *)[bcdString cStringUsingEncoding:NSUTF8StringEncoding];

for (int i=0; i <len; i+=2) {

char tempchar;

        tempchar = PDUStr[i];

if ((tempchar <='9') && (tempchar >= '0')) {

tempchar = tempchar & 0x0f;

}

else {

tempchar = (tempchar & 0x0f) + 9;

}

PDUpack[i/2] = tempchar;

tempchar = PDUStr[i+1];

if ((tempchar <= '9') && (tempchar >='0')) {

tempchar = tempchar & 0x0f;

}

else {

tempchar = (tempchar & 0x0f) + 9;

}

        PDUpack[i/2] = ((PDUpack[i/2] << 4 ) & 0xf0) + tempchar;

}

    

    NSString* retString = [[NSString alloc] initWithCString:PDUpack encoding:NSUTF8StringEncoding];

    return retString;

}


你可能感兴趣的:(数据结构,ios,iPhone,ios开发)