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#
  4. how do u convert a char to an int?

how do u convert a char to an int?

Scheduled Pinned Locked Moved C#
questionc++
4 Posts 4 Posters 6 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:

    C X C 3 Replies Last reply
    0
    • L Lost User

      hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      This is the C# forum, not the C++. You should try and ask questions in the right place. A char *is* an int, you can do this

      char a = 65; // = 'A'
      int i = 'A'; // i = 65
      int z = 5;
      z += a; // z = 70

      Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001

      1 Reply Last reply
      0
      • L Lost User

        hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:

        X Offline
        X Offline
        Xian
        wrote on last edited by
        #3

        Ok, it depends on what you want to do though. Are you talking about having a character such as '4' which you want the value 4? Or are you talking about what the Graus-man said and want the ASCII value of the character? Here is an example:

        #include <stdio.h>
        #include <stdlib.h>

        void main( void )
        {
        char c = 'A';
        char d = '4';
        int i = 0;

        printf( "Character '%c' is %d in ascii.\n", c, c );
        printf( "Character '%c' is %d in ascii.\n", d, d );
        printf( "Character '%c' is %d as an integer.\n", d, atoi( &d ) );
        }

        That yields the output :

        Character 'A' is 65 in ascii.
        Character '4' is 52 in ascii.
        Character '4' is 4 as an integer.

        I hope that helps you out! ---- Xian - www.hollowmedia.net

        1 Reply Last reply
        0
        • L Lost User

          hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:

          C Offline
          C Offline
          Carlos Antollini
          wrote on last edited by
          #4

          If is a char to int use that Christian Graus said but if String to int, you only need to use the atoi function... Cheers Carlos Antollini.

          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