Types and Literals

How Computers Store Text

In the Memory section, you mention bits can be used to represent characters. How can 0s and 1s represent letters?

3D art representing computer programming

Computers use something called ASCII (American Standard Code for Information Interchange) to convert letters into numbers, which can then be stored as bits. Think of it like a secret code where each letter is assigned a specific number.

For example, in ASCII:

  • The letter 'A' is represented by the number 65
  • The letter 'B' is represented by 66
  • The letter 'a' is represented by 97
  • The letter 'b' is represented by 98

Here's a program that shows this in action:

#include <iostream>
using namespace std;

int main() {
  char Letter{'A'};
  cout << "The letter " << Letter
    << " is stored as number: "
    << static_cast<int>(Letter) << '\n';

  char NextLetter{'B'};
  cout << "The letter " << NextLetter
    << " is stored as number: "
    << static_cast<int>(NextLetter) << '\n';

  // We can also go backwards - storing
  // a number creates a letter
  char LetterFromNumber{97}; // This creates 'a'
  cout << "The number 97 represents the letter: "
    << LetterFromNumber << '\n';
}
The letter A is stored as number: 65
The letter B is stored as number: 66
The number 97 represents the letter: a

Once we have these numbers, we can convert them to bits. For example, the letter 'A' (65 in decimal) is 01000001 in binary. Each character typically uses one byte (8 bits) of memory.

This system works for more than just letters. ASCII includes codes for numbers, punctuation marks, and special characters. For example:

  • The space character is 32
  • The exclamation mark (!) is 33
  • The number character '0' is 48

Modern computers often use more advanced systems like UTF-8 (which can represent characters from many different languages), but the basic idea is the same - convert characters to numbers, then store those numbers as bits.

This Question is from the Lesson:

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved