10进制转string ios

2014-02-18 13:59:02 CHENPOLU 阅读数 4702

           今天项目有一个需求是这样的。

服务器端传一个这样格式的字符串:aabbcc 到客户端用来代表RGB。

如:a3b8e5 即表示:RGB    red:163 green:184 blue:229.

那么怎样从 @“a3b8e5”中得到上面的结果?

有一个非常有用的函数:strtoul


       int   red = strtoul([[@“a3b8e5” substringWithRange:NSMakeRange(0, 2)] UTF8String],0,16);

        int   green = strtoul([[@“a3b8e5” substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16);

        int  blue = strtoul([[@“a3b8e5” substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16);


      OK,这样就解决了我们的问题。



2016-07-05 10:49:44 wujianjun6932 阅读数 1869

    //10进制转16进制字符串

    NSString *hexString= [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1lx",20]];

    

2015-06-27 07:59:10 hhxxhbmy 阅读数 3570

// TransformNumber.h文件
// 将字符串数字转换为2,8,16进制
#import <Foundation/Foundation.h>
@interface TransformNumber : NSObject
/**
 *  返回转换后的值,只能转换2,8,16进制
 *
 *  @para str 需要转换的值
 *
 *  @para sys 需要转换的进制
 */
+(NSString *)transformNumber:(NSString *)str withNumberSystem:(NSString *)sys;
@end


 //TransformNumber.m文件
#import "TransformNumber.h"

@implementation TransformNumber

+(NSString *)transformNumber:(NSString *)str withNumberSystem:(NSString *)sys
{
    NSMutableString *mstring = [NSMutableString stringWithFormat:@"X"];
    NSString *bitString = [NSString stringWithFormat:@"0123456789ABCDEF"];
    longlong int tmp = [str intValue],num = [sys intValue], p, a, b;
    if(num ==2)
    {
        a = 1;
        b = 1;
    }elseif (num == 8)
    {
        a = 7;
        b = 3;
    }elseif (num == 16)
    {
        a = 15;
        b = 4;
    }else
    {
        NSLog(@"您输入的进制错误!请输入2,8,16进制!");
        returnnil;
    }
    while(tmp!=0)
    {
        p=tmp&a;
        NSString *str1=[NSString stringWithFormat:@"%c",[bitString characterAtIndex:p]];
        [mstring insertString:str1 atIndex:0];
        tmp=tmp>>b;
    }

    return [mstring substringWithRange:NSMakeRange(0, [mstring length]-1)];
}


@end


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;

}

2016-08-25 14:10:49 vickyjfr 阅读数 450




 NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];