本文介绍了如何转换NSInteger的二进制(字符串)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何一个NSInteger转换,说56岁,以一个NSString,它是原始(INT)值的二进制重新presentation。也许有人知道格式化技术,可以接受56和目标C.感谢所有。

I am trying to figure out how to convert an NSInteger, say 56, to an NSString that is a binary representation of the original (int) value. Perhaps someone knows a formatting technique that can accept 56 and return "111000" within Objective C. Thanks All.

推荐答案

有没有内置的格式化操作人员做到这一点。如果你想将其转换为十六进制字符串,你可以这样做:

There's no built-in formatting operator to do that. If you wanted to convert it to a hexadecimal string, you could do:

NSString *str = [NSString stringWithFormat:@"%x", theNumber];

要其转换为二进制字符串,你必须建立它自己:

To convert it to a binary string, you'll have to build it yourself:

NSMutableString *str = [NSMutableString stringWithFormat:@""];
for(NSInteger numberCopy = theNumber; numberCopy > 0; numberCopy >>= 1)
{
    // Prepend "0" or "1", depending on the bit
    [str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}

这篇关于如何转换NSInteger的二进制(字符串)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:50
查看更多