被二进制位转换困扰
我发现并修改了一个 int 到 bin 转换器,它似乎工作正常。然后我组装了一个 bin 到 int 转换器,但结果表明我向后读取二进制文件。我反转了 for 循环的方向: for(i = 11; i >=0; i--),但得到了相同的结果。
@implementation MainViewController
- (void)intToBin:(int)theNumber
{
NSMutableString *str = [NSMutableString string];
NSInteger numberCopy = theNumber;
for(NSInteger i = 0; i <= 11 ; i++)
{
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
numberCopy >>= 1;
}
NSLog(@"Binary version: %@", str);
}
- (void)binToInt:(NSString *)theBinary
{
int decNumber = 0;
int i;
for(i = 0; i <=11; i++) // then tried: for(i = 11; i >=0; i--)
{
NSString *digitChar = [theBinary substringWithRange: NSMakeRange (i, 1)];
int digitNum = [digitChar intValue];
NSLog(@"digitNum: %d", digitNum);
if(digitNum == 1) decNumber += digitNum * pow(2,i);
}
NSLog(@"Decimal version: %d", decNumber);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self intToBin:3434];
[self intToBin:3418];
[self intToBin:2906];
[self binToInt:@"110101101010"];
[self binToInt:@"110101011010"];
[self binToInt:@"101101011010"];
}
日志详细信息:
[Session started at 2011-03-23 22:05:30 -0500.]
Binary version: 110101101010
Binary version: 110101011010
Binary version: 101101011010
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
Decimal version: 1387
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
Decimal version: 1451
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
Decimal version: 1453
[Session started at 2011-03-23 22:06:46 -0500.]
Binary version: 110101101010
Binary version: 110101011010
Binary version: 101101011010
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
Decimal version: 1387
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
Decimal version: 1451
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
Decimal version: 1453
我错过了什么?有没有更好的方法来做到这一点而不使用十六进制?或者与...
提前感谢您的帮助。 国会议员
I found and modified an int-to-bin converter that seems to be working right. Then I put together a bin-to-int converter but the result shows that im reading the binary backwards. I reversed the direction of the for loop: for(i = 11; i >=0; i--), but got the same result.
@implementation MainViewController
- (void)intToBin:(int)theNumber
{
NSMutableString *str = [NSMutableString string];
NSInteger numberCopy = theNumber;
for(NSInteger i = 0; i <= 11 ; i++)
{
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
numberCopy >>= 1;
}
NSLog(@"Binary version: %@", str);
}
- (void)binToInt:(NSString *)theBinary
{
int decNumber = 0;
int i;
for(i = 0; i <=11; i++) // then tried: for(i = 11; i >=0; i--)
{
NSString *digitChar = [theBinary substringWithRange: NSMakeRange (i, 1)];
int digitNum = [digitChar intValue];
NSLog(@"digitNum: %d", digitNum);
if(digitNum == 1) decNumber += digitNum * pow(2,i);
}
NSLog(@"Decimal version: %d", decNumber);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self intToBin:3434];
[self intToBin:3418];
[self intToBin:2906];
[self binToInt:@"110101101010"];
[self binToInt:@"110101011010"];
[self binToInt:@"101101011010"];
}
Log Details:
[Session started at 2011-03-23 22:05:30 -0500.]
Binary version: 110101101010
Binary version: 110101011010
Binary version: 101101011010
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
Decimal version: 1387
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
Decimal version: 1451
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
Decimal version: 1453
[Session started at 2011-03-23 22:06:46 -0500.]
Binary version: 110101101010
Binary version: 110101011010
Binary version: 101101011010
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
Decimal version: 1387
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
Decimal version: 1451
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 0
digitNum: 1
digitNum: 1
digitNum: 0
digitNum: 1
Decimal version: 1453
What did I miss? Is there a better way to do this without using hex? Or with...
Thanks in advance for the assist.
MP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您是从 0 到 11 还是从 11 到 0 迭代字符串,
decNumber += digitalNum * pow(2,i)
都会根据字符索引设置位。快速解决方法是使用pow(2,11-i)
代替,因此字符串中的位置 11 得到 2**0,位置 10 得到 2**1,依此类推。不过,更好的方法是这样的:
当它处理字符串中的每个数字时,
(decNumber << 1)
将所有先前处理的位移动 1,并且<代码>| (digitNum == 1 ? 1 : 0) 根据刚刚读取的字符将数字中的最低位设置为 0 或 1。它当然可以进一步改进,例如通过更有效地解析字符串,但我将把它作为读者的练习。Whether you iterate the string from 0 to 11 or 11 to 0,
decNumber += digitNum * pow(2,i)
sets the bit based on the character index. The quick fix is to usepow(2,11-i)
instead, so position 11 in the string gets 2**0, position 10 gets 2**1, and so on.A better way to do it, though, would be like this:
As it processes each digit in the string, the
(decNumber << 1)
shifts all the previously-handled bits over by 1 and the| (digitNum == 1 ? 1 : 0)
sets the lowest bit in the number to 0 or 1 based on the character just read. It could certainly be improved further, for example by more efficiently parsing the string, but I'll leave that as an exercise for the reader.您使用字符串来表示数字,这可能不是解决您想要做的事情的好方法。但是,为了回答你的问题,你将错误的索引提高到 2 次方。无论你以哪种方式计算循环,你仍然乘以相同的指数。当您开始读取字符串时,第一个数字将是最重要的。 来解决此问题
pow
)您的固定循环可能看起来像(警告,未经测试):
仅供参考,此代码往好里说是不必要的,往坏处说是不清楚的。您还可以通过简单的移位和加法来完成上述乘法+加法。
You're using strings to represent numbers, that's probably not a good way to solve what you're trying to do. But however, to answer your question, you're raising the wrong index to the power of 2. Regardless in which way you're counting your loop, you're still multiplying by the same indeces. As you start reading the string, the first digit will be most significant. You can solve this problem by
pow
)Your fixed loop can look like (warning, untested):
FYI, this code is unnecessary at best and unclear at worst. You can also accomplish the above multiply+add with a simple shift and add.