String to different types

Hi
i have a string, eg.
000050000081E1500F01EE100AF8EE34101FE59A

i want to read it as different types., how do i read it as hex? decimal etc?
could someone post the code? thanks

also , what is the syntax for comparing two strings?
thanks
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

That may be of use to you for reading the string.

As for the syntax for comparing strings if you mean std::string:
1
2
if (string1 == string2)
    // ... 

That should suffice. If you mean C-style strings (char arrays) them you can use strcmp defined in <cstring>.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstring>

int main()
{
    char str1[] = "this is a string";
    char str2[] = "another string";
    if (strcmp(str1, str2)) std::cout << "they match!" << std::endl;
    return 0;
}
Chewbob : strcmp returns 0 if the strings are equal. So either !strcmp(str1, str2) or strcmp(str1, str2) == 0.
Last edited on
Ah, yes. Slipped my mind. I don't see the logic behind that, to be honest.
@petergrwl
You have two tasks

1. Split the string into its component parts
2. Convert each part into a number

To split the string, member functions like substr() will help.
To convert strings into numbers, a stringstream will help.

Good luck!

[edit]
@Chewbob
It is so you can write boolean expressions the same way you would with integer values

if (x1 < x2) ... // x1 < x2

if (strcmp( s1, s2 ) < 0) ... // s1 < s2

Etc.
Last edited on
Oh yeah, thanks. I should have just looked at the reference page.
Please read this. If you are already using std::string why not use streams to get the job done?
http://cplusplus.com/forum/articles/9645/
Topic archived. No new replies allowed.