1. ## How can I convert char value to hexadecimal

I'm trying to find the hexadecimal value of the base 16 ip address.
For example I used this code to input base 16 IP address:
Code:
```char ipc11,ipc12,ipc21,ipc22,ipc31,ipc32,ipc41,ipc42;
scanf(" %c%c.%c%c.%c%c.%c%c",&ipc11,&ipc12,&ipc21,&ipc22,&ipc31,&ipc32,&ipc41,&ipc42);```
and I want to display an error if the hexadecimal value of the IP address is above FF (255).
Any helps are appreciated.

2. EDIT:You can't use strings,%x command and data structures.

3. Originally Posted by kayra09
I'm trying to find the hexadecimal value of the base 16 ip address.
What exactly is "the hexadecimal value of the base 16 ip address"? Given an IPv4 address in hex format, are you trying to compute its integer value?

If so, then this really is just a matter of a mathematical formula. You need to map each char to its integer value, and once you have that you can just multiply and add accordingly.

4. When I use their ASCII values I get the wrong hexadecimal value.For example: ASCII value of 'A' is 65 when I put 65 to converter I get 41 but hex value of 'A' is 10.

5. Originally Posted by kayra09
When I used their ASCII values I get the wrong hexadecimal value.For example: ASCII value of 'A' is 65 when I put 65 to converter I get 41 but hex value of 'A' is 10.
That's why I said that "You need to map each char to its integer value". Obviously, you aren't going to map each char to its actual integer value in the character set, but to its integer value as interpreted as a hex digit. I would suggest a switch as you can use the default case for the "display an error if the hexadecimal value of the IP address is above FF" requirement.

EDIT:
RULES:
You are not allowed to use data structures such as arrays to store values for theconversion operation.
You are not allowed to define your own functions.

It can still be done, but your instructor is a poor instructor by forbidding you to use tools that are designed for cases like this. This is a pretty good exercise to teach you about looping over arrays or defining your own functions, so I'm afraid your instructor is just bad.

6. This may help:

Consider the ASCII table for the characters from '0' to 'F', where '0' is 0x30:

0123456789:;<=>?@ABCDEF

Did you notice there are 7 chars between '9' and 'A'?
Then, to convert 4 bits to hex is a matter of:

Code:
```x = x and 0xf; // this is valid for 4 bits!
if x > 9 then x = x + 7;
print '0'+x;```
Now you can build your routine, in C, without using arrays or data structures to convert an unsigned char to hex string.