Letters to Numbers

Okay, so I have a science fair project and I decided on doing a series of programs that show the simpleness behind hacking a password. Each program increases in complexity starting with a four digit password, and ending with a 10 digit letter and number combo that is case sensitive. Right now I need to figure out a way to have my program be able to convert chars into ints, so that I can do a counter and then I also need to be able to convert the ints, back into chars. Thank you! Love to get an answer back ASAP and...MERRY CHRISTMAS!
convert chars into ints
int i = static_cast<int>(c);
ints, back into chars
char c = static_cast<char>(i);

ending with a 10 digit letter and number combo that is case sensitive
So my lO)bnS/h`Cr0d{U(;aKQqT<r is safe, then?
Well I haven't reached the 10 digit combo program yet, I'm working on the second program in the series which would allow the user to enter a five digit letter only password that is uppercase only, and I need to convert each of those letters that they input, into a number. From there I would just set a counter and a bunch of either switch of if-else statements to check where the number should go. But. How do I get The inputted letters to change to a number????? Aslo, what headers would I use?
xHyperionx wrote:
How do I get The inputted letters to change to a number
MiiNiPaa wrote:
int i = static_cast<int>(c);
Inside computer there are no characters. There are numbers. Computer uses something called character table to know that, say , character number 97 sould be drawn as 'a'
A digit '0' in fact is a character number 48 in ASCII
Actually there are no numbers, its binary.

What is your question, MiiNiPaa gave you one of the best ways to do it.
> Binary.
Especially in ternary computers.

And also it is binary numbers. Almost all assembly commands (aside fro those which threats register content as address) works as arithmetic operations.
I'm still confused, so what is the code in order to get it to change into a number for me to be able to put a counter? and what are the headers necessary?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
int main()
{
    //Hope nobody will use upper ASCII plane
    //Otherwise you should handle signed/unsigned chars yourself
    int frequency_table[128] = { 0 };
    char c = '\0'; //Or int. It works either way
    while((c = std::cin.get()) != '\n')
        ++frequency_table[c];
    for(int i = 0; i < 128; ++i)
        if (frequency_table[i] != 0)
            std::cout << (c = i) << ": " << frequency_table[i] << '\n';
//          std::cout << static_cast<char>(i) << ": " << frequency_table[i] << '\n';
}
Hello
H: 1
e: 1
l: 2
o: 1
Last edited on
ok but can you please explain to me what all of this does, because I cannot just copy your work and pass it off as my own?
Line 6: create a table all possible (and some impossible) base ASCII characters can be indices of. Base ASCII characters have codes 0-127.

Line 8: read character from standard input and save it to c, until we hit newline
Line 9: increment corresponding element of table. Uses char as int. (no actual conversion as char is already an integer)

Line 10 - 11: loop over the table and print all characters encountered at least once. Uses trick to convert integer to char. Commented out line uses explicit cast instead.
hmmm ok so if i wanted it to go from "A" to "Z" I would input it something like with the frequency table: int frequency_table[90] = {65}; is that right?
No. Except for situation where all your letters count should start from 65.
Code will be the same. Just output only values of 'A'-'Z':
1
2
3
4
    for(int i = 'A'; i < 'Z' + 1; ++i)
        if (frequency_table[i] != 0)
            std::cout << (c = i) << ": " << frequency_table[i] << '\n';
HeLLO!
H: 1
L: 2
O: 1
wait but I DO want it to start from 65 and only go to 90, because this is a prgram which only does uppercase alphabet
I DO want it to start from 65

Ok, so output you want is:
HeLLO!
H: 66
L: 67
O: 66
right?
no ur not understanding my program lol, her lemme try to explain better: My program should do output something like:

Please enter your maximum five digit uppercase alphabetical only password: PGGS
AAAAAAABAAACAAADAAAEAAAFAAAGAAAHAAAIAAAJAAAKAAALAAAMAAANAAAOAAAPAAAQAAARAAAS

AABSAACSAADSAAESAAFSAAGS

ABGSACGSADGSAEGSAFGSAGGS

BGGSCGGSDGGSDGGSEGGSFGGSGGGSHGGSIGGSJGGSKGGSLGGSMGGSNGGSOGGSPGGS

Your password is: PGGS



It can be formatted differently of course and the format shouldnt be too hard to do but i need it to like loop around if i could i would show u a file of what i have done for my first program.
I'm just not sure how to attach a file here lol
Last edited on
Start with string "AAAAA". Test it and increment last symbol until it is larger than 'Z'. Then set last symbol to 'A' again and increment second-to last, start again with last. When second to last will be larger than Z, set it to 'A' and inctement previous symbol, etc.
how would I implement that into the code that you posted earlier?
Code I posted earlier does completely different thing. It counts frequenvy of letters in entered string and exis only to show int←→char conversion.
Here http://www.cplusplus.com/forum/general/151874/#msg790526 I show how to loop over character range easily.
ok thx m8
Topic archived. No new replies allowed.