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 char to int and int to char

Converting char to int and int to char

Scheduled Pinned Locked Moved C / C++ / MFC
6 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.
  • S Offline
    S Offline
    sacoskun
    wrote on last edited by
    #1

    Hi, I would like to convert a char into an int and vice versa. For instance; char a = '3'; int a = 3; Thanks in advance,

    M Mircea PuiuM M 3 Replies Last reply
    0
    • S sacoskun

      Hi, I would like to convert a char into an int and vice versa. For instance; char a = '3'; int a = 3; Thanks in advance,

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      See the FAQ 6.3 How can I change a number into its string representation, or vice versa?[^] --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb

      1 Reply Last reply
      0
      • S sacoskun

        Hi, I would like to convert a char into an int and vice versa. For instance; char a = '3'; int a = 3; Thanks in advance,

        Mircea PuiuM Offline
        Mircea PuiuM Offline
        Mircea Puiu
        wrote on last edited by
        #3

        One way would be: ============================ char chA = '3'; int nA; char chValue[32]; // actually 2 it's enough here :-)) chValue[0] = chA; chValue[1] = 0; // null terminated string nA = atoi(chValue); ============================ char chA; int nA = 3; char chValue[32]; itoa(nA, chValue, 10); chA = chValue[0]; ============================ SkyWalker

        S 2 Replies Last reply
        0
        • Mircea PuiuM Mircea Puiu

          One way would be: ============================ char chA = '3'; int nA; char chValue[32]; // actually 2 it's enough here :-)) chValue[0] = chA; chValue[1] = 0; // null terminated string nA = atoi(chValue); ============================ char chA; int nA = 3; char chValue[32]; itoa(nA, chValue, 10); chA = chValue[0]; ============================ SkyWalker

          S Offline
          S Offline
          sacoskun
          wrote on last edited by
          #4

          Hi Mircea, I used both the array and * implementations and it worked, thank you very much... Thanks to Dunn also.

          1 Reply Last reply
          0
          • Mircea PuiuM Mircea Puiu

            One way would be: ============================ char chA = '3'; int nA; char chValue[32]; // actually 2 it's enough here :-)) chValue[0] = chA; chValue[1] = 0; // null terminated string nA = atoi(chValue); ============================ char chA; int nA = 3; char chValue[32]; itoa(nA, chValue, 10); chA = chValue[0]; ============================ SkyWalker

            S Offline
            S Offline
            sacoskun
            wrote on last edited by
            #5

            I also would like to ask that is it possible to store for example number 14 in one character array element? I mean; if( myCharArray[2] == 14 ) cout << "I want this" << endl; Thanks,

            1 Reply Last reply
            0
            • S sacoskun

              Hi, I would like to convert a char into an int and vice versa. For instance; char a = '3'; int a = 3; Thanks in advance,

              M Offline
              M Offline
              mirex
              wrote on last edited by
              #6

              if number is only one digit then its simple. char c = '3'; // char => int int i = c - '0'; // int => char c = '0' + i;

              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