Lesson 25. Working with binary data

 

After reading lesson 25, you’ll be able to

  • Use the ByteString type to efficiently work with binary data
  • Treat ByteStrings as regular ASCII strings by using ByteString.Char8
  • Glitch JPEG images by using Haskell
  • Work with binary Unicode data

In this lesson, you’ll learn about working with binary file data by using Haskell’s ByteString type. ByteString allows you to treat raw binary data as though it were a regular string. To demonstrate the use of ByteString, you’ll focus on a fun project requiring you to manipulate binary file data. You’ll create a simple command-line tool that will allow you to create glitch art, like that in figure 25.1.

Figure 25.1. A scene from Michael Betancourt’s glitch art video “Kodak Moment” (2013)

Glitch art is the practice of deliberately corrupting binary data in order to create visual artifacts in an image or video. You’ll work on the relatively simple task of “glitching” JPEG images. You’ll also take a look at some of the issues around working with binary Unicode data.

Consider this

You have the name of the Japanese author Tatsuhiko Takimoto represented in Japanese Kanji using T.Text:

tatsuhikoTakimoto :: T.Text
tatsuhikoTakimoto = ""

You need to know the number of bytes in this text. For ASCII text, this would be the length of the text, but in this case, using T.length gives you only the number of characters (5). How can you find the number of bytes?

25.1. Working with binary data by using ByteString

25.2. Glitching JPEGs

25.3. ByteStrings, Char8, and Unicode

Summary