被二进制位转换困扰

发布于 2024-10-25 13:23:43 字数 2592 浏览 2 评论 0原文

我发现并修改了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟火散人牵绊 2024-11-01 13:23:43

无论您是从 0 到 11 还是从 11 到 0 迭代字符串,decNumber += digitalNum * pow(2,i) 都会根据字符索引设置位。快速解决方法是使用 pow(2,11-i) 代替,因此字符串中的位置 11 得到 2**0,位置 10 得到 2**1,依此类推。

不过,更好的方法是这样的:

int decNumber = 0;
int i; 
for(i = 0; i <=11; i++)
{
    NSString *digitChar = [theBinary substringWithRange: NSMakeRange (i, 1)];
    int digitNum = [digitChar intValue];
    NSLog(@"digitNum: %d", digitNum);
    decNumber = (decNumber << 1) | (digitNum == 1 ? 1 : 0);
}

当它处理字符串中的每个数字时,(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 use pow(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:

int decNumber = 0;
int i; 
for(i = 0; i <=11; i++)
{
    NSString *digitChar = [theBinary substringWithRange: NSMakeRange (i, 1)];
    int digitNum = [digitChar intValue];
    NSLog(@"digitNum: %d", digitNum);
    decNumber = (decNumber << 1) | (digitNum == 1 ? 1 : 0);
}

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.

我为君王 2024-11-01 13:23:43

您使用字符串来表示数字,这可能不是解决您想要做的事情的好方法。但是,为了回答你的问题,你将错误的索引提高到 2 次方。无论你以哪种方式计算循环,你仍然乘以相同的指数。当您开始读取字符串时,第一个数字将是最重要的。 来解决此问题

  • 您可以通过首先反转二进制字符串或
  • 使用替代策略来获取十进制数 。 (即使用乘法和加法与使用pow

您的固定循环可能看起来像(警告,未经测试):

...
for(i = 0; i <=11; i++)   // then tried: for(i = 11; i >=0; i--)
{
    int digitNum = [[theBinary substringWithRange: NSMakeRange (i, 1)] intValue];
    NSLog(@"digitNum: %d", digitNum);
    decNumber = decNumber * 2 + digitNum;
}
...

仅供参考,此代码往好里说是不必要的,往坏处说是不清楚的。您还可以通过简单的移位和加法来完成上述乘法+加法。

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

  • first reversing the binary string, or
  • using an alternate strategy to get at your decimal number. (i.e using multiplies and adds versus using pow)

Your fixed loop can look like (warning, untested):

...
for(i = 0; i <=11; i++)   // then tried: for(i = 11; i >=0; i--)
{
    int digitNum = [[theBinary substringWithRange: NSMakeRange (i, 1)] intValue];
    NSLog(@"digitNum: %d", digitNum);
    decNumber = decNumber * 2 + digitNum;
}
...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文