Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. converting a "character" into hexadecimal

converting a "character" into hexadecimal

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rajeev82
    wrote on last edited by
    #1

    hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)

    C D Z B 4 Replies Last reply
    0
    • R rajeev82

      hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      rajeev82 wrote:

      in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent??

      Hexadecimal equivalent is not something very clear. What do you want to do ? Print this value in a string (using its hexa representation) ? If yes, you can use printf with the 'x' tag:

      char szString[50];
      char Temp = 'A';
      sprintf(szString,"%x",Temp);

      Because a number is neither hexa, decimal nor binary, it's just its representation that can change. EDIT: changed printf to sprintf, thanks David :)


      Cédric Moonen Software developer
      Charting control

      1 Reply Last reply
      0
      • R rajeev82

        hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        rajeev82 wrote:

        ...are there to convert this "single character" into its ASCII hexadecimal eqivalent??

        For what purpose? Usually when someone talks of converting from base-10 to base-16, it is for display purposes. When a number is stored, its base is irrelevant. In other words, 123, 0x7b, and 1111011 all represent the same number.


        "The largest fire starts but with the smallest spark." - David Crow

        "Judge not by the eye but by the heart." - Native American Proverb

        1 Reply Last reply
        0
        • R rajeev82

          hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          I was about to ask if you are working for the company I use to work for (the only one I know that uses RS-232 and transmits data at twice its size), but then I saw you are in India ... Anyway, to answer your question: To convert a number to a text-readable ASCII-Hex representation (that is, each half of a byte is represented by a single hexadecimal character [0-9aA-fF]), use the printf family: char buffer[100] = {0}; BYTE myInfo = 64; sprintf(buffer, "%X", myInfo); To convert an ASCII-Hex number back to a byte, use sscanf: char* buffer = "AB"; BYTE myInfo = 0; sscanf(buffer, "%2.2X", &MyInfo); If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          1 Reply Last reply
          0
          • R rajeev82

            hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)

            B Offline
            B Offline
            basementman
            wrote on last edited by
            #5

            Perhaps this helps?

            BYTE HexToByte(LPCSTR cpHex)
            {
            BYTE cRetVal;

            if (*cpHex < 58)
            cRetVal = *cpHex - '0';
            else
            cRetVal = *cpHex - '7';

            cRetVal <<= 4;

            cpHex++;

            if (*cpHex < 58)
            cRetVal += *cpHex - '0';
            else
            cRetVal += *cpHex - '7';

            return cRetVal;
            }

            onwards and upwards...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups