10进制转成2进制 ios

2016-12-27 15:58:01 u011146511 阅读数 1832

////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);



2016-05-09 22:48:49 l2i2j2 阅读数 669
- (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);
}

2015-12-29 16:23:29 u013428812 阅读数 1719

最近自己也在做蓝牙开发,发现好多做蓝牙开发的都没有接触过C ,字节转换更是晕乎乎的,所以写下此篇博客,提供我转换的方法。

一、整形装换成两个字节

    

    int value = 456;

    unsigned char  byte1 = (value & 0xff00)>>8; //8

    unsigned char  byte2 = (value & 0xff);//8

    

    NSLog(@"byte1= %x   byte2= %x ",byte1,byte2);

  

        byte1 = 1;  byte2= c8;

二、两个字节转换成十进制整数

    char bytes[]={byte1,byte2};

    unsigned char  by1 = (bytes[0] & 0xff); //8

    unsigned char  by2 = (bytes[1] & 0xff);//8

    int   temp  = (by2|(by1<<8));

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

    

    temp = 456;

三、整形装换成三个字节

    int value =134456;

    unsigned char  byte1 = (value & 0xff0000)>>16;//最高8

    unsigned char  byte2 = (value & 0xff00)>>8;//中间8

    unsigned char  byte3 = (value & 0xff);//8

    

    NSLog(@"byte1= %x   byte2= %x byte3= %x ",byte1,byte2,byte3);

    byte1= 2   byte2= d byte3= 38


四、三个字节转换成十进制整数

    char bytes[]={byte1,byte2,byte3};

    unsigned char  by1 = (bytes[0] & 0xff); //8

    unsigned char  by2 = (bytes[1] & 0xff);//8

    unsigned char  by3 = (bytes[2] & 0xff);//8

    

    int   temp  = (by3|(by2<<8)|(by1<<16));

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

   temp = 134456

如果还有更多的自己接需要转换以此类推


2016-10-01 15:53:44 u011774517 阅读数 1553

//将十进制转化为十六进制
- (NSString *)ToHex:(uint16_t)tmpid
{
NSString *nLetterValue;
NSString *str =@”“;
uint16_t 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 = [NSString stringWithFormat:@”%u”,ttmpig];

    }
    str = [nLetterValue stringByAppendingString:str];
    if (tmpid == 0) {
        break;
    }

}
return str;}

//十进制转二进制
- (NSString *)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)]];
}

return result;
}

2017-10-23 20:29:02 SharkToping 阅读数 468

/**

 十进制正数转二进制

  @param number 十进制正数

 

  @return 二进制字符串

 

  */

- (NSString *)binaryNumberStringFromIntegerNumber:(NSUInteger )number{

    NSMutableString *resultStr = [NSMutableString string];

    if (number == 1 || number == 0) {

        return [NSString stringWithFormat:@"%ld",number];

    }

    //递归、拼接

    while (number/2) {

        NSUInteger singleNumber = number % 2;

        NSString *singleStr = [NSString stringWithFormat:@"%ld",singleNumber];

        [resultStr insertString:singleStr atIndex:0];

        number = number / 2;

        //最后一位需判断、其实就是最高位1

        if (number / 2 == 0) {

            NSString *devStr = [NSString stringWithFormat:@"%ld",number];

            [resultStr insertString:devStr atIndex:0];

        }

    }

    return resultStr;

}

iOS开发进制转换

阅读数 67