Thread: using istringstream class to parse a string to get hex numbers

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    16

    using istringstream class to parse a string to get hex numbers

    Suppose I have a string with hex numbers in it like this "1a:2b:3c:4d:5e:6f"
    How do I use istringstream to parse it to get each of 1a, 2b, 3d, 4d, 5e, 6f hex numbers ?


    cheers

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Like this, (you may have to change the condition of the looping)
    Code:
    while(your_stream)
    {
        your_stream >> std::hex >>your_int;
        char temp; 
        your_stream >> temp;
    }

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    So I'd do something like:


    Code:
    istringstream iss (mystring,istringstream::in);
    
    int hexval[6];
    int temp;
    
    for (n=0; n<6; n++)
    {
    iss >> std::hex >> hexval[n];
    iss >> temp
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not quite. A colon cannot be read as an int, and it won't be skipped if it is there. Each attempt to read an int after encountering the first colon will fail.

    A colon can be read (and skipped) as a char though. That is why temp, in manasij7479's example, was of type char, not int.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    this worked but i wanted hexvalue to be unsigned char, this didn't work - none of the values written to it were correct

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by adetheheat View Post
    this worked but i wanted hexvalue to be unsigned char, this didn't work - none of the values written to it were correct
    Typecasting gives pretty correct answers for me.
    Post your whole code... if you want to see if you have done anything wrong.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    16
    this doesn't work:

    Code:
    int main(int argc, char* argv[])
    {
      int n;
      string stringvalues;
      //int val[6];
      unsigned char val[6];
      char temp;
      stringvalues = "1a:2b:3c:4d:5e:6f";
      istringstream iss (stringvalues,istringstream::in);
      for (n=0; n<6; n++) 
      { 
       iss >> std::hex >> (unsigned char) val[n]; 
       iss >> temp ;
      } 
    
     return 0;
    }
    value written to val[n] is always wrong if val array is unsigned char

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Get the number inputs as integers.
    After that type cast them into unsigned chars when putting into the unsigned char array.

    Your code does not work because the stream enters into an error state when it attempts to read in a number into a char.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Your code does not work because the stream enters into an error state when it attempts to read in a number into a char.
    What?? A char is a number. The stream will not go into an error state by trying to put a number into a char/unsigned char.

    The problem is trying to use an unsigned char in the extraction operation. You need to extract each section into an int, then convert to the unsigned char.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       int n;
       string stringvalues;
       unsigned char val[6];
       int int_temp;
       char temp;
       stringvalues = "1a:2b:3c:4d:5e:6f";
       istringstream iss (stringvalues);
       for(int i = 0; i < 6; ++i)
       {
          iss >> std::hex >> int_temp;  // Extract the number into an int.
          val[i] = int_temp; // Convert the int to your unsigned char.
          iss >> temp;  // Extract and throw away the colon.
       }
    
       for(int i = 0; i < 6; ++i)
          cout << static_cast<int>(val[i]) << " ";
       return 0;
    }
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parse a template class via xml
    By thedodgeruk in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2012, 09:47 PM
  2. how to parse a string
    By -EquinoX- in forum C Programming
    Replies: 48
    Last Post: 05-21-2008, 01:57 PM
  3. parse truly large numbers to binary
    By alch in forum C Programming
    Replies: 3
    Last Post: 07-20-2006, 01:39 PM
  4. Parse the string
    By swanley007 in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2005, 11:17 AM
  5. String Parse.
    By Coder87C in forum C# Programming
    Replies: 3
    Last Post: 08-16-2005, 02:18 AM