DEV Community

RexTora
RexTora

Posted on

Day 03: Data & Representation

🎯 Learning Objectives

By the end of this article, you will understand:

  • How computer storage scales up from a single letter to massive database drives.
  • How Hexadecimal and Binary formatting are used to write IP addresses and website colors.
  • How Base64 safely packages image and media data into text format for network traffic.
  • How computers store text, foreign languages, and emojis without messing up.
  • Why you should never use simple decimal formats for calculations handling money.

1. The Foundation: Bits, Bytes & Data Scale

  • Bit: The absolute smallest piece of data. It is just a single switch that can be 1 (On) or 0 (Off).
  • Byte: A group of 8 bits. This is the standard building block of data. One single Byte holds exactly one normal character of text (like the letter A, the number 7, or a blank space).
  • The Storage Scale: As a developer, you will manage file uploads, database sizes, and server memory every day. Data sizes scale up by multiplying by 1,024 at each step:
 1 Bit  ──────────────►  [ 0 ] or [ 1 ] (A single binary switch)
   β”‚
   β–Ό (x8)
 1 Byte ─────────────►  8 Bits (Stores exactly 1 character, like 'A')
   β”‚
   β–Ό (x1,024)
 1 Kilobyte (KB) ────►  1,024 Bytes (A tiny text file or backend config file)
   β”‚
   β–Ό (x1,024)
 1 Megabyte (MB) ────►  1,024 Kilobytes (A website image asset or code file)
   β”‚
   β–Ό (x1,024)
 1 Gigabyte (GB) ────►  1,024 Megabytes (The memory limit of a standard web server)
   β”‚
   β–Ό (x1,024)
 1 Terabyte (TB) ────►  1,024 Gigabytes (A massive cloud drive holding millions of users)

Enter fullscreen mode Exit fullscreen mode

πŸ’» Memory vs. Storage: RAM (Memory) is the fast, temporary desktop where your running apps hold active data right now. SSD (Storage) is the permanent filing cabinet where files sleep when the computer or app is turned off.

  • Real-World Scale Example: Think of a simple backend app. A user's text profile name is only a few Bytes. Their profile picture asset is around 2 MB. The entire active application runs inside a server container with 4 GB of RAM memory, and all user history is safely backed up on a 5 TB cloud storage database drive.

2. Hexadecimal (Base-16) & IP Addresses

  • Hexadecimal: A counting shorthand that uses numbers 0-9 and letters A-F (A=10, B=11, C=12, D=13, E=14, F=15).
  • Why it matters: Pure binary strings are too long for humans to read (11111010), and normal numbers don't match computer memory structures cleanly. Exactly two Hex characters represent 1 complete Byte (0xFA).
Binary System:   β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”   β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
                 β”‚ 1 β”‚ 1 β”‚ 1 β”‚ 1 β”‚   β”‚ 1 β”‚ 0 β”‚ 1 β”‚ 0 β”‚
                 β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜   β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
                       β”‚                   β”‚
Hex Shorthand:         β–Ό                   β–Ό
                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                 β”‚       F       β”‚   β”‚       A       β”‚ ──► Written as `FA` or `0xFA`
                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Enter fullscreen mode Exit fullscreen mode

🌐 Real-World Example: Network IP Addresses

Every server and phone connected to the internet needs an address. We use two main types depending on the network protocol:

  • IPv4: The older, classic address style. It is made of 4 bytes separated by dots. Each section is a single byte, so it can only count from 0 to 255.
  • Real-World Address Example: 192.168.1.254 (Your local home router address).

  • IPv6: The modern address system because the world ran out of IPv4 options. It is 16 bytes long. Because writing that out in normal numbers would be way too long, it uses Hexadecimal separated by colons.

  • Real-World Address Example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 (A standard cloud server destination address).

