using istringstream class to parse a string to get hex numbers

This is a discussion on using istringstream class to parse a string to get hex numbers within the C++ Programming forums, part of the General Programming Boards category; 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 ...

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

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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;
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    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,299
    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%.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    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, 08: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, 10:17 AM
  5. String Parse.
    By Coder87C in forum C# Programming
    Replies: 3
    Last Post: 08-16-2005, 02:18 AM

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