HEX to DEC

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aquib
    New Member
    • Feb 2009
    • 2

    #1

    HEX to DEC

    Hi,
    I have a text file which has a line of hex values in the following format

    AC FF 1A C5............. ....FD (Total of 15 hex values) - called hex.txt

    I need to create another file in C++, which would convert the hex.txt into decimal values keeping the format unchanged. The resulting file (dec.txt) should be something like

    172 255 26 197...........2 53

    I cant seem to start in terms of logic. If someone who has worked on something similar or has ideas please do share them with me.

    Cheers
    aquib
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    You have to figure out how to handle the files.
    Code:
    cout<<"Enter an integer in hex format";
    cin>>h;//if h==FF
    cout>>hex<<h<<" is in dec "<<dec<<h<<end line;//prints FF is in dec 255.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Start with something basic.

      Write a conversion function whose input is a char-pointer to a HEX-ASCII string with optional leading whitespace (that is, it is terminated by the first non-HEX-ASCII character). This function returns an updated char-pointer value (that points to the character that terminated the HEX-ASCII string) and the integer value corresponding to the HEX-ASCII string.

      How should your function handle the case where there are no HEX-ASCII characters in the string? That is, when you have no integer value to return. Do you want to add a success/failure return value?

      How should your function handle the case where there are so many HEX-ASCII characters that you will overflow the type used to return the integer type.

      Don't forget to support both upper- and lowercase HEX-ASCII characters.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Since this is C++, read your input file into a C++ string object using the >> operator.

        Next, pass the string to a function that you write that has a string& argument for the string you just read and returns a string. The returned string will have the decimal equivalent of the string passed in. Inside this function convert the single hax value in the argunment string to decimal. It is true that you might exceed the size of a long integer but considering this is an exercise I would get the thing working first on friendly values. Later, if you want, you can add error and overflow code.

        Next, output that returned string to your dec.txt file.

        Next, output a space to your dec.txt file as a delimiter.

        Repeat the above until your hex file is completely read.

        The >> operator stops on white space so you will get your hex values one at a time from your input file.

        Comment

        • aquib
          New Member
          • Feb 2009
          • 2

          #5
          Thanks for your responses guys..I find weaknessforcats idea worth pursuing...

          Comment

          Working...