Correct way to convert size in bytes to KB, MB, GB in JavaScript

From this: (source) function bytesToSize(bytes) { var sizes = [‘Bytes’, ‘KB’, ‘MB’, ‘GB’, ‘TB’]; if (bytes == 0) return ‘0 Byte’; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ‘ ‘ + sizes[i]; } Note: This is original code, Please use fixed version below. Fixed version, unminified and ES6’ed: (by … Read more

How do I convert Long to byte[] and back in java

public byte[] longToBytes(long x) { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.putLong(x); return buffer.array(); } public long bytesToLong(byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); buffer.put(bytes); buffer.flip();//need flip return buffer.getLong(); } Or wrapped in a class to avoid repeatedly creating ByteBuffers: public class ByteUtils { private static ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); public static byte[] longToBytes(long x) { buffer.putLong(0, … Read more

Getting the size of a field in bytes with C#

You can’t, basically. It will depend on padding, which may well be based on the CLR version you’re using and the processor etc. It’s easier to work out the total size of an object, assuming it has no references to other objects: create a big array, use GC.GetTotalMemory for a base point, fill the array … Read more

Convert integer into byte array (Java)

Have a look at the ByteBuffer class. ByteBuffer b = ByteBuffer.allocate(4); //b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN. b.putInt(0xAABBCCDD); byte[] result = b.array(); Setting the byte order ensures that result[0] == 0xAA, result[1] == 0xBB, result[2] == 0xCC and result[3] == 0xDD. Or alternatively, you could do it manually: … 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); }