Transforming a string into ASCII

Transforming a string into ASCII code using int() and the 'while' loop. Good for beginners in programming and C++.


ASCII = computer data exchange standard: a standard that identifies the letters of the alphabet, numbers, and various symbols by code numbers for exchanging data between different computer systems.
Full form American Standard Code for Information Interchange

Microsoft® Encarta® Reference Library 2003. © 1993-2002 Microsoft Corporation. All rights reserved.

Every character has a different number under which it is stored on your computer. There are 256 different ASCII codes (the possible values a byte can hold), that is why a character takes one byte.


// Transform a word into ASCII code

#include <iostream>
using namespace std;
int main()
{
    char word[32];
    int x = 0;
    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...
    {
        cout << int(word[x]);    // Transform the char to int
        x++;
    }
    cout << "\n";
    return 0;
}


With ‘while’ we loop trough the array and convert every character using int(word[x]) to its ASCII value. If we hit the ‘\0’ (end of the string), the while loop returns false and ends.

There is more to say about ASCII than I said here… Google can help you 😉 .

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top