Computer Science Canada

string::substr(begin, end)

Author:  Justin_ [ Sun Feb 12, 2006 10:10 pm ]
Post subject:  string::substr(begin, end)

Hi friends.

There is something screwy going on with the substr method. Here's my code.


c++:

int Hex::ConvertToInt(std::string hexNum)
{
    int num = 66//66 is an arbitrary initial value.
    std::cout << hexNum << std::endl;
    //transform(hexNum.begin(), hexNum.end(),hexNum.begin(), tolower);
    for (int i = 0; i <= 15; i++)
    {
       std::cout << "i= " << i << std::endl;
       std::cout << "substr= " << hexOrder.substr(i, i+1) << " ";
       if (hexNum == hexOrder.substr(i, i+1))
       {
          num = i;
          break;
       }
    }
    std::cout << num;
    return num;
}


here is the output:

code:

a
i= 0
substr= 0 i= 1
substr= 12 i= 2
substr= 234 i= 3
substr= 3456 i= 4
substr= 45678 i= 5
substr= 56789a i= 6
substr= 6789abc i= 7
substr= 789abcde i= 8
substr= 89abcdef i= 9
substr= 9abcdef i= 10
substr= abcdef i= 11
substr= bcdef i= 12
substr= cdef i= 13
substr= def i= 14
substr= ef i= 15
substr= f 66


the problem is that for each substring there should only be one character. For instance, if the substring was (0,1) only one character is printed. If i=0, then i + 1= 1 and thus (0,1). It should consistantly only be one character but you see, each time it goes it gets to be more characters all the way up to i=8 and then it retracts again. Why? And how to fix?

Author:  wtd [ Sun Feb 12, 2006 11:03 pm ]
Post subject: 

Try looking at the docs.

basic_string::substr takes two arguments. The first is the starting index, and the second is the count of characters.

You may wish to use the "at" member function instead, though.

Author:  Justin_ [ Sun Feb 12, 2006 11:23 pm ]
Post subject: 

okay, thanks.

Author:  wtd [ Mon Feb 13, 2006 1:33 am ]
Post subject: 

Oh, and you're working too hard.

code:
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string hex_as_string = "FF";
    std::stringstream ss(hex_as_string);
    int num;

    ss >> std::hex >> num;

    std::cout << num << std::endl;

    return 0;
}

Author:  Justin_ [ Tue Feb 14, 2006 5:58 pm ]
Post subject: 

Where there are comments I am unsure of what is taking place. Where there are not comments, I know what is taking place.

c++:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string hex_as_string = "FF";
    std::stringstream ss(hex_as_string) //initializing ss to "FF";
    int num;

    ss >>//whats this? std::hex //whats this? >> num;

    std::cout << num << std::endl;

    return 0;
}

Author:  Andy [ Tue Feb 14, 2006 6:13 pm ]
Post subject: 

read up on wtd's stringstream tutorial

found here

Author:  wtd [ Tue Feb 14, 2006 6:34 pm ]
Post subject: 

Streams work a little differently with some values than with others.

The std::hex variable, for instance, tells the stream that that next thing should be treated as a hexidecimal number.

Author:  Justin_ [ Tue Feb 14, 2006 8:52 pm ]
Post subject: 

good stuff Laughing

Author:  Justin_ [ Tue Feb 14, 2006 9:37 pm ]
Post subject: 

there doesn't seem to be an std::bin, how should I convert decimal to binary?

Author:  wtd [ Wed Feb 15, 2006 12:00 am ]
Post subject: 

You're gonna have to do that one the hard way. But even then, it's not that hard. Before you start writing code, really think about how you'd do it by hand.


: