Since some of your previous posts asked about C++ issues, I submit
the following C++ code for your consideration.
Note that the ASCII value of the character 'A' is 65 (decimal).
This bit pattern ("01000001") is stored in char_array[0] by
the initialization of char_array[].
The first "for" loop copies the numerical values to an array
of integers. The numerical value stored in int_array[0] is
the same as that in char_array[0]. The exact way that the
values are stored are (usually) not important to us, as long
as we can get the stuff in and out in a deterministic fashion.
The output statement with cout and char_array[] converts the
internal numerical value to whatever is required to display
the ASCII equivalent of this bit pattern.
The output statement with cout and int_array[] converts the (same)
internal numerical value to whatever is required to display
the decimal integer value of this bit pattern.
These output statements do not change the value of anything in
memory, they convert to whatever representation is defined
in the program.
Code:
#include <iostream>
using namespace std;
int main()
{
char char_array[ ] = "Ahmed"; // five characters and terminating '\0'
int int_array[6];
int i;
for (i = 0; i < 5; i++) { // copy the characters to integer
int_array[i] = char_array[i];
}
cout << "Here are the contents of char_array[]" << endl;
for (i = 0; i < 5; i++) { //cout shows contents, assuming they are chars
cout <<"<"<<char_array[i]<<">";
}
cout << endl << endl;
cout << "Here are the contents of int_array[]" << endl;
for (i = 0; i < 5; i++) { //now show integer values
cout <<"<"<<int_array[i]<<">";
}
cout << endl << endl;
cout << "Here are the contents of char_array[], using (int) cast"
<< endl;
for (i = 0; i < 5; i++) {// contents haven't changed; show integer equivalents
cout <<"<"<<(int)char_array[i]<<">";
}
cout << endl << endl;
cout << "Here are the contents of int_array[], using (char) cast"
<< endl;
for (i = 0; i < 5; i++) {// now feed cout with char
cout <<"<"<<(char)int_array[i]<<">";
}
cout << endl << endl;
return 0;
}
Dave