我想将NSString
转换为6位二进制文件。
例如:
输入:
NSString = @"A";
输出:
010001
答案 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];
}