Java: Converting String to and from ByteBuffer and associated problems

Check out the CharsetEncoder and CharsetDecoder API descriptions – You should follow a specific sequence of method calls to avoid this problem. For example, for CharsetEncoder: Reset the encoder via the reset method, unless it has not been used before; Invoke the encode method zero or more times, as long as additional input may be … Read more

Wrapping a ByteBuffer with an InputStream

There seem to be some bugs with the implementation referred to by Thilo, and also copy and pasted on other sites verbatim: ByteBufferBackedInputStream.read() returns a sign extended int representation of the byte it reads, which is wrong (value should be in range [-1..255]) ByteBufferBackedInputStream.read(byte[], int, int) does not return -1 when there are no bytes … Read more

How to convert a double into a byte array in swift?

typealias Byte = UInt8 func toByteArray<T>(var value: T) -> [Byte] { return withUnsafePointer(&value) { Array(UnsafeBufferPointer(start: UnsafePointer<Byte>($0), count: sizeof(T))) } } toByteArray(1729.1729) toByteArray(1729.1729 as Float) toByteArray(1729) toByteArray(-1729) But the results are reversed from your expectations (because of endianness): [234, 149, 178, 12, 177, 4, 155, 64] [136, 37, 216, 68] [193, 6, 0, 0, 0, 0, … Read more

Byte array to image conversion

You are writing to your memory stream twice, also you are not disposing the stream after use. You are also asking the image decoder to apply embedded color correction. Try this instead: using (var ms = new MemoryStream(byteArrayIn)) { return Image.FromStream(ms); }