将NSString转换为6位二进制表示

时间:2012-03-03 06:36:54

标签: iphone objective-c ios xcode binary

我想将NSString转换为6位二进制文​​件。

例如:

输入:

NSString = @"A";

输出:

010001

1 个答案:

答案 0 :(得分:3)

-(void)hexadecimal_change:(NSString *)string{
NSLog(@"hexa");

NSString *hex = string;
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:@"%@",[self toBinary:hexAsInt]];
NSLog(@"%@",binary);

long v = strtol([binary1 UTF8String], NULL, 2);
NSString *dec=[NSString stringWithFormat:@"%ld",v];
NSLog(@"%@",dec); 



}



 -(NSString *)toBinary:(NSUInteger)input{
    if (input == 1 || input == 0)
        return [NSString stringWithFormat:@"%u", input];
    return [NSString stringWithFormat:@"%@%u", [self toBinary:input / 2], input % 2];
}