16进制转int ios开发

2016-12-27 15:58:01 u011146511 阅读数 1832
  • 03 v4l x264 alsa库

    通过本课程的学习,学员从TCP/IP UDP Socket基础概念,到网络编程接口介绍,POSIX线程封装,私有协议定义,开发,服务器模型,客户端编程等详细实战讲解。大量的实际项目,代码的学习,让学员掌握一个类似无人机,...

    275人学习 陈超
    免费试看

////NSString转换成十六进制的字符串则可使用如下方式:

- (NSString *)ToHex:(uint16_t)tmpid

{

    NSString *nLetterValue;

    NSString *str =@"";

    uint16_t ttmpig;

    NSString *n1;

    

    //    for (int i = 0; i<9; i++) {

    ttmpig=tmpid%16;

    tmpid=tmpid/16;

    switch (ttmpig)

    {

        case10:

            nLetterValue =@"A";break;

        case11:

            nLetterValue =@"B";break;

        case12:

            nLetterValue =@"C";break;

        case13:

            nLetterValue =@"D";break;

        case14:

            nLetterValue =@"E";break;

        case15:

            nLetterValue =@"F";break;

        default:

            nLetterValue = [NSStringstringWithFormat:@"%u",ttmpig];

            

    }

    //        str = [nLetterValue stringByAppendingString:str];

    

    if (tmpid ==0) {

        str=[str stringByAppendingString:[NSStringstringWithFormat:@"0%@",nLetterValue]];

        return str;

    }

    

    if(tmpid >0 && tmpid<=16){

        switch (tmpid)

        {

            case10:

                n1 =@"A";break;

            case11:

                n1 =@"B";break;

            case12:

                n1 =@"C";break;

            case13:

                n1 =@"D";break;

            case14:

                n1 =@"E";break;

            case15:

                n1 =@"F";break;

            default:

                n1 = [NSStringstringWithFormat:@"%u",tmpid];

                

        }

        

        str = [n1 stringByAppendingString:nLetterValue];

    }

    

    

    

    

    //    }

    

    return str;

}



            NSString *tenToHex=[selfToHex:dataBuf[i]& 0xff];//10进制的转换成16进制

            NSLog(@"dataBuff-01-**%@----%@",[NSStringstringWithFormat:@"%d",i],tenToHex);



2015-12-31 15:55:33 iOSbird 阅读数 532
  • 03 v4l x264 alsa库

    通过本课程的学习,学员从TCP/IP UDP Socket基础概念,到网络编程接口介绍,POSIX线程封装,私有协议定义,开发,服务器模型,客户端编程等详细实战讲解。大量的实际项目,代码的学习,让学员掌握一个类似无人机,...

    275人学习 陈超
    免费试看

将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;

}

2018-11-15 17:26:02 qingshuiputeng 阅读数 1437
  • 03 v4l x264 alsa库

    通过本课程的学习,学员从TCP/IP UDP Socket基础概念,到网络编程接口介绍,POSIX线程封装,私有协议定义,开发,服务器模型,客户端编程等详细实战讲解。大量的实际项目,代码的学习,让学员掌握一个类似无人机,...

    275人学习 陈超
    免费试看
#pragma mark - 十六进制转换工具
// 16进制转NSData
- (NSData *)convertHexStrToData:(NSString *)str
{
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    return hexData;
}
// NSData转16进制 第一种
- (NSString *)convertDataToHexStr:(NSData *)data
{
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    return string;
}
//普通字符串转换为十六进制的。
- (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    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 *)stringFromHexString:(NSString *)hexString {
    
    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];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    return unicodeString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

2016-08-31 18:02:00 weixin_33949359 阅读数 61
  • 03 v4l x264 alsa库

    通过本课程的学习,学员从TCP/IP UDP Socket基础概念,到网络编程接口介绍,POSIX线程封装,私有协议定义,开发,服务器模型,客户端编程等详细实战讲解。大量的实际项目,代码的学习,让学员掌握一个类似无人机,...

    275人学习 陈超
    免费试看

 

        NSRange rangeErr;

        rangeErr.location = 6;

        rangeErr.length = 2;

        NSString *strings = [value substringWithRange:rangeErr];

        int errCode = (int)strtoul([strings UTF8String],0,16);  //16进制字符串转换成int

        NSString *ascStr = [NSString stringWithFormat:@"%c",red];

转载于:https://www.cnblogs.com/ios988/p/5827021.html

2016-05-09 22:48:49 l2i2j2 阅读数 669
  • 03 v4l x264 alsa库

    通过本课程的学习,学员从TCP/IP UDP Socket基础概念,到网络编程接口介绍,POSIX线程封装,私有协议定义,开发,服务器模型,客户端编程等详细实战讲解。大量的实际项目,代码的学习,让学员掌握一个类似无人机,...

    275人学习 陈超
    免费试看
- (void)RGBWithHexColor:(int)hexColor {
    int red = ((hexColor & 0xFF0000) >> 16);
    int green = ((hexColor & 0xFF00) >> 8);
    int blue = (hexColor & 0xFF);
    NSLog(@"red:%d  green:%d  blue:%d", red, green, blue);
}

ios扩展16进制颜色

阅读数 8369