# Thread: More hex and binary conversions

1. ## More hex and binary conversions

CGIASM again - if you haven't heard the whole story - nevermind, it's not relevant.

Lets say I need to enter 11001101 into a file. If I used the conventional way, I'd get the ascii character representing a 1 and a 0. I need it in the actually binary, so that it should actually represent ascii 205, or "-" Now once I can convert it to actuall binary, I'm OK. But how can I work with binary numbers in C? I'll need to declare them, initilaize them, add to them using a literal, and then convert them to decimal ascii numbers/characters. How do I do this?

2. What do you mean with binary numbers? This way you can declare a byte.

unsigned char byte;

And I guess this way you print the ASCII value of 205.

byte = 205;
printf ("%c\n", byte);

3. It does not matter whether it is a char, int, long, pointer, etc. It is _all_ binary.

*Stop* thinking about these elements as if they are different types of data-- they are not. they are all binary in different bit-widths.

The easiest way to work with binary in C, is to work with it in its hex form. that is because hex characters break down to 4-bit nybles. They are recognized by the compiler as numbers, so you don't have to play silly string games with them.

You can shift and mask them, and perform math on them to get what you want done.

---

Let me ask this critical question-- is it difficult, or awkward for you to convert between hex and binary representations? Perhaps there are some mechanics I could explain that will make working with decimal/hex/binary conversion trivial for you.

4. Even easier is to make use of bitfields.

5. In response to * - Duh. I know. I wanted to know how to enter a number in the binary number system, and trhen have it stored as decimal format.

6. Use strtoul() or strtol().
Code:
```#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char s[33] = "11111111";
unsigned long num;
char *endptr;

num = strtoul(s,&endptr,2);
printf("num:%08lX\n",num);
return 0;
}```