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. String (Hex) to Hex

String (Hex) to Hex

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
14 Posts 5 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
    suguimoto
    wrote on last edited by
    #1

    Hello, I got a file (.INI) and it's filled with a lot of hexa numbers:

    00000000
    00000000
    FFFFFC24
    00000000
    00000000
    FFFFFC2C
    00000000
    FFFFFC70
    FFFFFC24

    I must convert these strings hex to real hex number, obviously they must be unsigned. I tried a lot of ways, and got only trash :(( each line must be a row in an array, for example "unsigned int myArray[200];" this array must be filled with hexa from the string (INI File). Cuold anyone help me?? Thanks

    R W 2 Replies Last reply
    0
    • S suguimoto

      Hello, I got a file (.INI) and it's filled with a lot of hexa numbers:

      00000000
      00000000
      FFFFFC24
      00000000
      00000000
      FFFFFC2C
      00000000
      FFFFFC70
      FFFFFC24

      I must convert these strings hex to real hex number, obviously they must be unsigned. I tried a lot of ways, and got only trash :(( each line must be a row in an array, for example "unsigned int myArray[200];" this array must be filled with hexa from the string (INI File). Cuold anyone help me?? Thanks

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      suguimoto wrote:

      I tried a lot of ways, and got only trash

      Have you tried strtol( const char *nptr, char **endptr, int base )? -- Roger


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote

      "No one remembers a coward!" - Jan Elfström 1998
      "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above

      1 Reply Last reply
      0
      • S suguimoto

        Hello, I got a file (.INI) and it's filled with a lot of hexa numbers:

        00000000
        00000000
        FFFFFC24
        00000000
        00000000
        FFFFFC2C
        00000000
        FFFFFC70
        FFFFFC24

        I must convert these strings hex to real hex number, obviously they must be unsigned. I tried a lot of ways, and got only trash :(( each line must be a row in an array, for example "unsigned int myArray[200];" this array must be filled with hexa from the string (INI File). Cuold anyone help me?? Thanks

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        can you show us what you tried, maybe then we could help you to correct it.

        S 1 Reply Last reply
        0
        • W Waldermort

          can you show us what you tried, maybe then we could help you to correct it.

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

          I aprreciate your effort to help me :D so here is the code:

          #include "stdafx.h"
          #include
          #include

          int main(int argc, char* argv[])
          {
          FILE *fp;
          fp = fopen("file.ini", "rt");
          char str[32];
          unsigned int mem[9];
          char * endptr;

          for(int i = 0; i<9; i++){
          fgets(str, 32, fp);
          
          mem\[i\] = strtol(str, &endptr, 16);
          
          printf("%i\\n\\r", mem\[i\]);
          }
          

          system("PAUSE");
          return 0;
          }

          I'm a newbie in c++ and this seems to be a little bit hard for me. my result:

          0
          0
          2147483647
          0
          0
          2147483647
          0
          2147483647
          2147483647

          2147483647(dec) is equal to 7FFFFFFF(hex) =/ whats happening? hehehe Regards

          M W D 5 Replies Last reply
          0
          • S suguimoto

            I aprreciate your effort to help me :D so here is the code:

            #include "stdafx.h"
            #include
            #include

            int main(int argc, char* argv[])
            {
            FILE *fp;
            fp = fopen("file.ini", "rt");
            char str[32];
            unsigned int mem[9];
            char * endptr;

            for(int i = 0; i<9; i++){
            fgets(str, 32, fp);
            
            mem\[i\] = strtol(str, &endptr, 16);
            
            printf("%i\\n\\r", mem\[i\]);
            }
            

            system("PAUSE");
            return 0;
            }

            I'm a newbie in c++ and this seems to be a little bit hard for me. my result:

            0
            0
            2147483647
            0
            0
            2147483647
            0
            2147483647
            2147483647

            2147483647(dec) is equal to 7FFFFFFF(hex) =/ whats happening? hehehe Regards

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            suguimoto wrote:

            2147483647(dec) is equal to 7FFFFFFF(hex)

            Yes

            1 Reply Last reply
            0
            • S suguimoto

              I aprreciate your effort to help me :D so here is the code:

              #include "stdafx.h"
              #include
              #include

              int main(int argc, char* argv[])
              {
              FILE *fp;
              fp = fopen("file.ini", "rt");
              char str[32];
              unsigned int mem[9];
              char * endptr;

              for(int i = 0; i<9; i++){
              fgets(str, 32, fp);
              
              mem\[i\] = strtol(str, &endptr, 16);
              
              printf("%i\\n\\r", mem\[i\]);
              }
              

              system("PAUSE");
              return 0;
              }

              I'm a newbie in c++ and this seems to be a little bit hard for me. my result:

              0
              0
              2147483647
              0
              0
              2147483647
              0
              2147483647
              2147483647

              2147483647(dec) is equal to 7FFFFFFF(hex) =/ whats happening? hehehe Regards

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              You are reading 32 characters from the file, is this correct? If so then declare your str array to hold 33 to allow for the null terminator. What is the value of endptr at the end of each loop?

              1 Reply Last reply
              0
              • S suguimoto

                I aprreciate your effort to help me :D so here is the code:

                #include "stdafx.h"
                #include
                #include

                int main(int argc, char* argv[])
                {
                FILE *fp;
                fp = fopen("file.ini", "rt");
                char str[32];
                unsigned int mem[9];
                char * endptr;

                for(int i = 0; i<9; i++){
                fgets(str, 32, fp);
                
                mem\[i\] = strtol(str, &endptr, 16);
                
                printf("%i\\n\\r", mem\[i\]);
                }
                

                system("PAUSE");
                return 0;
                }

                I'm a newbie in c++ and this seems to be a little bit hard for me. my result:

                0
                0
                2147483647
                0
                0
                2147483647
                0
                2147483647
                2147483647

                2147483647(dec) is equal to 7FFFFFFF(hex) =/ whats happening? hehehe Regards

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                suguimoto wrote:

                whats happening?

                strtol() is returning LONG_MAX because of overflow. 0xFFFFFC24 is too big to fit in a signed long. Try strtoul()

                1 Reply Last reply
                0
                • S suguimoto

                  I aprreciate your effort to help me :D so here is the code:

                  #include "stdafx.h"
                  #include
                  #include

                  int main(int argc, char* argv[])
                  {
                  FILE *fp;
                  fp = fopen("file.ini", "rt");
                  char str[32];
                  unsigned int mem[9];
                  char * endptr;

                  for(int i = 0; i<9; i++){
                  fgets(str, 32, fp);
                  
                  mem\[i\] = strtol(str, &endptr, 16);
                  
                  printf("%i\\n\\r", mem\[i\]);
                  }
                  

                  system("PAUSE");
                  return 0;
                  }

                  I'm a newbie in c++ and this seems to be a little bit hard for me. my result:

                  0
                  0
                  2147483647
                  0
                  0
                  2147483647
                  0
                  2147483647
                  2147483647

                  2147483647(dec) is equal to 7FFFFFFF(hex) =/ whats happening? hehehe Regards

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

                  suguimoto wrote:

                  mem[i] = strtol(str, &endptr, 16);

                  Why aren't you using strtoul()?


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  S 1 Reply Last reply
                  0
                  • D David Crow

                    suguimoto wrote:

                    mem[i] = strtol(str, &endptr, 16);

                    Why aren't you using strtoul()?


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    S Offline
                    S Offline
                    suguimoto
                    wrote on last edited by
                    #9

                    I changed to strtoul... and get the same result =/ also increased the size from 32 to 33, still get the same thing. very weird. But I'm still trying

                    D 1 Reply Last reply
                    0
                    • S suguimoto

                      I aprreciate your effort to help me :D so here is the code:

                      #include "stdafx.h"
                      #include
                      #include

                      int main(int argc, char* argv[])
                      {
                      FILE *fp;
                      fp = fopen("file.ini", "rt");
                      char str[32];
                      unsigned int mem[9];
                      char * endptr;

                      for(int i = 0; i<9; i++){
                      fgets(str, 32, fp);
                      
                      mem\[i\] = strtol(str, &endptr, 16);
                      
                      printf("%i\\n\\r", mem\[i\]);
                      }
                      

                      system("PAUSE");
                      return 0;
                      }

                      I'm a newbie in c++ and this seems to be a little bit hard for me. my result:

                      0
                      0
                      2147483647
                      0
                      0
                      2147483647
                      0
                      2147483647
                      2147483647

                      2147483647(dec) is equal to 7FFFFFFF(hex) =/ whats happening? hehehe Regards

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

                      suguimoto wrote:

                      printf("%i\n\r", mem[i]);

                      Since mem[i] is an unsigned int, you should be using "%u" instead.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      S 1 Reply Last reply
                      0
                      • S suguimoto

                        I changed to strtoul... and get the same result =/ also increased the size from 32 to 33, still get the same thing. very weird. But I'm still trying

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

                        suguimoto wrote:

                        I changed to strtoul... and get the same result =/

                        How are you confirming that strtoul() is failing?

                        suguimoto wrote:

                        also increased the size from 32 to 33, still get the same thing.

                        Why would you expect that to make a difference, since there are no more than 10 characters per line?


                        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • D David Crow

                          suguimoto wrote:

                          printf("%i\n\r", mem[i]);

                          Since mem[i] is an unsigned int, you should be using "%u" instead.


                          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                          "Judge not by the eye but by the heart." - Native American Proverb

                          S Offline
                          S Offline
                          suguimoto
                          wrote on last edited by
                          #12

                          Thanks!! It's working! I used %x to display everything as Hex, and it's working perfectly. Thank you guys very much!!! code:

                          int main(int argc, char* argv[])
                          {
                          FILE *fp;
                          fp = fopen("mem.ini", "rt");
                          char str[32];
                          unsigned int mem[8192];
                          char * endptr;
                          int teste;

                          for(int i = 0; i<8192; i++){
                          
                          fgets(str, 32, fp);
                          
                          mem\[i\] = strtoul(str, &endptr, 16);
                          
                          printf("%x\\n\\r", mem\[i\]); // %u   to display unsigned values
                          }
                          

                          system("PAUSE");
                          return 0;
                          }

                          D 1 Reply Last reply
                          0
                          • S suguimoto

                            Thanks!! It's working! I used %x to display everything as Hex, and it's working perfectly. Thank you guys very much!!! code:

                            int main(int argc, char* argv[])
                            {
                            FILE *fp;
                            fp = fopen("mem.ini", "rt");
                            char str[32];
                            unsigned int mem[8192];
                            char * endptr;
                            int teste;

                            for(int i = 0; i<8192; i++){
                            
                            fgets(str, 32, fp);
                            
                            mem\[i\] = strtoul(str, &endptr, 16);
                            
                            printf("%x\\n\\r", mem\[i\]); // %u   to display unsigned values
                            }
                            

                            system("PAUSE");
                            return 0;
                            }

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

                            suguimoto wrote:

                            for(int i = 0; i<8192; i++){

                            What if there are only 5, or 8193, lines in the file? It's not wise to hard-code such numbers.


                            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                            "Judge not by the eye but by the heart." - Native American Proverb

                            S 1 Reply Last reply
                            0
                            • D David Crow

                              suguimoto wrote:

                              for(int i = 0; i<8192; i++){

                              What if there are only 5, or 8193, lines in the file? It's not wise to hard-code such numbers.


                              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                              "Judge not by the eye but by the heart." - Native American Proverb

                              S Offline
                              S Offline
                              suguimoto
                              wrote on last edited by
                              #14

                              its must be huge. because it's simulating a memory in a "rom" inside a virtual processor. i needed a function "readmemh" from verilog, but it couldnt be translated to c++, so i did it manually. this mem is going to be read a lot of times and changed a lot of times. I think 8192 is fixed, as it was in Verilog. So i'm just gonna leave it like that Thanks!!

                              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