本文介绍了NSInteger的以8位二进制格式(字符串)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哈迪贾勒特昨天发布此code到A NSinteget转换为二进制,和完美的作品,但我需要在8位格式为:
Jarret Hardie (thanks !) post this code yesterday to convert a NSinteget to binary, and works perfectly, but i need in 8bit format:
4 - > 00000100
4 -> 00000100
任何想法修改此code?
any ideas to modify this code?
// Original author Adam Rosenfield... SO Question 655792
NSInteger theNumber = 56;
NSMutableString *str = [NSMutableString string];
for(NSInteger numberCopy = theNumber; numberCopy > 0; numberCopy >>= 1)
{
// Prepend "0" or "1", depending on the bit
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
NSLog(@"Binary version: %@", str);
感谢!!!!!!
Thanks !!!!!!
推荐答案
这应该工作:
NSInteger theNumber = 56;
NSMutableString *str = [NSMutableString string];
NSInteger numberCopy = theNumber; // so you won't change your original value
for(NSInteger i = 0; i < 8 ; i++) {
// Prepend "0" or "1", depending on the bit
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
numberCopy >>= 1;
}
NSLog(@"Binary version: %@", str);
这篇关于NSInteger的以8位二进制格式(字符串)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!