Hi I need to convert a hex number to binary number in C++. It represents a 4 by 8 binary image. For example, the following hex number 0xc00e90 represent the image:
00000000
11000000
00001110
10010000
Here’s my working solution. u is the input hex #, and image is the bool array [4][8]. But I’m pretty sure this is not the optimal solution, what would be the better way to implement this?
for(int i=0;i<4;i++)
{
std::bitset<8> bit((u>>(3-i)*8)&0xFF);
cout<<bit<<"\n";
for (int j=0;j<8;j++)
{
image[i][j]=(bool) bit[7-j];
}
}
Actually
uis not a hex number, it is just a number without a base until you represent them with some digits of some base. Your solution is fine, another way of doing with without creating a bitset: