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
    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 😉 .

Leave a Reply

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

Back To Top