Bytes bytes bytes 🐶 🐶 in Rust and other languages

Bytes bytes bytes 🐶 🐶 in Rust and other languages

I don’t know why for years it had been difficult for me to deal with bytes in Rust and probably for no reason

Bytes are great! They allow interoperability and all sort of things in data transmission on the Internet. In many languages, there are primitives allowing to convert data to bytes or bytes to text to allow serialization et deserialization.

Let’s say you want to process some data, say “hello world” text or the number 1000. In order for the computer to be able to handle them, we need data types. This is where we will have string of characters for “hello world” and integer for the number 1000. But is it possible to have a common data type that can carry both “hello world” and 1000 ?

The answer is yes : a string of bytes or an array of bytes.

You know ASCII ? It doesn’t matter. If you don’t know it, please go look it up.

⌨️⌨️⌨️⌨️⌨️ <= Going on ChatGPT or Google to look for ASCII

Okay – you now know ASCII and they told you “a” is 97. Nice!!

As we only have 255 8-bits or bytes number, it’s difficult to represent all UTF-8 characters. To enlarge our range of possibilities, we can represent each character with two bytes.

So, we will have our string bytes as follows

[[0, 97], [0, 97], [97, 97]], …

This is an array of arrays of bytes.

What can we do with bytes?

They are numbers : we can perform addition. Is it all : yes – addition is everything. Substruction is simply adding the opposite number, multiplication is just addition as well as division.

With that said, we can encrypt data, we can compress data, we can transfer data and read them on another environment or programming language nd you can be happy! By the way, be happy! Always.

Does the order count?

Yes – it does : if you decrypt from left to right the string of bytes, you won’t read the same thing as the one reading from right to left. For thus, human(maverlous animals) have create low endian and big endian bytes reading possibilities. Low endian reads from right to left and big endian reads from left to right.

In Rust, I struggled with bytes though they are simple

Before talking about Rust, did you know that in Go, converting bytes has a weird name “marshaling” and “unmarshaling”. I stop you before you say something. I know I might be crazy saying this name is weird but who in the world using Marshal law to talk about bytes conversion. Wtf coined this term?

To convert “hello world” to bytes, simply add a b before “hello world”.

e.g let my_bytes = b”hello world”;

With the bytes “my_bytes”, do what ever you want : iterate add 12 to the numbers, send the data to another computer : decrypt it it and be happy. You have completed something even though I feel asleep. I need some rest even though I don’t know what I did today.

Cools guys, you know where to find me.

Featured image by “Photo by Crina Doltu: https://www.pexels.com/photo/cat-biting-persons-finger-1202481/”

Posted by elielmathe