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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. converting char to unsigned int

converting char to unsigned int

Scheduled Pinned Locked Moved C / C++ / MFC
question
19 Posts 12 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.
  • T thepersonof

    Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #2

    thepersonof wrote:

    char c = 250;

    thepersonof wrote:

    How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way?

    unsigned char c = 250; Signed char limit is 127.


    Nibu thomas Software Developer

    1 Reply Last reply
    0
    • T thepersonof

      Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

      stefanmihaimogaS Offline
      stefanmihaimogaS Offline
      stefanmihaimoga
      wrote on last edited by
      #3

      thepersonof wrote:

      How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way?

      char c = 250; int a = (int)(unsigned int)c;

      1 Reply Last reply
      0
      • T thepersonof

        Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

        C Offline
        C Offline
        color Aljechin
        wrote on last edited by
        #4

        thepersonof wrote:

        char c = 250; int a = int(c)

        char c = 250; unsigned int a = (int)c; ??

        1 Reply Last reply
        0
        • T thepersonof

          Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

          K Offline
          K Offline
          kakan
          wrote on last edited by
          #5

          Make c an unsigned char. (Yes, there are such a thing as signed and unsigned chars in C). Or cast c to an unsigned char. So: 1. unsigned char c = 250; Or: 2. int a = (int) (unsigned char) c; Or both... -- modified at 7:49 Thursday 6th April, 2006 OR: int a = 250; :laugh:

          1 Reply Last reply
          0
          • T thepersonof

            Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

            A Offline
            A Offline
            Aqueel
            wrote on last edited by
            #6

            do you want the value of a to be 250 in this case? We Believe in Excellence www.aqueelmirza.cjb.net

            T 1 Reply Last reply
            0
            • A Aqueel

              do you want the value of a to be 250 in this case? We Believe in Excellence www.aqueelmirza.cjb.net

              T Offline
              T Offline
              thepersonof
              wrote on last edited by
              #7

              Thankyou ... converted to an unsigned char and it works great

              A 1 Reply Last reply
              0
              • T thepersonof

                Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

                W Offline
                W Offline
                Wim Engberts
                wrote on last edited by
                #8

                union { char c[sizeof (int)]; int n; } Convert; char c = 250; Convert.n = 0; Convert.c[0] = c; Then, in Convert.n you will get the proper value. Good luck! William

                C A 2 Replies Last reply
                0
                • T thepersonof

                  Thankyou ... converted to an unsigned char and it works great

                  A Offline
                  A Offline
                  Aqueel
                  wrote on last edited by
                  #9

                  Yah i misunderstood. They are right. We Believe in Excellence www.aqueelmirza.cjb.net

                  1 Reply Last reply
                  0
                  • W Wim Engberts

                    union { char c[sizeof (int)]; int n; } Convert; char c = 250; Convert.n = 0; Convert.c[0] = c; Then, in Convert.n you will get the proper value. Good luck! William

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

                    Geez :omg: That's a really complicated way of doing that !

                    1 Reply Last reply
                    0
                    • W Wim Engberts

                      union { char c[sizeof (int)]; int n; } Convert; char c = 250; Convert.n = 0; Convert.c[0] = c; Then, in Convert.n you will get the proper value. Good luck! William

                      A Offline
                      A Offline
                      Aqueel
                      wrote on last edited by
                      #11

                      Chaa gia hai bhai! We Believe in Excellence www.aqueelmirza.cjb.net

                      T 1 Reply Last reply
                      0
                      • A Aqueel

                        Chaa gia hai bhai! We Believe in Excellence www.aqueelmirza.cjb.net

                        T Offline
                        T Offline
                        thatsme_cool
                        wrote on last edited by
                        #12

                        Kya cha gaya hai Yar

                        A 1 Reply Last reply
                        0
                        • T thatsme_cool

                          Kya cha gaya hai Yar

                          A Offline
                          A Offline
                          Aqueel
                          wrote on last edited by
                          #13

                          hahahaha What a solution yaar! Nice Unique and innovative :-D We Believe in Excellence www.aqueelmirza.cjb.net

                          T 1 Reply Last reply
                          0
                          • A Aqueel

                            hahahaha What a solution yaar! Nice Unique and innovative :-D We Believe in Excellence www.aqueelmirza.cjb.net

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #14

                            ...and you said what ? :confused:

                            A 1 Reply Last reply
                            0
                            • T toxcct

                              ...and you said what ? :confused:

                              A Offline
                              A Offline
                              Aqueel
                              wrote on last edited by
                              #15

                              i said "Chaa gia hai bhai" which means "Great job! brother!". This is Urdu language idiom. It is used for a person who does something great. :) We Believe in Excellence www.aqueelmirza.cjb.net

                              1 Reply Last reply
                              0
                              • T thepersonof

                                Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

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

                                thepersonof wrote:

                                int a = int(c)

                                A cast is actually not necessary. You could accomplish the same with:

                                int a = c;

                                because a char gets internally promoted to an int during such operations. A char can hold values in the range -128 to 127, whereas an unsigned char can hold values in the range 0 to 255.


                                "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                "There is no death, only a change of worlds." - Native American Proverb

                                1 Reply Last reply
                                0
                                • T thepersonof

                                  Hi I found that char could be converted to an interger as follows: char c = 250; int a = int(c) How can I get the a value of a to be the same as what I put in? It sometimes comes out negative, although I can add 256. There is a simpler way? thank you

                                  A Offline
                                  A Offline
                                  abbiyr
                                  wrote on last edited by
                                  #17

                                  Hi there. As the other replies have stated, you need to use the unsigned char for values above 127. To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts. unsigned char c = 250; int a = static_cast<int>(c); Cheers

                                  D 1 Reply Last reply
                                  0
                                  • A abbiyr

                                    Hi there. As the other replies have stated, you need to use the unsigned char for values above 127. To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts. unsigned char c = 250; int a = static_cast<int>(c); Cheers

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

                                    abbiyr wrote:

                                    To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts.

                                    For integral types, this is not necessary.


                                    "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                    "There is no death, only a change of worlds." - Native American Proverb

                                    A 1 Reply Last reply
                                    0
                                    • D David Crow

                                      abbiyr wrote:

                                      To perform explicit conversions in C++, you should use the static_cast instead of the old C-Style casts.

                                      For integral types, this is not necessary.


                                      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                                      "There is no death, only a change of worlds." - Native American Proverb

                                      A Offline
                                      A Offline
                                      abbiyr
                                      wrote on last edited by
                                      #19

                                      Quite agree. It is not necessary, the unsigned char will be cast correctly without it. I recommended it though, as it does explicity show the intentions of the developer.

                                      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