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. hex converting

hex converting

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
22 Posts 6 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.
  • N Offline
    N Offline
    NewHSKid
    wrote on last edited by
    #1

    Hi everyone, First off i would like to say this site is great, especially for a 15 year old that is trying to learn VC++ on his own. I want to send a hex number to the screen, but don't know how. What data type should it be, and how would i represent a hex number. For example I want to send something like 5FF00231 which would be an 4 byte number. I don't know how though, would it be an int, a string? Please help...thank you so much

    B D L 3 Replies Last reply
    0
    • N NewHSKid

      Hi everyone, First off i would like to say this site is great, especially for a 15 year old that is trying to learn VC++ on his own. I want to send a hex number to the screen, but don't know how. What data type should it be, and how would i represent a hex number. For example I want to send something like 5FF00231 which would be an 4 byte number. I don't know how though, would it be an int, a string? Please help...thank you so much

      B Offline
      B Offline
      Brian Delahunty
      wrote on last edited by
      #2

      Do you just want to display "5FF00231" on the screen??? If so just send it as a normal string. Regards, Brian Dela :-) IE 6 required: http://www.briandela.com[^]
      It works now.

      1 Reply Last reply
      0
      • N NewHSKid

        Hi everyone, First off i would like to say this site is great, especially for a 15 year old that is trying to learn VC++ on his own. I want to send a hex number to the screen, but don't know how. What data type should it be, and how would i represent a hex number. For example I want to send something like 5FF00231 which would be an 4 byte number. I don't know how though, would it be an int, a string? Please help...thank you so much

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

        So is your question about converting base X numbers to base 16, or displaying text to the screen? If the former, try: int number = 1609564721; char str[16]; sprintf(str, "%#x", number); If the latter, try: printf("%s\n", str); or cout << str << endl;

        N 1 Reply Last reply
        0
        • D David Crow

          So is your question about converting base X numbers to base 16, or displaying text to the screen? If the former, try: int number = 1609564721; char str[16]; sprintf(str, "%#x", number); If the latter, try: printf("%s\n", str); or cout << str << endl;

          N Offline
          N Offline
          NewHSKid
          wrote on last edited by
          #4

          My friends dad gave me a program to write cause he said it will make me learn. I have to connect to a server and send it 4 byte messages that have the form of 5FF00239. The message changes though, because the window (dialog i think its called) has 2 check boxes. If only the first check box is checked then the message should be 5FF00239 and if the second check box is only check it should be 5FF0023A. But if both are checked then he wants the message to be 5FF0023B. How do I make these messages? I don't know what type they should be. I can do everything else he wants except i don't know how to do the message. I thought it would be a string, but he says if it is, then the message is 8 bytes instead of 4? Please help me...

          D K 2 Replies Last reply
          0
          • N NewHSKid

            Hi everyone, First off i would like to say this site is great, especially for a 15 year old that is trying to learn VC++ on his own. I want to send a hex number to the screen, but don't know how. What data type should it be, and how would i represent a hex number. For example I want to send something like 5FF00231 which would be an 4 byte number. I don't know how though, would it be an int, a string? Please help...thank you so much

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hex numbers are usually preceded by the characters 0x to indicate the following four byte code is a hex value and not an integer. So, the end message should read something like 0x5FF00231. I would use an integer (because hex numbers are generally used to display a 32-bit integer unsigned or signed in Base 16 format - remember that, especially if you want to get into the games industry :)). So my code in WinMain would look like : int WinMain(blah...) { int myhex = 0x5FF00231; char message[32]; sprintf(&message[0], "0x%x", myhex); ::MessageBox(NULL, message, message, MB_OK); } } Hope that helps you :)

            L N 2 Replies Last reply
            0
            • L Lost User

              Hex numbers are usually preceded by the characters 0x to indicate the following four byte code is a hex value and not an integer. So, the end message should read something like 0x5FF00231. I would use an integer (because hex numbers are generally used to display a 32-bit integer unsigned or signed in Base 16 format - remember that, especially if you want to get into the games industry :)). So my code in WinMain would look like : int WinMain(blah...) { int myhex = 0x5FF00231; char message[32]; sprintf(&message[0], "0x%x", myhex); ::MessageBox(NULL, message, message, MB_OK); } } Hope that helps you :)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              to be absolutely correct you could use the \0 null terminator to end the string. I always use at least a 32 byte string cos memory byte placements like multiples of 32 :) to avoid memory padding. sprintf(&message[0], "0x%x\0", myhex);

              1 Reply Last reply
              0
              • L Lost User

                Hex numbers are usually preceded by the characters 0x to indicate the following four byte code is a hex value and not an integer. So, the end message should read something like 0x5FF00231. I would use an integer (because hex numbers are generally used to display a 32-bit integer unsigned or signed in Base 16 format - remember that, especially if you want to get into the games industry :)). So my code in WinMain would look like : int WinMain(blah...) { int myhex = 0x5FF00231; char message[32]; sprintf(&message[0], "0x%x", myhex); ::MessageBox(NULL, message, message, MB_OK); } } Hope that helps you :)

                N Offline
                N Offline
                NewHSKid
                wrote on last edited by
                #7

                That is what I want to do later on, I want to program video games. :-) Can you explain what the char message[32]does?

                D 1 Reply Last reply
                0
                • N NewHSKid

                  My friends dad gave me a program to write cause he said it will make me learn. I have to connect to a server and send it 4 byte messages that have the form of 5FF00239. The message changes though, because the window (dialog i think its called) has 2 check boxes. If only the first check box is checked then the message should be 5FF00239 and if the second check box is only check it should be 5FF0023A. But if both are checked then he wants the message to be 5FF0023B. How do I make these messages? I don't know what type they should be. I can do everything else he wants except i don't know how to do the message. I thought it would be a string, but he says if it is, then the message is 8 bytes instead of 4? Please help me...

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

                  Ok, numbers are treated differently when sending them versus displaying them. When you talk about a "hex number," that is for display purposes only. In other words, 1609564729 and 0x5FF00239 are treated identically by the computer. It stores them as binary regardless of how you use them. Now if you are sending "1609564729" and "0x5FF00239" (notice the quotes) to some other process/computer, then they are two totally different things. So, whether you send 1609564729 or 0x5FF00239 to the server, it makes no difference. What mechanism are you using to send data to the server?

                  N 1 Reply Last reply
                  0
                  • D David Crow

                    Ok, numbers are treated differently when sending them versus displaying them. When you talk about a "hex number," that is for display purposes only. In other words, 1609564729 and 0x5FF00239 are treated identically by the computer. It stores them as binary regardless of how you use them. Now if you are sending "1609564729" and "0x5FF00239" (notice the quotes) to some other process/computer, then they are two totally different things. So, whether you send 1609564729 or 0x5FF00239 to the server, it makes no difference. What mechanism are you using to send data to the server?

                    N Offline
                    N Offline
                    NewHSKid
                    wrote on last edited by
                    #9

                    Hi, thanks for helping me. mechanism? I want to use the int send command. Is that what you are talking about. So you are saying that I can use a int variable, but just put 0x in front of the number?

                    D 1 Reply Last reply
                    0
                    • N NewHSKid

                      That is what I want to do later on, I want to program video games. :-) Can you explain what the char message[32]does?

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

                      It reserves a spot (named "message") in memory for 32 characters.

                      N 1 Reply Last reply
                      0
                      • D David Crow

                        It reserves a spot (named "message") in memory for 32 characters.

                        N Offline
                        N Offline
                        NewHSKid
                        wrote on last edited by
                        #11

                        but I only need 4 bytes? should i change the 32 to 4 then? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

                        D T 2 Replies Last reply
                        0
                        • N NewHSKid

                          Hi, thanks for helping me. mechanism? I want to use the int send command. Is that what you are talking about. So you are saying that I can use a int variable, but just put 0x in front of the number?

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

                          Bingo!! These three code snippets are the same: int num = 123456789; // base-10 SendToServer(num); int num = 0x75BCD15; // base-16 SendToServer(num); int num = 0726746425; // base-8 SendToServer(num);

                          N 1 Reply Last reply
                          0
                          • N NewHSKid

                            but I only need 4 bytes? should i change the 32 to 4 then? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

                            You don't need this at all. Dangleberry's post was just showing you how to convert a number to a string (which was then displayed in a message box).

                            1 Reply Last reply
                            0
                            • D David Crow

                              Bingo!! These three code snippets are the same: int num = 123456789; // base-10 SendToServer(num); int num = 0x75BCD15; // base-16 SendToServer(num); int num = 0726746425; // base-8 SendToServer(num);

                              N Offline
                              N Offline
                              NewHSKid
                              wrote on last edited by
                              #14

                              For the send command, i have problems it needs the socket and a message and a length, and some flags. Its says I can't use int because the message has to be const char *. for the length is that in bytes? should it be 4 or 10 to represent all numbers/letters? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

                              D 1 Reply Last reply
                              0
                              • N NewHSKid

                                For the send command, i have problems it needs the socket and a message and a length, and some flags. Its says I can't use int because the message has to be const char *. for the length is that in bytes? should it be 4 or 10 to represent all numbers/letters? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

                                How about a code snippet of what you are trying to do?

                                N 1 Reply Last reply
                                0
                                • D David Crow

                                  How about a code snippet of what you are trying to do?

                                  N Offline
                                  N Offline
                                  NewHSKid
                                  wrote on last edited by
                                  #16

                                  hi, here is what i want to do, the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) int message; message = 0x5FF0023A; send(s, message, strlen(message), 0); It says send can't convert parameter 2 from int to const char * and it says the same thing about strlen. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

                                  D L 2 Replies Last reply
                                  0
                                  • N NewHSKid

                                    hi, here is what i want to do, the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) int message; message = 0x5FF0023A; send(s, message, strlen(message), 0); It says send can't convert parameter 2 from int to const char * and it says the same thing about strlen. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

                                    NewHSKid wrote: the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) Does that mean he is supplying you with a valid socket descriptor? Given that the second parameter of send() is expecting a char*, you'll need:

                                    int nMessage = 0x5FF0023A;
                                    char szMessage[9]; // hold 8 characters plus a \0
                                    sprintf(szMessage, "%d", nMessage); // or itoa(nMessage, szMessage, 16);
                                    int nBytes = send(s, szMessage, lstrlen(szMessage), 0);

                                    N 1 Reply Last reply
                                    0
                                    • D David Crow

                                      NewHSKid wrote: the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) Does that mean he is supplying you with a valid socket descriptor? Given that the second parameter of send() is expecting a char*, you'll need:

                                      int nMessage = 0x5FF0023A;
                                      char szMessage[9]; // hold 8 characters plus a \0
                                      sprintf(szMessage, "%d", nMessage); // or itoa(nMessage, szMessage, 16);
                                      int nBytes = send(s, szMessage, lstrlen(szMessage), 0);

                                      N Offline
                                      N Offline
                                      NewHSKid
                                      wrote on last edited by
                                      #18

                                      Hi, Thanks for helping. So when i use that code, the receiving program will get the hex number of 5FF0023A? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

                                      D 1 Reply Last reply
                                      0
                                      • N NewHSKid

                                        hi, here is what i want to do, the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) int message; message = 0x5FF0023A; send(s, message, strlen(message), 0); It says send can't convert parameter 2 from int to const char * and it says the same thing about strlen. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        try this : send(s, (const char FAR*)message, sizeof(message), 0); though the sizeof(message) param can just be 0, as message oriented sockets will send a zero-length transport datagram. :cool:

                                        1 Reply Last reply
                                        0
                                        • N NewHSKid

                                          Hi, Thanks for helping. So when i use that code, the receiving program will get the hex number of 5FF0023A? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

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

                                          No, the recv() function on the other end will receive "5FF0023A" as a char*. It can convert that to an integer (long actually) with strtol(). long lMessage; char szMessage[9], *stop; int nBytes = recv(s, szMessage, sizeof(szMessage), 0); lMessage = strtol(szMessage, &stop, 16);

                                          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