Skip to content
Tutorials Center
My learning

Topics

Lesson 97 of 146 ยท Standard Library and Foundation

Data and Bytes

Work with raw bytes in Swift - the Data type, UInt8 arrays, UTF-8, Base64 and hex encoding, endianness, bit operations, safe unsafe-pointer access and a small binary packet format.

Underneath every file, network message, image and password hash is the same thing: a sequence of bytes, each a number from 0 to 255. Text is just one interpretation of those bytes. When you read a file, call a server or talk to a device, you first receive bytes and must decide what they mean.

Swift gives you two main ways to hold bytes. The standard library has [UInt8], an ordinary array of unsigned 8-bit integers. Foundation adds Data, a value type built for byte buffers that interoperates with files, networking and encoders. This lesson covers both, the encodings you meet every day (UTF-8, Base64, hexadecimal), the difference between big- and little-endian numbers, bit manipulation, and a careful first look at raw memory access. It builds on files, basic types and numbers.

Data: a collection of bytes

Data is a collection of UInt8, so almost everything you know about arrays works: count, indexing, append, map, slicing, iteration. It is a value type with copy-on-write storage, like Array, so copies are cheap and independent.

Swift Creating and changing Data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

print(data) shows 3 bytes, not the contents, which keeps logs short; convert with Array(data) to see the numbers. After the appends the array is [72, 105, 33, 10, 1, 2, 195, 169]. 0x48 0x69 0x21 is the ASCII text Hi!. The word hรฉllo has five characters but six UTF-8 bytes, because รฉ takes two (195, 169). That is the key fact about text: characters and bytes are different counts. The last line turns every byte into two hexadecimal digits with String(format: "%02x", ...), giving 4869210a0102c3a9.

You can create Data from an array literal, from a string's utf8 view, from the contents of a file or URL, or as a block of zeros or a repeated byte:

Swift
let zeros = Data(count: 4)               // 4 bytes, all 0
let filled = Data(repeating: 0xAB, count: 3)

Value semantics and slice indices

Because Data is a value, assigning it copies it logically, and changing one copy leaves the other alone. Slices behave differently from what newcomers expect, and this is the most common source of crashes with Data: a slice keeps the indices of the original.

Swift Slices keep the original indices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Changing changed[0] does not touch original. The slice original[2..<5] still counts from 2: startIndex is 2, endIndex is 5, and slice[2] is 3. Wrapping it in Data(slice) makes a fresh buffer that starts at index 0 again. Searching with range(of:) and firstIndex(of:) returns indices in the original numbering (2..<4 and 4). Copying a slice with Data(...) is the safe habit whenever you pass a part of a buffer to other code.

Here is the crash that follows when you forget. The program asks for index 0 of a slice that starts at 2:

