Concatenating Hex Bytes And Strings In Javascript While Preserving Bytes
I would like to concatenate a hex value and a string using JavaScript. var hexValue = 0x89; var png = 'PNG'; The string 'PNG' is equivalent to the concatenation of 0x50, 0x4E, and
Solution 1:
Well i was investigating and i found that in javascript to achieve this eficienly JavaScript typed arrays is used.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
http://msdn.microsoft.com/en-us/library/br212485(v=vs.94).aspx
Here i wrote a code (not tested) to perform what you want:
var png = "PNG";
var hexValue = 0x89;
var lenInBytes = (png.Length + 1) * 8; //left an extra space to concat hexValuevar buffer = newArrayBuffer(lenInBytes); //create chunk of memory whose bytes are all pre-initialized to 0var int8View = newInt8Array(buffer); //treat this memory like int8for(int i = 0; i < png.Length ; i++)
int8View[i] = png[i] //here convert the png[i] to bytes//at this point we have the string png as array of bytes
int8View[png.Length] = hexValue //here the concatenation is performed
Well hope it helps.
Post a Comment for "Concatenating Hex Bytes And Strings In Javascript While Preserving Bytes"