Int to byte array

Update for 2020 – BinaryPrimitives should now be preferred over BitConverter. It provides endian-specific APIs, and is less allocatey. byte[] bytes = BitConverter.GetBytes(i); although note also that you might want to check BitConverter.IsLittleEndian to see which way around that is going to appear! Note that if you are doing this repeatedly you might want to … Read more

Using a byte array as Map key

It’s okay so long as you only want reference equality for your key – arrays don’t implement “value equality” in the way that you’d probably want. For example: byte[] array1 = new byte[1]; byte[] array2 = new byte[1]; System.out.println(array1.equals(array2)); System.out.println(array1.hashCode()); System.out.println(array2.hashCode()); prints something like: false 1671711 11394033 (The actual numbers are irrelevant; the fact that … Read more

Put byte array to JSON and vice versa

Here is a good example of base64 encoding byte arrays. It gets more complicated when you throw unicode characters in the mix to send things like PDF documents. After encoding a byte array the encoded string can be used as a JSON property value. Apache commons offers good utilities: byte[] bytes = getByteArr(); String base64String … 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

Storing UUID as base64 String

I was also trying to do something similar. I am working with a Java application which uses UUIDs of the form 6fcb514b-b878-4c9d-95b7-8dc3a7ce6fd8 (which are generated with the standard UUID lib in Java). In my case I needed to be able to get this UUID down to 30 characters or less. I used Base64 and these … Read more

When to use []byte or string in Go?

My advice would be to use string by default when you’re working with text. But use []byte instead if one of the following conditions applies: The mutability of a []byte will significantly reduce the number of allocations needed. You are dealing with an API that uses []byte, and avoiding a conversion to string will simplify … Read more

How to create bitmap from byte array?

You’ll need to get those bytes into a MemoryStream: Bitmap bmp; using (var ms = new MemoryStream(imageData)) { bmp = new Bitmap(ms); } That uses the Bitmap(Stream stream) constructor overload. UPDATE: keep in mind that according to the documentation, and the source code I’ve been reading through, an ArgumentException will be thrown on these conditions: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)