convert alpha to hex

This is a discussion on convert alpha to hex within the C Programming forums, part of the General Programming Boards category; How do you convert the ASCII characters to their hexadecimal values? Such as 'A' is equal 65. ssprint and sscanf ...

  1. #1
    mofojordan
    Guest

    convert alpha to hex

    How do you convert the ASCII characters to their hexadecimal values?

    Such as 'A' is equal 65.

    ssprint and sscanf only can convert ints to hexdecimals. And atoi is not a standard C function.

    I'm stuck.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Cast the char to an int before using sprintf()
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    int main (void)
    {
       char ch = 'A';
       printf("ch = '%c'\n", ch);
       printf("ch = %d\n",   (int)ch);
       printf("ch = 0x%x\n", (unsigned int)ch);
       printf("ch = 0%o\n",  (unsigned int)ch);
       return (0);
    }
    /* my output
    ch = 'A'
    ch = 65
    ch = 0x41
    ch = 0101
    */

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >ssprint and sscanf only can convert ints to hexdecimals.
    A char is an integral value, in fact, char and int are often used interchangeably in C. Just use the %x format for sprintf and cast the char as unsigned.

    >And atoi is not a standard C function.
    Yes it is. I believe you are thinking of itoa, which is not a standard function.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 12:59 PM
  2. Replies: 23
    Last Post: 07-09-2007, 05:49 AM
  3. function to convert hex to decimal
    By Moony in forum C Programming
    Replies: 5
    Last Post: 07-11-2006, 04:14 AM
  4. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  5. Utility to Convert hex to binary
    By learnC in forum C Programming
    Replies: 3
    Last Post: 10-20-2005, 01:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21