Read from string

This is a discussion on Read from string within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to read every two characters from a string inputted by the user, then convert that from hex ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    Read from string

    Hi, I need to read every two characters from a string inputted by the user, then convert that from hex to decimal form. I don't know how to start this so can someone please help?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>I don't know how to start

    Hint:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
       // put your code here
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    Sorry, maybe i was to vague.

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    int main()
    {
        int   intpass;
        string enpass, hexpass;
    
        cin >> hexpass;
        
       intpass = (int(hexpass.at(0)) + (hexpass.at(1)));      
        cout << hex << intpass;
      
        cin.ignore();
        cin.get();
        return 0;
    }
    The String will look something like this

    A347F2B74EE9A9F6

    I want to read every two characters, ( they're already in hex form) then convert to int.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    You need to use a loop to traverse through the string and extracting 2 characters until it reaches the end of the string.
    Last edited by 0rion; 02-19-2006 at 09:02 AM.
    The cost of software maintenance increases with the square of the programmer's creativity.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    I was going to use a loop, but the thing is A3 in decimal 163. What I'm doing now is taking the value of A and adding it to the value of 3, which is not 163. I don't know how to store 2 characters like this.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    What I'm doing now is taking the value of A and adding it to the value of 3, which is not 163.
    You need to multiply by 16 before adding. Of course the value of 0xA is 10, not 65.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    I would probably try and create another function that converts hex to decimal that takes 2 characters and returns back the decimal value. You could use a mini-symbol table to link up 'A' to 10, 'B' to 11 etc. and then just do "firstdigit * 16 + seconddigit" and return that.
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21