changing letters value and binary conversion

so i'm attempting to change the value of chars (a-z, and 0-9) to 0-35. (a is 0, z is 25, 0 is 26, 9 is 35) can someone give me some help on how to do this? would pointers work for this?

Also, i'm attempting to convert an integer into binary, and have those value put into an array (number by number), so that the array would be:

arrayname [0][0] == 32, arrayname [0][1] == 16, etc.

I have seen while loops to convert an integer to binary, but that only works to print it to the screen with cout, i couldn't make it work reading into an array.

Any help much appreciated.
SGTChubbz wrote:
so i'm attempting to change the value of chars (a-z, and 0-9) to 0-35. (a is 0, z is 25, 0 is 26, 9 is 35) can someone give me some help on how to do this? would pointers work for this?
It's not possible unless you modify the compiler and character set you're using, which are both way out of even my abilities. Make sure you realize that the number associated with characters is not the same everywhere in the world, so NEVER rely on the numbers referring to specific characters.

What have you attempted with the binary thing? Hint: start with the program that prints it to the screen.
I've already gotten it to where you can turn it into binary and print the binary on the screen using cout. however i can't get the loop to be able to store the 0's and 1's in a string, array, etc.
If you can print it out you can store it in a string. What are you trying that doesn't work for you?
using a loop similar to this one: http://groups.engin.umd.umich.edu/CIS/course.des/cis400/cpp/binary.html
I am taking an integer (right now normal ascii values, but eventually it's supposed to be between 0 and 35) and converting them to their decimal (ascii) and binary values.

I'm just puzzled as how to get the loop to store the output in a string or array.

edit: i know the loop functions recursively, but i just can't think of how to implement a counter of some sort to determine the position of the digit. Unless i have the function pass another int (which starts at o for the original function call) that increases each time the function calls itself (pass int+1)?
Last edited on
I am having trouble understanding how there is a disconnect between "send character to console" and "append character to string". What exactly is confusing to you? I can't help unless I know that.
Last edited on
My problem is that the function is returning integer values and trying to add an integer to a string is giving me smiley faces when printing that string.
To convert int containing a decimal digit n to a char, add '0' to it. 1 + '0' == '1' and 5 + '0' == '5'.

1
2
3
4
5
6
int i = 1 ; // decimal digit 1

char c = i + '0' ; // char c == '1' 

string str = "10010" ;
str += ( i + '0' ) ; // str becomes "100101"  
thanks JL. I've been working on my code, and its now freezing no matter what i do and i can't tell why...

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
 #include <iomanip>
 #include <string>
 #include <cstring>
 using namespace std;

 //function to xor 2 strings together into a 3rd string
 //would have to change if using array storage?
 string XorStrings (string, string);
 //void printXor();
 int binToDec(string);
 void PTtoDec(string);
 void KtoDec(string);
 void bin(int);
 void printPTDecimal();
 void printKDecimal();


int PTdecimal[0][4]={};
int Kdecimal[0][4]={};

//Bin is where the current calculation it temp. held.
//ptbinx/kbinx is where the binary values of the letters are held are converting from decimal.
string Bin ="";
string ptbin0,ptbin1,ptbin2,ptbin3 ="";
string kbin0,kbin1,kbin2,kbin3 ="";

 int main()
 {

     string plaintext;
     string keytext;
     string ptbinary;
     string keybinary;
     string ciphertext;

     cout << "Enter the plaintext: ";
     cin >> plaintext;
     cout << "\nEnter the key: ";
     cin >> keytext;

     //testing values for xoring the strings together
     //plaintext = "010110";
     //keytext =   "000111";

    //for testing xoring two string together and printing the result
     //XorStrings(plaintext, keytext);
     //printXor();


     //decimal conversion of PT and K
     PTtoDec(plaintext);
     KtoDec(keytext);
     printKDecimal();
     printPTDecimal();


/*
     cout << "\ntesting ";
     cout << "\nvalue of pt o,o: " << PTdecimal[0][0];
     bin(PTdecimal[0][0]);
     ptbin0 = Bin;
     cout << "\nbin is:" <<Bin;

     Bin ="";
     bin(PTdecimal[0][1]);
     ptbin1 = Bin;

     Bin="";
     bin(PTdecimal[0][2]);
     ptbin2 = Bin;

     Bin="";
     bin(PTdecimal[0][3]);
     ptbin3 = Bin;
*/
    return(0);
 }
 //010001

string XorStrings(string p, string k)
{
    string x;
    for (int i=0; i < k.size(); i++ )
    {
        if ((p[i] == '1' && k[i] == '0')||(p[i]=='0' && k[i]=='1'))
        {
            x += '1';
            //X[i] = '1';
            cout << "\nResult is a 1";
        }
        else
        {
           x += '0';
           //X[i] = '0';
            cout << "\nResult is a 0";
        }
    }
    return(x);
}

/*
void printXor()
{
    cout << "\nThe string x is: " << X;
}
*/

//turns binary back into decimal
int binToDec(string s)
{
    int value;
        if (s[0] = '1')
            value += 32;
        if (s[1] == '1')
            value += 16;
        if (s[2] == '1')
            value += 8;
        if (s[3] == '1')
            value += 4;
        if (s[4] == '1')
            value += 2;
        if (s[5] == '1')
            value += 1;

    return (value);
}

void PTtoDec(string p)
{
    for (int i=0;i<p.size();i++)
    {
        int value = p[i];
        PTdecimal[0][i] = value;
    }
}

void KtoDec(string o)
{
    for (int i=0;i<o.size();i++)
    {
        int value = o[i];
        Kdecimal[0][i] = value;
    }
}

void bin(int n)
{
    //ptbin="";
	int r;

	if(n <= 1)
        {
		//cout << n;
		if (n==1)
            Bin += '1';
        else
            Bin += '0';
		return;
        }

	r = n%2;
	bin(n >> 1);
	//cout << r;
	if (r==1)
            Bin += '1';
        else
            Bin += '0';
}

void printPTDecimal()
{
    for (int c=0;c<=3;c++)
    {
        cout << "Ptdecimal value 1: ";
        cout << PTdecimal[0][c] << " ";
    }
    cout << endl;
}

void printKDecimal()
{
    for (int c=0;c<=3;c++)
    {
        cout << "Kdecimal value 1: ";
        cout << Kdecimal[0][c] << " ";
    }
    cout << endl;
}
> its now freezing no matter what i do and i can't tell why...

The code shouldn't even compile. We cant have arrays of zero size.
http://coliru.stacked-crooked.com/a/609264fccce790a5

There are other logical errors too. For instance:
http://coliru.stacked-crooked.com/a/f51fe3b750152eb8

Turn on all warnings (-Wall -Wextra on clang++ and g++),
turn off all linuxisms (or microsoftisms) that are not C++ (-pedantic-errors on clang++ and g++),
and pay attention to the diagnostic messages emitted by the compiler.

The three primitive operations:

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
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <iomanip>

bool is_binary_string( std::string str )
{
    // if any character other than '0' or '1' is present, it is not a binary string
    for( char c : str ) if( c != '0' && c != '1' ) return false ;

    return true ;
}

std::string to_binary_string( unsigned long long n )
{
    if( n == 0 ) return "" ;

    else return to_binary_string(n/2) + ( (n%2) ? '1' : '0' ) ;
    // to_binary_string(29) == to_binary_string(14) + '1'
    // to_binary_string(28) == to_binary_string(14) + '0'
}

unsigned long long to_number( std::string binary_str )
{
    if( binary_str.empty() || !is_binary_string(binary_str) ) return 0 ;

    // first n-1 binary digits
    const std::string head( binary_str.begin(), binary_str.end() - 1 ) ; //
    const char tail = binary_str.back() ; // last char

    // to_numer( "100101" ) == to_numer( "10010" /*head*/ ) * 2 + ( 1 /*tail*/ )
    // to_numer( "100100" ) == to_numer( "10010" /*head*/ ) * 2 + ( 0 /*tail*/ )
    return to_number(head) * 2 + ( tail == '1' ) ;
}

std::string xor_binary_strings( std::string a, std::string b )
{
    if( !is_binary_string(a) || !is_binary_string(b) ) return "" ;

    // make the strings of the same size (pad zeroes on the left)
    while( a.size() < b.size() ) a = '0' + a ;
    while( b.size() < a.size() ) b = '0' + b ;

    std::string xor_result ;
    for( std::size_t i = 0 ; i < a.size() ; ++i )
        xor_result += a[i] == b[i] ? '0' : '1' ; // '0' if same, '1' if different

    return xor_result ;
}

int main() // minimal test driver
{
    const unsigned long long n = 123456789012345 ;
    std::cout << n << '\n'
               << to_binary_string(n) << '\n'
               << to_number( to_binary_string(n) )
               << "\n----------------------------\n" ;

    const std::string binary = "110111100000010100011000101110101111101101101110111000110100" ;
    std::cout << binary << '\n'
               << to_number(binary) << '\n'
               << to_binary_string( to_number(binary) )
               << "\n----------------------------\n" ;

    const std::string a = "1100110011001100110011001100110011001100" ;
    const std::string b = "101010101010101010101010101010101010101010101010" ;
    std::cout << std::setw(50) << a << '\n'
               << std::setw(50) << b << '\n'
               << std::setw(50) << xor_binary_strings(a,b) << '\n'
               << to_number(a) << '\n'
               << to_number(b) << '\n'
               << ( to_number(a) ^ to_number(b) ) << '\n'
               << to_number( xor_binary_strings(a,b) ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/84be6618830f5941
Topic archived. No new replies allowed.