C语言中以二进制形式输出整数

来源:互联网 发布:财经类重要数据 编辑:程序博客网 时间:2024/05/19 00:37

(声明:此为原创,首发于http://blog.csdn.net,转载请注明 “作者:究竟实相,邮箱:LmjLmj8@163.com”。本文所给代码可未经本人允许而做任何使用,但是本人不承担因此造成的任何后果)

在C语言程序中,可以使用标准库函数中printf()来向屏幕输出信息,或者使用sprintf()向缓冲区输出信息。对整数而言,可以使用%d、%o、%x(或%X)输出十进制形式、八进制、十六进制形式,但貌似缺乏二进制形式,这不得不说是一种遗憾。因此,网上经常见到有人询问如何解决这个问题,解决方式有两种,一种是利用Windows提供的itoa()函数,另一种自行设计一个新的函数。笔者昨晚测试一个新的想法时,恰好遇到这个问题,因此就提供了一种自创实现方法,与大家共享。为了对比,也给出了itoa()的实现。所有代码在Visual Studio 2013 Express测试通过。由于编辑器出了问题,无法将用中文来表示代码注释,请原谅。限于笔者水平有限,这里给出的代码可能还有一些不足,请大家指正。


/** Test.h */

#pragma once


#ifndef Test_H
#define Test_H


/** use itoa() to print integers in the form of binary 
 *  @param[in] n the integer to print
 *  @return void
 */
void printBinaryByItoa(unsigned int n);


/** my function to print integers in the form of binary
 *  @param[in] n the integer to print
 *  @param[in] separator the separator to group binary bits. Each group has four bits. The common separators are ',' and ' '. If no need, set it 0
 *  @param[in] needPrefixZeros if true (any nonzero value), then prefix zeros will be printed out, else not
 *  @return void
*/
void printBinary(unsigned int n, unsigned char separator, unsigned char needPrefixZeros);


#endif  //end of Test_H


/** Test.c */

#include <stdio.h>
#include <stdlib.h>


void printBinaryByItoa(unsigned int n)
{
  unsigned char data[33];


  _itoa_s(n, data, 33, 2);
  
  printf("%sb\n", data);
}


void printBinary(unsigned int n, unsigned char separator, unsigned char needPrefixZeros)
{
  unsigned int i = 0;
  unsigned int mask = 0;    /* the mask code to get each bit of n */
  unsigned char data[40];   /* the buffer to store the bits of n in the form of characters */
  unsigned int index = 0;   /* used to access data[] */
  unsigned int start = 0;   /* to point out where and when to store bits of n */
  
  data[39] = '\0';


  if (n > 1)
  { /* when n == 1 or n == 0, it will be dealt with specially */


    if (needPrefixZeros)
    {
      start = 1;
    }


    /* n has 32 bits. The left bit (with index i = 0) is the highest bit */
    for (i = 0; i < 32; i++)
    {
      /* to get the i-th bit of n. It has two values, 0 or 1 */
      mask = 1 << (31 - i);
      mask = mask & n;
      mask = mask >> (31 - i);


      /* if needPrefixZeros is false, then, only when and after the first bit 1 is found, the bits of n are stored in data[] */
      if ((0 != mask) && (0 == start))
      {
        start = 1;
      }


      if (1 == start)
      {
        data[index++] = mask + '0';


        if ('\0' != separator && (i + 1) % 4 == 0)
        {
          data[index++] = separator;
        }
      }      
    }


    if ('\0' != separator)
    {
      data[index - 1] = '\0';
    }
    else
    {
      data[index] = '\0';
    }
  }
  else
  {
    data[0] = n + '0';
    data[1] = '\0';
  }


  printf("%sb\n", data);
}


/** Main.c */

#define _CRT_RAND_S


#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "Test.h"


int main(void)
{
  unsigned int n = 0;
  unsigned int count = 10;
  unsigned int i = 0;
  unsigned int numbers[10] = { 0xFFFFFFFF, 0xEABCDEF0, 0x81234567, 0x7100AB01, 0x456789, 0x100, 0x3, 0x2, 0x1, 0x0 };
  
  printf("=============test with special integers==============\n");


  for (i = 0; i < 10; i++)
  {
    printf("0X%X\n", numbers[i]);


    /* use itoa() to print binary bits of numbers[i] */
    printBinaryByItoa(numbers[i]);


    /* use my function to print binary bits of numbers[i] with no prefix zeros and no separator*/
    printBinary(numbers[i], 0, 0);


    /* use my function to print binary bits of numbers[i] with prefix zeros but no separator */
    printBinary(numbers[i], 0, 1);


    /* use my function to print binary bits of numbers[i] with separator ' ' bu no prefix zeros */
    printBinary(numbers[i], ' ', 0);


    /* use my function to print binary bits of numbers[i] with prefix zeros and separator ',' */
    printBinary(numbers[i], ',', 1);
    printf("\n");
  }


  printf("=============test with random integers==============\n");


  srand((unsigned int)time(NULL));


  for (i = 0; i < count; i++)
  {
    rand_s(&n);
    
    printf("0X%X\n", n);


    /* use itoa() to print binary bits of numbers[i] */
    printBinaryByItoa(n);


    /* use my function to print binary bits of numbers[i] with no prefix zeros and no separator*/
    printBinary(n, 0, 0);


    /* use my function to print binary bits of numbers[i] with prefix zeros but no separator */
    printBinary(n, 0, 1);


    /* use my function to print binary bits of numbers[i] with separator ' ' bu no prefix zeros */
    printBinary(n, ' ', 0);


    /* use my function to print binary bits of numbers[i] with prefix zeros and separator ',' */
    printBinary(n, ',', 1);
    printf("\n");
  }


  scanf_s("%u", &n);
  return 0;
}


/** 测试结果 */

=============test with special integers==============
0XFFFFFFFF
11111111111111111111111111111111b
11111111111111111111111111111111b
11111111111111111111111111111111b
1111 1111 1111 1111 1111 1111 1111 1111b
1111,1111,1111,1111,1111,1111,1111,1111b


0XEABCDEF0
11101010101111001101111011110000b
11101010101111001101111011110000b
11101010101111001101111011110000b
1110 1010 1011 1100 1101 1110 1111 0000b
1110,1010,1011,1100,1101,1110,1111,0000b


0X81234567
10000001001000110100010101100111b
10000001001000110100010101100111b
10000001001000110100010101100111b
1000 0001 0010 0011 0100 0101 0110 0111b
1000,0001,0010,0011,0100,0101,0110,0111b


0X7100AB01
1110001000000001010101100000001b
1110001000000001010101100000001b
01110001000000001010101100000001b
111 0001 0000 0000 1010 1011 0000 0001b
0111,0001,0000,0000,1010,1011,0000,0001b


0X456789
10001010110011110001001b
10001010110011110001001b
00000000010001010110011110001001b
100 0101 0110 0111 1000 1001b
0000,0000,0100,0101,0110,0111,1000,1001b


0X100
100000000b
100000000b
00000000000000000000000100000000b
1 0000 0000b
0000,0000,0000,0000,0000,0001,0000,0000b


0X3
11b
11b
00000000000000000000000000000011b
11b
0000,0000,0000,0000,0000,0000,0000,0011b


0X2
10b
10b
00000000000000000000000000000010b
10b
0000,0000,0000,0000,0000,0000,0000,0010b


0X1
1b
1b
1b
1b
1b


0X0
0b
0b
0b
0b
0b


=============test with random integers==============
0XA2862080
10100010100001100010000010000000b
10100010100001100010000010000000b
10100010100001100010000010000000b
1010 0010 1000 0110 0010 0000 1000 0000b
1010,0010,1000,0110,0010,0000,1000,0000b


0X53F5047D
1010011111101010000010001111101b
1010011111101010000010001111101b
01010011111101010000010001111101b
101 0011 1111 0101 0000 0100 0111 1101b
0101,0011,1111,0101,0000,0100,0111,1101b


0X80EC7FA9
10000000111011000111111110101001b
10000000111011000111111110101001b
10000000111011000111111110101001b
1000 0000 1110 1100 0111 1111 1010 1001b
1000,0000,1110,1100,0111,1111,1010,1001b


0XEEEE7F42
11101110111011100111111101000010b
11101110111011100111111101000010b
11101110111011100111111101000010b
1110 1110 1110 1110 0111 1111 0100 0010b
1110,1110,1110,1110,0111,1111,0100,0010b


0XC47AD5AD
11000100011110101101010110101101b
11000100011110101101010110101101b
11000100011110101101010110101101b
1100 0100 0111 1010 1101 0101 1010 1101b
1100,0100,0111,1010,1101,0101,1010,1101b


0X8B8CB6E7
10001011100011001011011011100111b
10001011100011001011011011100111b
10001011100011001011011011100111b
1000 1011 1000 1100 1011 0110 1110 0111b
1000,1011,1000,1100,1011,0110,1110,0111b


0X57F82C83
1010111111110000010110010000011b
1010111111110000010110010000011b
01010111111110000010110010000011b
101 0111 1111 1000 0010 1100 1000 0011b
0101,0111,1111,1000,0010,1100,1000,0011b


0X9FE8B07D
10011111111010001011000001111101b
10011111111010001011000001111101b
10011111111010001011000001111101b
1001 1111 1110 1000 1011 0000 0111 1101b
1001,1111,1110,1000,1011,0000,0111,1101b


0X50DAC96D
1010000110110101100100101101101b
1010000110110101100100101101101b
01010000110110101100100101101101b
101 0000 1101 1010 1100 1001 0110 1101b
0101,0000,1101,1010,1100,1001,0110,1101b


0X6E432E0
110111001000011001011100000b
110111001000011001011100000b
00000110111001000011001011100000b
110 1110 0100 0011 0010 1110 0000b
0000,0110,1110,0100,0011,0010,1110,0000b

0 0
原创粉丝点击