🎨 Other Places You See Hex:

  • CSS Colors: Web designs use pairs of Hex characters to set Red, Green, and Blue values. For example, #FF5733 targets precise color channel bytes on a display.
  • Database IDs: Unique tracking strings (UUIDs) like de305d54-75b4-431b-adb2-eb6b9e546013.

3. Base64 Encoding

  • What It Is: A system that turns raw binary files (like images or PDFs) into a long string of safe text characters (A-Z, a-z, 0-9, +, /).
  • Why It Exists: Web systems (like emails or HTML pages) were built for plain text. Raw media files can get corrupted over these channels. Base64 masks files as safe text so they can travel across networks without breaking.
  • How It Works: While Hex uses groups of 4 bits, Base64 splits data into 6-bit blocks. Each block maps to one text character. If the final data chunk doesn't fit perfectly, it adds = signs at the end as padding.
Raw Bytes (24 bits):  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚    Byte 1     β”‚ β”‚    Byte 2     β”‚ β”‚    Byte 3     β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚                 β”‚                 β”‚
Split into 6 bits:    β”Œβ”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”
                      β”‚6bitsβ”‚     β”‚6bitsβ”‚     β”‚6bitsβ”‚     β”‚6bitsβ”‚
                      β””β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”˜
                         β”‚           β”‚           β”‚           β”‚
Base64 Text String:   [  S  ]     [  G  ]     [  V  ]     [  s  ]  ──► "SGVs"

Enter fullscreen mode Exit fullscreen mode
  • Where You See It: Embedding tiny logo graphics directly into HTML code to avoid extra server downloads (<img src="data:image/png;base64,iVBORw..." />) or packaging secure web tokens (JWTs).
  • The Penalty: Base64 makes files 33% larger than the original binary. Never use it for large files like video uploads, as it wastes network bandwidth. Use cloud buckets instead.

4. Character Encoding (Text & Emojis)

  • ASCII: An old system that only maps English letters and numbers to data values (0 to 127). It breaks completely if you try to use accents, Asian scripts, or symbols.
  • Unicode: A giant global dictionary that assigns a specific ID number (called a Code Point) to every language letter, character, and emoji on Earth.
  • UTF-8: The smart system that saves these characters to your drive efficiently. It scales up the storage size dynamically based on what you type:
  • Standard English text uses 1 Byte.
  • European alphabets use 2 Bytes.
  • Complex Asian symbols use 3 Bytes.
  • Graphical Emojis use 4 Bytes.
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ UTF-8 FILE SIZE SAVING SYSTEM                               β”‚
β”‚                                                             β”‚
β”‚  β”œβ”€β”€ Character: 'A'      ──► Uses: β”Œβ”€β”€β”€β”                    β”‚
β”‚  β”‚                                 β”‚1B β”‚                    β”‚
β”‚  β”‚                                 β””β”€β”€β”€β”˜                    β”‚
β”‚  β”œβ”€β”€ Character: 'Ξ©'      ──► Uses: β”Œβ”€β”€β”€β”¬β”€β”€β”€β”                β”‚
β”‚  β”‚                                 β”‚1B β”‚1B β”‚                β”‚
β”‚  β”‚                                 β””β”€β”€β”€β”΄β”€β”€β”€β”˜                β”‚
β”‚  └── Character: 'πŸ”₯'     ──► Uses: β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”        β”‚
β”‚                                    β”‚1B β”‚1B β”‚1B β”‚1B β”‚        β”‚
β”‚                                    β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Enter fullscreen mode Exit fullscreen mode
  • Real-World Storage Example: If you write a chat system database backend and save the text "Hello! πŸ”₯", the "Hello!" section consumes 6 bytes of disk space, while the single "πŸ”₯" emoji immediately takes up 4 full bytes on its own.

5. Endianness: How Hardware Reads Bytes

