Converting bytes to an image for drawing on a HTML5 canvas

I used this in the end: function draw(imgData, frameCount) { var r = new FileReader(); r.readAsBinaryString(imgData); r.onload = function(){ var img=new Image(); img.onload = function() { cxt.drawImage(img, 0, 0, canvas.width, canvas.height); } img.src = “data:image/jpeg;base64,”+window.btoa(r.result); }; } I needed to read the bytes into a string before using btoa().

Byte Array to Bitmap Image

This is an alternative method int w= 100; int h = 200; int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case) byte[] imageData = new byte[w*h*ch]; //you image data here Bitmap bitmap = new Bitmap(w,h,PixelFormat.Format24bppRgb); BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); IntPtr pNative = bmData.Scan0; … Read more

How can I convert bits to bytes?

The code is treating the first bit as the low bit of the word, so you end up with each word reversed. As a quick-and-dirty fix, try this: bytes[byteIndex] |= (byte)(1 << (7-bitIndex)); That puts the first bit in the array at the highest position in the first byte, etc.

Convert hex string to byte []

Convert hex to byte and byte to hex. public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len/2]; for(int i = 0; i < len; i+=2){ data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } final protected static char[] hexArray = {‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’A’,’B’,’C’,’D’,’E’,’F’}; public static String … Read more

How to convert byte array to any type

Primitive types are easy because they have a defined representation as a byte array. Other objects are not because they may contain things that cannot be persisted, like file handles, references to other objects, etc. You can try persisting an object to a byte array using BinaryFormatter: public byte[] ToByteArray<T>(T obj) { if(obj == null) … Read more