₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,164 members, 8,510,476 topics. Date: Friday, 30 October 2026 at 01:02 PM

Toggle theme

Programming - Programming - Nairaland

Nairaland Forum › Science/Technology › Programming › Programming (872 Views)

1 Reply (Go Down)

Programming by caroline2120(op): 4:49pm On May 23, 2013
please i want to convert string to ascii code and vice versa. e.g "ade" to ascii code and back to "ade"
Re: Programming by simplyme3(f): 7:29pm On May 23, 2013
#include <iostream.h>
int main()
{
char word[32];
int x = 0;
[B]int ascii[32];[/B]
cout << "Please enter the word (maximum 32 characters):\n";
cin >> word;
cout << "The ASCII for this word is:\n";
while (word[x] != '\0') // While the string isn't at the end...
{
[B]ascii[x] = int(word[x]); // Transform the char to int[/B]
x++;
}
[B] //Now, to display the array with the asciis...
for (x=0; x<32; x++) {
cout << ascii[x] << ' ';
}[/B]
cout << "\n";
return 0;
}
Re: Programming by WhiZTiM(m):
[quote author=simply_me]#include <iostream.h>
int main()
{
char word[32];
int x = 0;
[B]int ascii[32];[/B]
cout << "Please enter the word (maximum 32 characters):\n";
cin >> word;
cout << "The ASCII for this word is:\n";
while (word[x] != '\0') // While the string isn't at the end...
{
[B]ascii[x] = int(word[x]); // Transform the char to int[/B]
x++;
}
[B] //Now, to display the array with the asciis...
for (x=0; x<32; x++) {
cout << ascii[x] << ' ';
}[/B]
cout << "\n";
return 0;
}[/quote]good one...

But err. . . Madam, printing an array of 32 ints when the user's input only initializes less than 32 of them will print garbaged or max int.

Your code is a C styled code in C++... I discourage that.

Why don't you use the string class... Feed the string, then use the string.size() to instantiate the array ...convert to ascii like you did ...and loop over the int array using the string.size() as boundary ? ? ...its much cleaver that way. cool
...btw, I feel humbled whenever I see a ....ehhmm... What was I saying again? ? ..cool
Re: Programming by petersmith5544(m): 1:47pm On May 25, 2013
Programming is instructing a computer to do something for you with the help of a programming language. The role of a programming language can be described in two ways:

Technical: It is a means for instructing a Computer to perform Tasks
Conceptual: It is a framework within which we organize our ideas about things and processes.
1 Reply

• 2 • 3 • 4

Pls Help Me Is Ugent • Update Database Via Wlan • How Do I Read Through Huge PHP Code Samples?