2016-08-24 09:47:20 u012091216 阅读数 344
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15603 人正在学习 去看看 何山

不明觉厉:

-(int)StringToInt:(NSString*)Data{
    NSData *myData = [data dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte*)[myData bytes];s
    int newValue = 0;
    for(int i = 0; i < myData.length; i++){
        Byte s = bytes[i];
        int t_value = (s&0xff) - 0x30;
        if(t_value >= 0&&t_value <= 9){
            newValue *= 16;
            newValue += t_value;
        }
        else{
            t_value = (s&0xff) - 0x61;
            if(t_value >= 0&&t_value <=6){
                t_value += 10;
                newValue *= 16;
                newValue += t_value; 
            }
        }
    }
}
return newValue;
2015-12-31 15:55:33 iOSbird 阅读数 471
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15603 人正在学习 去看看 何山

将16进制字符串转换成16进制数,返回NSData类型的数据


- (NSData *)transformSignString:(NSString *)hexString{


    int j=0;

    NSInteger desLength = hexString.length/2;

    if (hexString.length%2 != 0) {

        

        return nil;

    }

    Byte bytes[desLength]; ///3ds keyByte 数组, 128

    for(int i=0;i<[hexString length];i++)

    {

        int int_ch; /// 两位16进制数转化后的10进制数

        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)

        int int_ch1;

        if(hex_char1 >= '0' && hex_char1 <='9')

            int_ch1 = (hex_char1-48)*16;   //// 0 Ascll - 48

        else if(hex_char1 >= 'A' && hex_char1 <='F')

            int_ch1 = (hex_char1-55)*16; //// A Ascll - 65

        else

            int_ch1 = (hex_char1-87)*16; //// a Ascll - 97

        i++;

        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)

        int int_ch2;

        if(hex_char2 >= '0' && hex_char2 <='9')

            int_ch2 = (hex_char2-48); //// 0 Ascll - 48

        else if(hex_char2 >= 'A' && hex_char2 <='F')

            int_ch2 = hex_char2-55; //// A Ascll - 65

        else

            int_ch2 = hex_char2-87; //// a Ascll - 97

        

        

        int_ch = int_ch1+int_ch2;

        NSLog(@"int_ch=%d",int_ch);

        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里

        j++;

    }

    NSData *newData = [[NSData alloc] initWithBytes:bytes length:desLength];

    NSLog(@"newData=%@",newData);

    return newData;

}

2017-07-21 11:46:18 churk123 阅读数 255
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15603 人正在学习 去看看 何山

JAVA专业字符串按进制转换补码获取其值的算法直接用parseInt()一句代码即可,OC的话可按照补码的原理一步步实现.

补码:正整数的补码是本身,负数的补码是除符号位全部取反再加一.

如题,要想实现算法,将字符串按照十六进制每两位进行求补码运算获取byte数组即可.

 

+(int )HexToByte:(NSString *)hexString{
    NSInteger length =hexString.length/2;
    Byte res[length];
    int sum =0;
        for(int i=0;i<[hexString length]/2;i++)
        {
            int int_ch;  /// 两位16进制数转化后的10进制数

            unichar hex_char1 = [hexString characterAtIndex:i*2]; ////两位16进制数中的第一位(高位*16)
            int int_ch1;
            if(hex_char1 >= '0' && hex_char1 <='9')
                int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
            else if(hex_char1 >= 'A' && hex_char1 <='F')
                int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
            else
                int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97


            unichar hex_char2 = [hexString characterAtIndex:i*2+1]; ///两位16进制数中的第二位(低位)
            int int_ch2;
            if(hex_char2 >= '0' && hex_char2 <='9')
                int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
            else if(hex_char2 >= 'A' && hex_char2 <='F')
                int_ch2 = hex_char2-55; //// A 的Ascll - 65
            else
                int_ch2 = hex_char2-87; //// a 的Ascll - 97

            int_ch = int_ch1+int_ch2;

            BOOL complentResult =  [self toBinarySystemWithDecimalSystem:int_ch];
            if (!complentResult) {
                res[i] = 256 -  int_ch;
                sum = sum - res[i];

            }else {
                res[i] = int_ch;
                  ///将转化后的数放入Byte数组里
                sum = sum +res[i];
            }


        }

    return sum;
}