Swift Indexing a slice from zero (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

The program is killed with SIGILL and a backtrace pointing into Data._Representation.subscript.getter. There is no friendly message, because the build uses optimizations and an out-of-range index is a trap. Use tail.first, tail[tail.startIndex], or copy the slice with Data(tail).

Bytes and text

A String becomes bytes with Data(string.utf8), and bytes become a String in two ways, which differ in how they treat invalid data:

Swift
let bytes = Data([0xFF, 0xFE, 0x41])
String(data: bytes, encoding: .utf8)         // nil: not valid UTF-8
String(decoding: bytes, as: UTF8.self)       // "๏ฟฝ๏ฟฝA": bad bytes replaced

Use the failable form when invalid input should be rejected, for example when parsing a protocol. Use String(decoding:as:) when you want to display something regardless. UTF-8 is the only encoding you should choose for new data; other encodings, such as .isoLatin1, exist for old files, and decoding with the wrong one gives mojibake (hรฉllo read as Latin-1 becomes hรƒยฉllo).

Base64 and hexadecimal

Raw bytes cannot go in places that expect plain text, such as JSON, URLs, email and logs. Base64 solves this: it encodes every three bytes as four characters from a safe alphabet. Hexadecimal spells every byte as two characters and is the usual way to show hashes, keys and memory dumps.

Swift Base64 and hex round trips

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Hello, Swift! becomes SGVsbG8sIFN3aWZ0IQ==, and decoding returns the text. Decoding garbage gives nil, never a crash. The input SGk is also nil: Data(base64Encoded:) requires the = padding, and SGk= decodes to Hi. The bytes FB FF FE encode to +//+ in standard Base64, but + and / are special in URLs, so the URL-safe form uses - and _ and prints -__-. Base64 is an encoding, not encryption: anyone can decode it.

The hex helpers print 48656c6c6f2c20537769667421, parse back to equal Data, and reject odd-length text (abc) and non-hex digits (zz) with nil. Foundation has no built-in hex converter, so almost every project carries a small helper like this one. Asking for one that does not exist is a classic compile error:

TEXT
error: value of type 'Data' has no member 'hexString'

Integers, bits and endianness

Bytes often carry numbers wider than 8 bits. A 32-bit integer occupies four bytes, and there are two ways to order them. Big-endian puts the most significant byte first, the way we write numbers, and is the standard for network protocols. Little-endian puts the least significant byte first and is what x86 and ARM processors use in memory.

Swift Endianness and byte order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

On this machine (x86-64 Linux, little-endian) value.littleEndian == value is true and value.bigEndian == value is false. The same number in memory as little-endian is [4, 3, 2, 1] and as big-endian is [1, 2, 3, 4]. The wire bytes 00 00 01 2C mean 300 in big-endian; shifting and OR-ing them gives 300, while loading the memory directly gives 738263040 until UInt32(bigEndian:) fixes the order and returns 300. Writing UInt16(513).bigEndian produces [2, 1], because 513 is 0x0201.

Always state the byte order in a file format or protocol and convert explicitly with bigEndian, littleEndian or UInt32(bigEndian:). Code that works on your little-endian laptop and silently breaks on another platform is exactly the bug this prevents.

Fitting numbers into bytes

Converting between integer sizes is a place where Swift insists you say what you mean. A literal that does not fit does not compile:

Swift A literal too big for UInt8 (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

TEXT
main.swift:1:20: error: integer literal '300' overflows when stored into 'UInt8'

A runtime value that does not fit traps: UInt8(someInt) with 300 ends the program with SIGILL. Choose the conversion that matches your intent:

Swift Choosing the right conversion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

You see 44 (300 is 0x12C, and the low byte is 0x2C), 255, and nil. The flag byte prints as 101 and FF; bit 2 is set (true), setting bit 1 gives 7, flipping gives 250, shifting left by 2 gives 20, right by 1 gives 2, and the byte has 2 set bits and 5 leading zeros. The shifting operators on fixed-size integers do not trap on overflow; they drop bits.

Raw memory access: use with care

Sometimes you must view a buffer as raw memory, for example to read a number out of a byte buffer, as in the previous example. withUnsafeBytes hands you an UnsafeRawBufferPointer that is only valid inside the closure. Never store the pointer or return it; the buffer may move or be freed afterwards.

Swift
data.withUnsafeBytes { (raw: UnsafeRawBufferPointer) in
    print(raw.count, raw[3])          // bytes, bounds-checked in debug builds
}

loadUnaligned(as:) reads a value without requiring alignment, which is what you want for bytes from a file or socket. Plain load(as:) traps if the address is not aligned. The Unsafe Swift lesson covers pointers in depth; for byte formats, prefer shifts and UInt32(bigEndian:) as shown above, which are safe and portable.

A small binary format

Everything so far combines into a tiny protocol: two magic bytes, a version, a 16-bit id, a 32-bit payload length (big-endian), and then the payload. Encoding is straightforward. Decoding is where you must distrust the input: check lengths before you read and report problems with an error, not a crash.

Swift Encoding and decoding a binary packet

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

The encoded packet is 53 57 01 01 02 00 00 00 03 68 69 21: magic, version 1, id 01 02, length 00 00 00 03 and the three payload bytes. Decoding it gives back an equal packet. The three damaged inputs produce tooShort, badMagic and truncated(expected: 3, got: 2). Notice that the decoder converts to an [UInt8] first, so indices start at zero and cannot be thrown off by a Data slice.

For structured data that others must read, prefer a standard format. JSON with Codable covers the common case; a hand-written binary layout is for protocols and files that are already defined that way.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The string "hรฉllo" has 5 characters. How many UTF-8 bytes does it have?

    รฉ needs two bytes in UTF-8, so the string is 6 bytes long.
  2. 2. You take let tail = data[2...]. What is the first valid index of tail?

    Use tail.startIndex, tail.first, or copy the slice with Data(tail) to restart at zero.
  3. 3. What is Base64?

    Base64 only makes bytes safe to place in text. It provides no secrecy.
  4. 4. On a little-endian machine, what are the memory bytes of UInt32(0x01020304)?

    Little-endian stores the least significant byte first. bigEndian gives [1, 2, 3, 4].
  5. 5. Which initializer returns nil instead of crashing when a value does not fit in a UInt8?

    exactly: returns an optional, clamping: saturates, truncatingIfNeeded: keeps the low bits, and plain UInt8(_:) traps.

Summary

  • Bytes are UInt8 values. Data (Foundation) and [UInt8] hold them; Data is a value type that works directly with files and encoders.
  • A character is not a byte: UTF-8 uses 1 to 4 bytes per character. String(data:encoding:) returns nil for invalid UTF-8, String(decoding:as:) repairs it.
  • Slices of Data keep the original indices; copy with Data(slice) or use startIndex to avoid out-of-range traps.
  • Base64 turns bytes into safe text (not encryption) and needs correct padding; hex needs a small helper you write yourself.
  • Declare the byte order of your formats and convert with bigEndian, littleEndian and UInt32(bigEndian:).
  • Choose the integer conversion by intent: truncatingIfNeeded, clamping, exactly, or the trapping default.
  • Treat pointer access (withUnsafeBytes) as short-lived and use shifts for portable parsing; validate lengths before reading.

Next: Working with JSON turns structured values into bytes and back with Codable and JSONSerialization.