Thread: How can I convert char value to hexadecimal

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    11

    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;
    printf("Please enter the IP Address:");
     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. #2
    Registered User
    Join Date
    Mar 2019
    Posts
    11
    EDIT:You can't use strings,%x command and data structures.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote 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.
    Last edited by laserlight; 03-17-2019 at 07:40 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    11
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote 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:
    Actually, I realised you posted about this in another thread, and there was this requirement:
    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.
    Last edited by laserlight; 03-17-2019 at 08:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert Binary to Hexadecimal
    By dnguyen8 in forum C Programming
    Replies: 3
    Last Post: 10-11-2013, 09:28 PM
  2. how to convert a binary table to hexadecimal value
    By thefirstone92 in forum C Programming
    Replies: 12
    Last Post: 10-03-2013, 10:56 AM
  3. trying to convert hexadecimal to decimal
    By chrissy2860 in forum C Programming
    Replies: 7
    Last Post: 01-23-2012, 04:11 PM
  4. convert string to hexadecimal
    By nocturna_gr in forum C Programming
    Replies: 3
    Last Post: 12-11-2007, 04:45 AM
  5. ASCII convert to Hexadecimal value?
    By ashish.malviya in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:45 AM

Tags for this Thread