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. how to change a char to an int

how to change a char to an int

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 3 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.
  • H Offline
    H Offline
    hearties
    wrote on last edited by
    #1

    I want to extract the first char from a string and test if it is equal to an int in a for loop. char c; for (int i=1, i

    C M 2 Replies Last reply
    0
    • H hearties

      I want to extract the first char from a string and test if it is equal to an int in a for loop. char c; for (int i=1, i

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

      cast it. Christian I've learned that you cannot make someone love you. All you can do is stalk them and hope they panic and give in. The early bird may get the worm, but it's the second mouse that gets the cheese.

      H 1 Reply Last reply
      0
      • C Christian Graus

        cast it. Christian I've learned that you cannot make someone love you. All you can do is stalk them and hope they panic and give in. The early bird may get the worm, but it's the second mouse that gets the cheese.

        H Offline
        H Offline
        hearties
        wrote on last edited by
        #3

        how to ? tried this for ((int)c == i) it still doesnt work....:((

        C 1 Reply Last reply
        0
        • H hearties

          how to ? tried this for ((int)c == i) it still doesnt work....:((

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

          Oh, I see. The code snippet you posted was broken, so I didn't spot exactly what you wanted to do. I just compiled this no worries: char c = 'c'; for (int i = 0; i < 255;i++) { if ( i == (int)c) break; } for (int j = 0; j < i; j++) printf("c"); so I'd suggest you create a new int, and make it equal to (int) c, then make a loop of it. If ( as I assume, otherwise there would be no point ) you want to output this character, you need merely say c = z ( assuming z is the (int) of c to start with in the loop ) and it should work. You MAY need to make c a char * with two elements to ensure element 2 is a 0 though. Christian I've learned that you cannot make someone love you. All you can do is stalk them and hope they panic and give in. The early bird may get the worm, but it's the second mouse that gets the cheese.

          1 Reply Last reply
          0
          • H hearties

            I want to extract the first char from a string and test if it is equal to an int in a for loop. char c; for (int i=1, i

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

            You don't need to do anything. The char will be implicitly converted to an int (look up "integral promotion" in your help docs) to do the comparison. --Mike-- http://home.inreach.com/mdunn/ Tables, ladders, chairs, OH MY!

            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