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. present int as char

present int as char

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 4 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.
  • A Offline
    A Offline
    Aljaz111
    wrote on last edited by
    #1

    I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks

    E _ G 3 Replies Last reply
    0
    • A Aljaz111

      I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      It could be:

      int i(65);
      // only the low byte will be accepted
      char c = char(i);

      ...or even:

      int i(65);
      // both bytes will be accepted
      WCHAR wc = WCHAR(i);

      :)

      Check your definition of Irrationality[^] :) 1 - Avicenna 5 - Hubbard 3 - Own definition

      1 Reply Last reply
      0
      • A Aljaz111

        I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        In addition to what Eugen said, you can do this -

        char buffer[30];
        sprintf_s(buffer, 30, "%c", int_value);

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        1 Reply Last reply
        0
        • A Aljaz111

          I am figuring out how to present single chars from integer value, that can be only from 65-122(ASCII). I randomly get int value and now i would like to change that int to char, so lets say for int value 65 output would be 'A'. With itoa(int value, buffer, 10) where buffer is char*[30] i get result from buffer in numbers. What can be used to convert this? Thanks

          G Offline
          G Offline
          Gwenio
          wrote on last edited by
          #4

          Casting would be the simple way of doing it. In C, casting looks like this:

          (char)x

          In C++, you can do the above or use static_cast which is the preferred method:

          static_cast<char>(x)

          This is uglier and takes more text, but it is safer (more type checking at compile time) and it is good to make a habit of it. Once you cast the int to a char, it will be treated as a char for all operations. Of course, it will be converted implicitly when assigning it to a char and casting will not be needed.

          modified on Thursday, March 4, 2010 10:51 PM

          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