2017-06-13 20 views
-4

我想獲得一個二進制表示在C中的所有類型的變量:intunsigned intlongunsigned longshortunsigned shortfloatdoublechar獲取的變量二進制表示用C

這是最好的解決方案,當我得到一個變量的大小(由sizeof())並將其轉換爲二進制系統?

如何快速,輕鬆地進行這樣的轉換?

+4

https://stackoverflow.com/questions/111928/is -there-a-printf-converter-to-print-in-binary-format –

+3

它們已經是二進制的。 –

+0

你想要所有這些類型的二進制轉換函數嗎?或sizeof()這些類型?請詳細說明您需要的信息 –

回答

0

得到任何類型的二進制表示:

我寫了下面的代碼提供你問什麼兩種功能。第一個函數getBinaryHostMemoryRepresentation()將填充您提供的buffer中的位如何將它們保存在主機內存中:這可能是小端,大端或其他(不太可能)。例如整數0xABCD被保存在一個小尾數爲0xCDAB

getBinaryRepresentation()函數將檢查您正在運行的機器,並將您的buffer填充到您期望的位代碼中,因爲人們可以讀取這是大端的,因此您會得到0xABCD。要檢查程序運行的是哪種機器,請使用isLittleEndian()函數,看看它是如何工作的here on SO

這兩個函數都需要以下參數:char * const buffer, size_t bufSize, const void * var, size_t varSize。前兩個是緩衝區和緩衝區的大小。最後兩個是你想要轉換的變量(它是一個空指針,它可以是任何數據)以及可以通過sizeof()得到的變量的字節大小。此外,函數檢查緩衝區是否足夠大,如果不是,則返回空指針。如果足夠大,他們會將指針返回到您提供的緩衝區。用法可以在main()函數中觀察到。

main()函數使用以上TEH功能兩個輸入:

  1. 整數:2882400235其爲0xABCDEFEB0b10101011110011011110111111101011
  2. 浮子:42.0其是0x422800000b01000010001010000000000000000000

要檢查整數,您可以使用Windows計算器來檢查花車,您可以使用this site online

輸出的程序:

integer 2882400235: 
Host memory binary representation: "11101011111011111100110110101011" 
Human readable binary representation: "10101011110011011110111111101011" 

single floating point 42.0: 
Host memory binary representation: "00000000000000000010100001000010" 
Human readable binary representation: "01000010001010000000000000000000" 

爲您標記爲C的問題在C中的代碼:

#include <stdio.h> 
#include <stddef.h> 
#include <limits.h> 

#if CHAR_BIT != 8 
#error "unsupported char size" 
#endif 

int isLittleEndian() 
{ 
    static const int num = 1; 
    if (1 == *((char *) &num)) 
    { 
     return 1; 
    } 
    else 
    { 
     return 0; 
    } 
} 

char * getBinaryHostMemoryRepresentation (char * const buffer, size_t bufSize, const void * var, size_t varSize) 
{ 
    size_t byteIdx; 
    size_t bitIdx; 

    if (bufSize < varSize * CHAR_BIT + 1) 
    { 
     return NULL; 
    } 
    const unsigned char * curByte = (const unsigned char *) var; 
    for (byteIdx = 0; byteIdx < varSize; ++byteIdx, ++curByte) 
    { 
     for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx) 
     { 
     unsigned char curBit = (*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx); 
     buffer[byteIdx * CHAR_BIT + bitIdx] = curBit + '0'; 
     } 
    } 
    buffer[varSize * CHAR_BIT] = '\0'; 
    return buffer; 
} 

char * getBinaryRepresentation (char * const buffer, size_t bufSize, const void * var, size_t varSize) 
{ 
    size_t byteIdx; 
    size_t bitIdx; 

    if (bufSize < varSize * CHAR_BIT + 1) 
    { 
     return NULL; 
    } 

    const unsigned char * curByte;; 
    int incByte; 
    if (isLittleEndian()) 
    { 
     curByte = (const unsigned char *) var + (varSize - 1); 
     incByte = -1; 
    } 
    else 
    { 
     curByte = (const unsigned char *) var; 
     incByte = 1; 
    } 
    for (byteIdx = 0; byteIdx < varSize; ++byteIdx, curByte += incByte) 
    { 
     for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx) 
     { 
     unsigned char curBit = (*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx); 
     buffer[byteIdx * CHAR_BIT + bitIdx] = curBit + '0'; 
     } 
    } 
    buffer[varSize * CHAR_BIT] = '\0'; 
    return buffer; 
} 

int main() 
{ 
    int integer = 2882400235; /* 10101011110011011110111111101011 */ 
    char bufferMemInt[sizeof (integer) * CHAR_BIT + 1]; 
    char bufferBinInt[sizeof (integer) * CHAR_BIT + 1]; 

    printf ("integer 2882400235:\n"); 
    if (getBinaryHostMemoryRepresentation (bufferMemInt, 
              sizeof (bufferMemInt), 
              (void *) &integer, 
              sizeof (integer))) 
    { 
     printf ("Host memory binary representation: \"%s\"", 
       bufferMemInt); 
     printf ("\n"); 
    } 

    if (getBinaryRepresentation (bufferBinInt, 
           sizeof (bufferBinInt), 
           (void *) &integer, 
           sizeof (integer))) 
    { 
     printf ("Human readable binary representation: \"%s\"", 
       bufferBinInt); 
     printf ("\n"); 
    } 


    float floating = 42.0; /* 01000010001010000000000000000000 */ 
    char bufferMemFloat[sizeof (floating) * CHAR_BIT + 1]; 
    char bufferBinFloat[sizeof (floating) * CHAR_BIT + 1]; 

    printf ("\n"); 
    printf ("single floating point 42.0:\n"); 
    if (getBinaryHostMemoryRepresentation (bufferMemFloat, 
              sizeof (bufferMemFloat), 
              (void *) &floating, 
              sizeof (floating))) 
    { 
     printf ("Host memory binary representation: \"%s\"", 
       bufferMemFloat); 
     printf ("\n"); 
    } 

    if (getBinaryRepresentation (bufferBinFloat, 
           sizeof (bufferBinFloat), 
           (void *) &floating, 
           sizeof (floating))) 
    { 
     printf ("Human readable binary representation: \"%s\"", 
       bufferBinFloat); 
     printf ("\n"); 
    } 

    return 0; 
} 
+0

嗨@geekcode如果這個答案解決了你的問題,請點擊[接受它](https://meta.stackexchange.com/q/5234/179419)複選標記。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 –