Working with bytes in Dart

Suragch
25 min readDec 26, 2020

If you can understand the bytes, you can understand anything.

Preface

I started on this topic because I was researching how to communicate between Dart and a PostgreSQL database server. It turned out to be much more low-level than I had been expecting. I thought I’d write a short article explaining some of new things I was learning. Well, three full days of writing later, that short article has turned into one of the most in-depth explanations I’ve ever written on an already niche topic. Although the article is long, I don’t think you’ll find it boring, and there’s a good chance you learn one or two new things about Dart even if you’ve been using it for a while. I certainly did. As always, please let me know if you find any errors. That’s how I learn. And try the code examples out yourself. That’s how you learn.

This article is up to date for Dart 2.10.

Bits and bytes

Everyone reading this article knows that a byte is eight bits:

00101010

That 8-bit byte has a value, a value of 42 in this case, which is just an integer. Now combining that knowledge with the fact that all binary data is just a sequence of bytes, this means it’s possible to represent any binary data as a list of integers in Dart:

--

--