Wednesday 20 March 2013

Convert Bits value of a variable(any type) to Hex

Program is written in the purpose of Converting Bits value of a variable to Hex

#include <iostream>

using namespace std;

void bitDump(void* address, unsigned int size);

int main(){
  long double ld = 12345.123456;
  bitDump(&ld, sizeof(ld)); // print the bit pattern (cool if formatted)
  return 0;
}
//Loop through size and convert each char to 8 binary digits.
void bitDump(void* var, unsigned int size){
    char* addres = (char*) var;
    int count = 1;
    for(int i = 0; i < sizeof(size); i++){
        unsigned int mask = 1 << 7;
        while(m){//convert each char to 8 binary digits
            printf("%d", !!(addres[i] & mask));
            m = m >> 1;
           
            if(count == 4)
            {
                putchar(' ');
                cnt = 1;
            }
            else
            {
                count++;
            }
    }
    putchar('\n');
}

No comments:

Post a Comment