• iOS -- 十进制、十六进制字符串,byte,data等之间的转换


    十进制->十六进制
    Byte bytes[]={0xA6,0x27,0x0A};
    NSString *strIdL  = [NSStringstringWithFormat:@"%@",[[NSStringalloc]initWithFormat:@"%02lx",(long)bytes[0]]];
    十六进制->十进制
    NSString *rechargeInfo =  @"0xff055008";
    NSString *cardId2 = [rechargeInfosubstringWithRange:NSMakeRange(2,2)];
    cardId2 = [NSStringstringWithFormat:@"%ld",strtoul([cardId2UTF8String],0,16)];
    
    
    NSString
     *str = @"0xff055008";
    
    //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型
    
    unsignedlongred
     = strtoul([str UTF8String],0,16);
    
    //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六进制的,这样写也可以:
    
    unsignedlongred
     = strtoul([@"0x6587"UTF8String],0,0);
    
    NSLog(@"转换完的数字为:%lx",red);
    #pragma mark --字符串处理方法
    //字符串处理方法
    +(NSString *)cutString:(NSString *)str{
        NSString *targetStr=nil;
        if (str.length>9) {
            targetStr=[str substringFromIndex:9];
            
        }
        return targetStr;
    }
    +(NSString *)transToStr:(NSData *)data{
        
        NSString *str=[NSStringstringWithFormat:@"%@",data];
        return str;
    }
    
    //字符串补零操作
    +(NSString *)addZero:(NSString *)str withLength:(int)length{
        NSString *string = nil;
        if (str.length==length) {
            return str;
        }
        if (str.length<length) {
            NSUInteger inter = length-str.length;
            for (int i=0;i< inter; i++) {
                string = [NSString stringWithFormat:@"0%@",str];
                str = string;
            }
        }
        return string;
    }
    
    +(NSString *)stringFromHexString:(NSString *)hexString {//
        
        char *myBuffer = (char *)malloc((int)[hexStringlength] / 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 = [[NSScanneralloc] initWithString:hexCharStr] ;
            [scanner scanHexInt:&anInt];
            myBuffer[i / 2] = (char)anInt;
            NSLog(@"myBuffer is %c",myBuffer[i /2] );
        }
        NSString *unicodeString = [NSStringstringWithCString:myBuffer encoding:4];
        NSLog(@"———字符串=======%@",unicodeString);
        return unicodeString; 
    }
    
    //10进制转16进制
    +(NSString *)ToHex:(longlong int)tmpid
    {
        NSString *nLetterValue;
        NSString *str =@"";
        long longint ttmpig;
        for (int i =0; i<9; i++) {
            ttmpig=tmpid%16;
            tmpid=tmpid/16;
            switch (ttmpig)
            {
                case 10:
                    nLetterValue =@"A";break;
                case 11:
                    nLetterValue =@"B";break;
                case 12:
                    nLetterValue =@"C";break;
                case 13:
                    nLetterValue =@"D";break;
                case 14:
                    nLetterValue =@"E";break;
                case 15:
                    nLetterValue =@"F";break;
                default:nLetterValue=[[NSStringalloc]initWithFormat:@"%lli",ttmpig];
                    
            }
            str = [nLetterValue stringByAppendingString:str];
            if (tmpid == 0) {
                break;
            }
            
        }
        return str;
    }
    
    //将16进制的字符串转换成NSData
    + (NSMutableData *)convertHexStrToData:(NSString *)str {
        if (!str || [str length] == 0) {
            return nil;
        }
        
        NSMutableData *hexData = [[NSMutableDataalloc] initWithCapacity:8];
        NSRange range;
        if ([str length] %2 == 0) {
            range = NSMakeRange(0,2);
        } else {
            range = NSMakeRange(0,1);
        }
        for (NSInteger i = range.location; i < [strlength]; i += 2) {
            unsigned int anInt;
            NSString *hexCharStr = [str substringWithRange:range];
            NSScanner *scanner = [[NSScanneralloc] initWithString:hexCharStr];
            
            [scanner scanHexInt:&anInt];
            NSData *entity = [[NSDataalloc] initWithBytes:&anIntlength:1];
            [hexData appendData:entity];
            
            range.location += range.length;
            range.length = 2;
        }
      
        return hexData;
    }
    
    + (NSString *)convertDataToHexStr:(NSData *)data {
        if (!data || [data length] == 0) {
            return @"";
        }
        NSMutableString *string = [[NSMutableStringalloc] initWithCapacity:[datalength]];
        
        [data enumerateByteRangesUsingBlock:^(constvoid *bytes, NSRange byteRange,BOOL *stop) {
            unsigned char *dataBytes = (unsignedchar*)bytes;
            for (NSInteger i =0; i < byteRange.length; i++) {
                NSString *hexStr = [NSStringstringWithFormat:@"%x", (dataBytes[i]) &0xff];
                if ([hexStr length] == 2) {
                    [string appendString:hexStr];
                } else {
                    [string appendFormat:@"0%@", hexStr];
                }
            }
        }];
        
        return string;
    }
    //十进制准换为十六进制字符串
    + (NSString *)hexStringFromString:(NSString *)string{
        NSData *myD = [stringdataUsingEncoding:NSUTF8StringEncoding];
        Byte *bytes = (Byte *)[myDbytes];
        //下面是Byte转换为16进制。
        NSString *hexStr=@"";
        for(int i=0;i<[myDlength];i++)
            
        {
            NSString *newHexStr = [NSStringstringWithFormat:@"%x",bytes[i]&0xff];///16进制数
            
            if([newHexStr length]==1)
                
                hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
            
            else
                
                hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; 
        } 
        return hexStr; 
    }
    +(NSString *) parseByteArray2HexString:(Byte[]) bytes
    {
        NSMutableString *hexStr = [[NSMutableString alloc]init];
        int i = 0;
        if(bytes)
        {
            while (bytes[i] != '')
            {
                NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数
                if([hexByte length]==1)
                    [hexStr appendFormat:@"0%@", hexByte];
                else
                    [hexStr appendFormat:@"%@", hexByte];
                
                i++;
            }
        }
        NSLog(@"bytes 的16进制数为:%@",hexStr);
        return hexStr;
    }
  • 相关阅读:
    InnoDB的数据页结构
    InnoDB的行溢出数据,Char的行结构存储
    InnoDB的行记录格式, Compact, Redundant, Compressed, Dynamic
    python py_innodb_page_info.py -v /usr/local/var/mysql/ibdata1
    InnoDB的表类型,逻辑存储结构,物理存储结构
    InnoDB存储引擎的表空间文件,重做日志文件
    MYSQL数据库的套接字文件,pid文件,表结构文件
    MYSQL数据库的日志文件
    MYSQL数据库的参数文件
    InnoDB的启动,关闭,恢复
  • 原文地址:https://www.cnblogs.com/mafeng/p/6672168.html
Copyright © 2020-2023  润新知