Is there any utility or library provides a simple function to convert a hex to binary and then depending on bit set assigning decimal value ?
This will convert hex to binary but i want to manipulate each bit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int getBit(int n, int k)
{
return (n & (1<<k)) != 0;
}
int main()
{
int n = 0x05;
for (int k = 3; k>=0; k--)
cout << getBit(n,k);
cout << endl;
return 0;
}
This will convert 0x05 = 0101 . After this how to check each bit and if it is set means depending on position assign decimal value.
Say from left to right first bit: 0=0x01;
2nd bit: 1=0x02;
3rd bit: 0=0x03;
4th bit: 1=0x04;