如何将Hex转换为二进制iphone [英] How to convert Hex to Binary iphone

查看:156
本文介绍了如何将Hex转换为二进制iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在objective-c中将十六进制字符串转换为二进制形式,有人可以指导我吗?
例如,如果我有一个十六进制字符串7fefff78,我想将其转换为1111111111011111111111101111000?

I need to convert a hex string to binary form in objective-c, Could someone please guide me? For example if i have a hex string 7fefff78, i want to convert it to 1111111111011111111111101111000?

BR,
Suppi

BR, Suppi

推荐答案

漂亮的递归解决方案......

Nice recursive solution...

NSString *hex = @"49cf3e";
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:@"%@", [self toBinary:hexAsInt]];

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

这篇关于如何将Hex转换为二进制iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