When an application handles data blocks that use multiple bytes, different CPU hardware layouts write them down in different directions:

  • Big-Endian: Writes data from left to right (the way humans read).
  • Real-World Example: The Internet. Network systems use Big-Endian so that routers read packet addresses uniformly.

  • Little-Endian: Writes data backwards, from right to left (the least important byte first).

  • Real-World Example: Your Computer. Intel, AMD, and Apple M-series chips use this layout internally because it lets the hardware process calculations faster.

Value to store: `0x12345678` (A 4-byte system tracking number)

                          Memory Address order ──► 
                       [0x00]     [0x01]     [0x02]     [0x03]
                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 BIG-ENDIAN:          β”‚    12    β”‚    34    β”‚    56    β”‚    78    β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 LITTLE-ENDIAN:       β”‚    78    β”‚    56    β”‚    34    β”‚    12    β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Enter fullscreen mode Exit fullscreen mode

6. Data Types & The Floating-Point Problem

  • Integers: Whole numbers without fractions.
  • Unsigned: Positive numbers only ($0$ to $255$ in 1 byte). Excellent for items like server port numbers (e.g., Port 80).
  • Signed: Uses the very first bit to track if a number is positive or negative. Used for values like temperature registers (-15).

  • Floating-Point (Floats): Numbers that use decimal points (like 10.5 or 0.23).

🚨 The $0.1 + 0.2 \neq 0.3$ Calculation Glitch

Computers count fractions using divisions of 2 ($\frac{1}{2}$, $\frac{1}{4}$, $\frac{1}{8}$), not divisions of 10. Because of this, computer chips cannot store values like 0.1 or 0.2 perfectly. They turn into infinite repeating numbers that get cut off, creating a tiny mathematical leak:

// Open your browser Console window and type this:
console.log(0.1 + 0.2); 

// The actual output result: 
0.30000000000000004

Enter fullscreen mode Exit fullscreen mode

⚠️ The Absolute Golden Rule: Never use regular Float data types to calculate or store money balances in an app. These fractional errors will compound over thousands of transactions, causing your database balances to drift. Always store currency values as whole integer cents (1000 instead of 10.00) or use specific precise tools like Decimal in Python.

  • Real-World Bug Example: Imagine an online store cart where an item costs $0.10 and another costs $0.23. If processed as loose float values, your checkout page calculation code might display a total price like $0.33000000000000007 to a confused customer.

βœ… Key Takeaways

✨ Data Scaling: Storage steps up cleanly in blocks of 1,024 (Byte ──► KB ──► MB ──► GB ──► TB).
✨ IP Addresses: IPv4 uses simple 4-byte decimal strings; modern IPv6 scales up to 16-byte Hexadecimal blocks to fit the whole internet.
✨ Base64 Protection: Base64 converts raw file bytes into safe text strings so text-based web channels can route media assets securely without packet damage.
✨ Text Control: Always ensure your database character collation is set to UTF-8, or emojis and non-English names will corrupt and crash your code.
✨ Financial Calculations: Keep money balances locked into whole integer cent values to avoid fraction bugs.


🏎️ Quick Review

  • Bits & Bytes: A bit is a single 1/0 position switch. A byte is a group of 8 bits that represents a single text character.
  • Hex vs. Base64: Hex uses 4 bits per character to map web colors and memory pointer slots cleanly. Base64 uses 6 bits per character to safely ship media files inside text layouts at a 33% file size penalty.
  • UTF-8: The standard internet layout that securely compresses text files while allowing emojis to function seamlessly.
  • Endianness: The structural direction a physical CPU chip reads data out of system memory rows.

🎯 30-Second "Elevator Pitch" Definitions

  • Data Scale: "1 Byte holds 8 bits. Every scale up to Kilobytes, Megabytes, and Gigabytes multiplies that space capacity by exactly 1,024."
  • Base64 Encoding: "A translation map that turns messy binary media files into standard text string letters so they can travel across text-only network channels without breaking."
  • The Float Bug: "Computers handle fractions using binary logic, meaning simple decimal numbers like 0.1 cause micro-rounding leaks that will corrupt currency math if you don't use integers."

Top comments (0)