Best way to convert a character to a number?

Hello!
I'm working on a program that asks for a serial key, checks the input and runs it through an algorithm. To do that I want to convert every character in an array to a number, what's the best way to do that?

I've tried several different ways to solve the problem, but nothing has worked. It was really easy in Python, but very difficult in C++.

Thanks in advance.
A c++ function atoi might be what you are looking for
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
I've already tried it, with no success.

To elaborate what I want to do, I can say that I have an array with the whole alphabet in it. I want each character to represent a number.
Atoi didn't work for that.

Thank you for your answer though.
Have you established your translation algorithm? Meaning, have you mapped the characters to numbers, and chosen a radix for your interpretation?
Have you established your translation algorithm? Meaning, have you mapped the characters to numbers, and chosen a radix for your interpretation?


I haven't mapped the characters to any number yet, that's what I can't figure out how to do.
What's a radix?


Thank you for your answer.
static_cast operator works the best :). it needs a good understanding of pointer and class
I'm still trying to understand what you want to do. The kind of information that would be helpful is stuff like:

- how many characters are you going to read in?
- will a character have the same value regardless of position?
- are you going to sum the numeric conversions into a single number, or maintain a list of numbers corresponding to the individual characters read in?

In short, it would be helpful if you'd flesh out the problem for us, along with whatever work you've done so far. We can probably give you better guidance that way.
I'm still trying to understand what you want to do. The kind of information that would be helpful is stuff like:

- how many characters are you going to read in?
- will a character have the same value regardless of position?
- are you going to sum the numeric conversions into a single number, or maintain a list of numbers corresponding to the individual characters read in?

In short, it would be helpful if you'd flesh out the problem for us, along with whatever work you've done so far. We can probably give you better guidance that way.


Sorry, I'll give you an elaboration of my program.
I want each character in an alphabet to have a numerical value, I was thinking of storing them in an array.
Yes, I'm going to sum all the numerical values into a single number.

Here's the code I have so far: http://pastebin.com/wnvu0mFM
Keep in mind that the code is in a very early stage and you can probably find some errors there. Some parts are just there for testing, aswell.
I'm not very experienced with OOP, so there might be some errors with the class and how it's structured. I wouldn't mind tips on how to improve my code though!

static_cast operator works the best :). it needs a good understanding of pointer and class


It seems kind of complicated, aren't there any easier solutions?

Thank you both for answering.
Just a harmless bump. :3
umm- chars already represent numbers?

http://www.asciitable.com/

so if I understand this correctly, here's all you'd have to do:
1
2
3
4
5
uint32_t totalValue = 0;
string instring = "Hello this is an input string";
for (int a = 0; a < instring.size(); a++){
totalValue+=instring[a];
}
Thanks, ultifinitus. Is there a way to set the value for each character manually? But yeah, it works.
Thanks again!
well you can certainly can, if you wanted you could have an associative array or a map or you could algorithmically produce the values, all depends on your end goals.
Topic archived. No new replies allowed.