+ (BOOL)toBinarySystemWithDecimalSystem:(NSInteger)decimal
{
    NSInteger num = decimal;//[decimal intValue];
    NSInteger remainder = 0;      //余数
    NSInteger divisor = 0;        //除数

    NSString * prepare = @"";

    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%ld",remainder];

        if (divisor == 0)
        {
            break;
        }
    }

    NSString * result = @"";
    for (NSInteger i = prepare.length - 1; i >= 0; i --)
    {
        result = [result stringByAppendingFormat:@"%@",
                  [prepare substringWithRange:NSMakeRange(i , 1)]];
    }

    result = [self resultCompleted:result];

    unichar firstChar = [result characterAtIndex:0];
    BOOL complent;
    if(firstChar == '0'){

        complent = YES;
    }else if (firstChar == '1'){

        complent = NO;

    }else{

        complent = NO;
    }
    return complent;
}

+ (NSString *)toHexStringWithNum:(NSInteger )num{

    NSInteger remainder = 0;      //余数
    NSInteger divisor = 0;        //除数

    NSString * prepare = @"";
    NSArray *arr = @[@"A",@"B",@"C",@"D",@"E",@"F"];
    while (true)
    {
        remainder = num%16;
        divisor = num/16;
        num = divisor;
        if (remainder>9) {
            NSInteger index = remainder - 10;

            prepare = [prepare stringByAppendingFormat:@"%@", arr[index]];
        }else{

            prepare = [prepare stringByAppendingFormat:@"%ld",remainder];
        }

        if (divisor == 0)
        {
            break;
        }
    }

    NSString * result = @"";
    for (NSInteger i = prepare.length - 1; i >= 0; i --)
    {
        result = [result stringByAppendingFormat:@"%@",
                  [prepare substringWithRange:NSMakeRange(i , 1)]];
    }
    return result;

}
#pragma mark  自动补齐8位
+ (NSString *)resultCompleted:(NSString *)resultString{
    NSUInteger lengthAdd = 8- resultString.length;
    NSString *strZero = resultString;
    for (int  i=0; i<lengthAdd; i++) {
        strZero =[NSString stringWithFormat:@"0%@",strZero];
    }
    return strZero;
}


PS:代码中还包括了后续的将补码值转为十六进制字符等操作.代码实现最终功能是根据所给十六进制字符串计算其校验和并转为十六进制字符添加到原有字符串上.

 

2015-08-28 15:41:01 kongu 阅读数 3093
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15603 人正在学习 去看看 何山

iOS 二进制数组转成16进制字符串

 Byte bytes[28];
 [data getBytes:bytes length:28];

 NSMutableString *hexString = [[NSMutableString alloc] init];
 for (int i = 0; i < 28; i++) {
      NSLog(@"%hhu",bytes[i]);
      [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", bytes[i]]];
 }

 NSLog(@" %@  ", hexString);

字符串转二进制

int len = [self length] / 2;    // Target length  
unsigned char *buf = malloc(len)  
unsigned char *whole_byte = buf;  
char byte_chars[3] = {'\0','\0','\0'};  

int i;  
for (i=0; i < [self length] / 2; i++) {  
    byte_chars[0] = [self characterAtIndex:i*2];  
    byte_chars[1] = [self characterAtIndex:i*2+1];  
    *whole_byte = strtol(byte_chars, NULL, 16);  
    whole_byte++;  
}  

NSData *data = [NSData dataWithBytes:buf length:len];  
free( buf );  
return data; 
2015-12-18 11:14:58 IOS_dashen 阅读数 2523
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15603 人正在学习 去看看 何山

把16进制字符串转换成NSData:

-(NSData *)hexString:(NSString *)hexString {
    int j=0;
    Byte bytes[20];
    ///3ds key的Byte 数组, 128位
    for(int i=0; i<[hexString length]; i++)
    {
        int int_ch;  /// 两位16进制数转化后的10进制数
        
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            int_ch2 = hex_char2-87; //// a 的Ascll - 97
        
        int_ch = int_ch1+int_ch2;
        NSLog(@"int_ch=%d",int_ch);
        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里
        j++;
    }
    
    NSData *newData = [[NSData alloc] initWithBytes:bytes length:20];
    
    return newData;
}

把Byte转换成NSData:

    Byte byte[] = {1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    NSData *sData=[NSData dataWithBytes:byte length:10];


没有更多推荐了,返回首页