Hex to Bin

J

Josh Parker

What would be a good way to convert a Hex number to a Binary number.
I know all numbers stored in variables are stored as binary anyway but
how would i go about bring this out. I will be writing this info to a
pipe, which another process will then pick it up and write it to a
file.

Anyone got any advice?
 
M

Mike Wahler

Josh Parker said:
What would be a good way to convert a Hex number to a Binary number.
I know all numbers stored in variables are stored as binary anyway but
how would i go about bring this out. I will be writing this info to a
pipe, which another process will then pick it up and write it to a
file.

Anyone got any advice?


Roll your own function. Here's one of mine:

std::string bin(unsigned int i)
{
std::string result;
while(i)
{
result.insert(result.begin(), i % 2 + '0');
i /= 2;
}
return result;
}


-Mike
 
J

Julie J.

Are you converting it to a string representation? If not, then there is no
need to do any conversion (unless you run into endian issues).

Presuming that you want to convert it to a string representation:

Check your compiler & C library documentation. Some C libraries support
something to the effect of itoa (or _itoa) which has a radix (or base). Use 2
to convert a number to a binary string. However, itoa (et al.) are *not* ANSI
standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top