Char array to int array conversion

closed account (Ey6Cko23)
Hello, i've been working on some encryption as a project and now i need to turn a char array into an int array. i also want to add an integer to every single value in the int array. how would i do that? the str array contains letters and i want to turn them into their ascii numbers for the int array.

i have made this and its working but now i want to do it with arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    char a; //The characters
    char b;
    char c;
    char d;
    char e;

    std::cout << "*CURRENTLY MAX 5 LETTERS* \n Please input a word to encrypt: ";
    std::cin >> a; //assigns the input to a variable
    std::cin >> b;
    std::cin >> c;
    std::cin >> d;
    std::cin >> e;

    int aNumber(a); //turns the variable into numbers
    int bNumber(b);
    int cNumber(c);
    int dNumber(d);
    int eNumber(e);



this is the code i currently have but how do i turn the str array into an int array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "stdafx.h" //needed for reasons
#include <iostream>
int input(); //forward declarations so the compiler doesn't cry
int varToInt();
int key();
int encrypting();
void wait();

int main()
{
    input();
    varToInt();
    key();
    encrypting();
    wait();
    return 0;
}


int input()
{
    char toEncrypt[140] = "0";
    int x = 0;
    std::cout << "*MAX 140 CHARACTERS* \n Please input a word/sentence to encrypt: ";
    char str[140];
    std::cin.getline(str, 140);
    std::cout << "Input all good \n";
    std::cout << str[4];
    //int y;
    //y = std::cin.gcount();
    //while (x < y)
    //{
    //    std::cin >> toEncrypt[x];
    //    x++;
    //}
    //std::cout << toEncrypt[5];
    return 0;
}

int varToInt()
{
    return 0;
}

int key()
{
    return 0;
}

int encrypting()
{
    return 0;
}

void wait()
{
    std::cin.clear(); //makes sure the program doesnt directly close when finished
    std::cin.ignore(32767, '\n');
    std::cin.get();
    
}
Last edited on
closed account (Ey6Cko23)
the str array contains letters and i want to turn them into their ascii numbers for the int array
i need to turn a char array into an int array
Why?

i also want to add an integer to every single value in the int array
You can add an integer to a char: str[4] += 5;

I don't think that you need an int array. You can do anything with a char array that you can do with an int array.
closed account (Ey6Cko23)
@coder777 im making a really weak encryption and the encrypted sentence/word will be numbers so thats why i need to convert the lettters to their ascii numbers.
and i dont want to add it to just one char from the array but i want to add it to every char in the array
i need to convert the lettters to their ascii numbers.
No, a string contains already the ascii value. For instance:

char toEncrypt[140] = "0";

contains toEncrypt[0] -> 48 which is the ascii representation of '0'

Another example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    char num[10];
    std::cout << "Enter number: ";
    std::cin >> num;
    for(int i = 0; num[i] != 0; ++i)
    {
        num[i] += 1;
    }
    std::cout << "Encrypted: " << num;

    return 0;
}
Enter number: 123
Encrypted: 234
closed account (Ey6Cko23)
@coder777
could you please explain this line:
1
2
3
4
    for(int i = 0; num[i] != 0; ++i)
    {
        num[i] += 1;
    }


im still a beginner and i dont understand any of that.
Topic archived. No new replies allowed.