如何从整数打印二进制文件

时间:2016-02-16 03:37:04

标签: c

NSString *TS = [prefs objectForKey:@"Run"];
NSString *TS1 = [prefs objectForKey:@"Run1"];
NSString *TS2 = [prefs objectForKey:@"Run2"];
NSString *TS3 = [prefs objectForKey:@"Run3"];
NSString *TS4 = [prefs objectForKey:@"Run4"];
NSString *TS5 = [prefs objectForKey:@"Run5"];
NSString *TS6 = [prefs objectForKey:@"Run6"];
NSString *TS7 = [prefs objectForKey:@"Run7"];
NSString *TS8 = [prefs objectForKey:@"Run8"];
NSString *TS9 = [prefs objectForKey:@"Run9"];
NSString *TS10 = [prefs objectForKey:@"Run10"];
NSString *TS11 = [prefs objectForKey:@"Run11"];
NSString *TS12 = [prefs objectForKey:@"Run12"];
NSString *TS13 = [prefs objectForKey:@"Run13"];
NSString *TS14 = [prefs objectForKey:@"Run14"];
NSString *TS15 = [prefs objectForKey:@"Run15"];
NSString *TS16 = [prefs objectForKey:@"Run16"];
NSString *TS17 = [prefs objectForKey:@"Run17"];
//   NSString *TS18 = [prefs objectForKey:@"Run18"];


if  ([TS   isEqual: @"NotRunning"] | [TS1   isEqual: @"NotRunning"] |[TS2   isEqual: @"NotRunning"] |[TS3   isEqual: @"NotRunning"] | [TS4   isEqual: @"NotRunning"] |[TS5   isEqual: @"NotRunning"] | [TS6   isEqual: @"NotRunning"] | [TS7   isEqual: @"NotRunning"] | [TS8   isEqual: @"NotRunning"] | [TS9   isEqual: @"NotRunning"] | [TS10   isEqual: @"NotRunning"] |[TS11   isEqual: @"NotRunning"] | [TS12   isEqual: @"NotRunning"] | [TS13   isEqual: @"NotRunning"] |[TS14   isEqual: @"NotRunning"]|[TS15   isEqual: @"NotRunning"]|[TS16   isEqual: @"NotRunning"]|[TS17   isEqual: @"NotRunning"])
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Would you like to clear everything?",@"") message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"NO",@"") otherButtonTitles:NSLocalizedString(@"YES",@""), nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    alert.tag = 99;

    [alert show];
}

它没有打印出正确的二进制数,但是有适当的位数,以及如何解决这个问题的建议?

3 个答案:

答案 0 :(得分:2)

您的问题出在printBin

num<<1;

这句话什么都不做。你想要的是:

num<<=1;

为清楚起见,您还应在此处使用括号:

int mask = 1 << (bits - 1);

您应该将printBin移到main之上,以便在调用该函数时可以看到该定义。

最后,main应始终返回int

答案 1 :(得分:2)

您的代码中存在以下几个问题:

  1. 您需要printBin()的原型。
  2. 使用int main(void)int main(int argc, char** argv)代替void main()
  3. num<<1;是一个没有副作用的表达式。也许你可以从编译器的警告中推断出这一点。写num<<=1;以发挥作用。
  4. 只有一个建议:为什么不用printf("%d", !!(mask & num));替换if语句?
  5. 这是使用clang编译而没有警告的固定代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    void printBin(int num, int bits);
    
    int main(void)
    {
            int testNums[] = {3, 0x12, 0xFF, -3};
            int testBits[] = {9, 7, 12, 15};
            int i;
    
            for (i = 0; i < sizeof testNums / sizeof testNums[0]; i++)
                    printBin(testNums[i], testBits[i]);
            return 0;
    }
    
    void printBin(int num, int bits)
    {
            int pow;
            int mask = 1 << (bits - 1);
    
            for(pow=0; pow<bits; pow++)
            {
                    printf("%d", mask & num);
                    num <<= 1;
            }
            putchar('\n');
    }
    

    输出:

    000000011
    0010010
    000011111111
    111111111111101
    

答案 2 :(得分:0)

虽然你的方法是传统的,@ dbush的答案是合适的,但我想提一下另一种方法。您可能知道这一点并且没有非常好地处理负值但是在这种方法中,您可以将任何碱基的数量很容易地转换为其他

#include <stdio.h>
#include <string.h>
#define RESOLUTION 128

void compute(int number, int base, char* placeHolder){
    int k = 0;
    int i, j;
    char temp;
    while(number){
        placeHolder[k++] = "0123456789ABCDEF"[number % base];
        number /= base;
    }
    if (k != 0) {
      placeHolder[k] = '\0';
      for(i = 0, j = strlen(placeHolder) - 1; i < j; i++, j--){
        temp = placeHolder[i];
        placeHolder[i] = placeHolder[j];
        placeHolder[j] = temp;
      }
    }else{
        placeHolder[0] = '0';
        placeHolder[1] = '\0';
    }
}


int main(void) {
    char bin[RESOLUTION];
    compute(0xFF, 10, bin);
    printf("%s\n", bin);
    return 0;
}

随时更改RESOLUTION值。

更具体二元,您可以打印任何数据类型的二进制文件

void printBits(size_t const size, void const * const ptr)
{
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i, j;

    for (i=size-1;i>=0;i--)
    {
        for (j=7;j>=0;j--)
        {
            byte = b[i] & (1<<j);
            byte >>= j;
            printf("%u", byte);
        }
    }
    puts("");
}


int main()
{
        int i = 23;
        uint ui = UINT_MAX;
        float f = 23.45f;
        printBits(sizeof(i), &i);
        printBits(sizeof(ui), &ui);
        printBits(sizeof(f), &f);
        return 0;
}