hex string conversion to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emp
    New Member
    • Nov 2006
    • 32

    hex string conversion to integer

    Yes 6.0

    I am using code similar to this:
    Code:
    #include <stdlib.h>
     #include <stdio.h>
    
     int main(void)
     {
     char string[] = "0D76";
     long value = strtol(string, NULL, 16);
     printf("value of '%s' is %ld\n", string, value);
     return 0;
     }
    I have this application running in VS2015 but cannot get it to output an executable that will run on an old 32bit XP computer that the owner doesn't want to upgrade.
    Thus I'm on VS6.0

    strtol requires a
    char string[] as its first argument.
    all of my data is being passed in, in a huge array of records declared
    std::string hexdata[250000];
    How do I get around this? This VS6 environment doesn't really have streams to implement.
    Also, when I look in the VS include directory all the standary headers are there including stdlib.h
    The compiler an error that strtol is not a member of std.
    I have opened stdlib.h and I find strtol listed in there.
    Any clues on how to get it to see strtol in stdlib.h or is there another workaround giving my above constraints.
    Thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I don't know how VS2015 is packaged but this code compiles and links just fine using VS2013.

    I'd like to see the error. strtol has never been part of std since it's part of C and std applies only to C++.

    Comment

    • emp
      New Member
      • Nov 2006
      • 32

      #3
      Ok, there is the exact block of code that I am using:
      Code:
      std::string hold =   text[i].substr(2, 1);
      std::cout << "hold = " << hold << std::endl;	
      int byte_count  = std::strtol(hold, &ptr, 16);
      and here are the errors:

      file_operations .cpp
      c:\program files\microsoft visual studio\myprojec ts\rcmprogrammi ng\file_operati ons.cpp(112) : error C2039: 'strtol' : is not a member of 'std'
      c:\program files\microsoft visual studio\myprojec ts\rcmprogrammi ng\file_operati ons.cpp(112) : error C2664: 'strtol' : cannot convert parameter 1 from 'class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >' to 'const
      char *'
      No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
      Error executing cl.exe.

      RCMprogramming. exe - 2 error(s), 0 warning(s)

      Comment

      • emp
        New Member
        • Nov 2006
        • 32

        #4
        If I remove the std:: from before strtol I get this error which I cant beat either

        c:\program files\microsoft visual studio\myprojec ts\rcmprogrammi ng\file_operati ons.cpp(111) : error C2664: 'strtol' : cannot convert parameter 1 from 'class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >' to 'const
        char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
        Error executing cl.exe.

        RCMprogramming. exe - 1 error(s), 0 warning(s)

        Remember that the data comes to this function in the form of an array declared:
        std::string hexdata[250000];

        Here is a representative line of data:

        :800C0200000000 111111110000000 000000000000000 000000000000000 000111111110000 000000000000000 000001111111100 000000040404040 404040404040404 F6F6F6F6E8E8E8E 800000000000000 001515151504040 404040404040000 000011111111040 404040404040400 000000040404040 404040415151515 4040404004AE

        Once I get through the header bytes then I grab 2 bytes at a time and convert it to integer.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I see what's happening.

          Some nutcase has written a function that works like strtol but which takes an std::string as an argument. Then the function was named strtol. To avoid a name collision, the function was put in the std namespace. This violates namespace rules since std is reserved for C++ library items. It also violates the rule that you don't name your functions with the same name as system functions.

          You need to find this code.

          Comment

          • emp
            New Member
            • Nov 2006
            • 32

            #6
            I managed a fix.

            const * temphold;

            then

            temphold = hold.c_str();
            int byte_count = strtol(temphold , &ptr, 16);

            This correctly decodes the two bytes in temphold to the correct decimal value.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Probably you don't need temphold:

              Code:
              int byte_count = strtol(hold.c_str() , &ptr, 16);
              I would certainly go through and remove all std::strtol calls. The confusion factor should be obvious.

              Comment

              • emp
                New Member
                • Nov 2006
                • 32

                #8
                Certainly, and thanks

                Comment

                Working...