How to convert between bytes and strings in Python 3?

The ‘mangler’ in the above code sample was doing the equivalent of this: bytesThing = stringThing.encode(encoding=’UTF-8′) There are other ways to write this (notably using bytes(stringThing, encoding=’UTF-8′), but the above syntax makes it obvious what is going on, and also what to do to recover the string: newStringThing = bytesThing.decode(encoding=’UTF-8′) When we do this, the … Read more

Why is the range of bytes -128 to 127 in Java?

The answer is two’s complement. In short, Java (and most modern languages) do not represent signed integers using signed-magnitude representation. In other words, an 8-bit integer is not a sign bit followed by a 7-bit unsigned integer. Instead, negative integers are represented in a system called two’s complement, which allows easier arithmetic processing in hardware, … Read more

When to use byte array & when byte buffer?

There are actually a number of ways to work with bytes. And I agree that it’s not always easy to pick the best one: the byte[] the java.nio.ByteBuffer the java.io.ByteArrayOutputStream (in combination with other streams) the java.util.BitSet The byte[] is just a primitive array, just containing the raw data. So, it does not have convenient … Read more

Byte Array in Python

In Python 3, we use the bytes object, also known as str in Python 2. # Python 3 key = bytes([0x13, 0x00, 0x00, 0x00, 0x08, 0x00]) # Python 2 key = ”.join(chr(x) for x in [0x13, 0x00, 0x00, 0x00, 0x08, 0x00]) I find it more convenient to use the base64 module… # Python 3 key … Read more

What is the maximum number of bytes for a UTF-8 encoded character?

The maximum number of bytes per character is 4 according to RFC3629 which limited the character table to U+10FFFF: In UTF-8, characters from the U+0000..U+10FFFF range (the UTF-16 accessible range) are encoded using sequences of 1 to 4 octets. (The original specification allowed for up to six byte character codes for code points past U+10FFFF.) … Read more

Write bytes to file

If I understand you correctly, this should do the trick. You’ll need add using System.IO at the top of your file if you don’t already have it. public bool ByteArrayToFile(string fileName, byte[] byteArray) { try { using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { fs.Write(byteArray, 0, byteArray.Length); return true; } } catch (Exception ex) … Read more