How Computers Store Text

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

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.

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.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Memory Usage in Modern Computers
Why do we need to care about memory usage in modern computers when they have so much RAM available?
Monitoring Program Memory Usage
How can I know how much memory my entire program is using? Is there a way to check this?
When to Use Small Integer Types
In the real world, when would I choose to use a smaller integer type like int8_t instead of just using regular int?
Uses for Unsigned Integers
If unsigned integers are problematic, why do they exist at all? Are there any real-world cases where they're useful?
Preventing Number Overflow
How can I check if my number will cause an overflow before performing a calculation?
Controlling Decimal Places
When I print floating point numbers, they sometimes show way more decimal places than I want. How can I control this?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant