About JavaScript front-end development of the implementation of binary read-write operation of the relevant introduction, please look at the following details, this article introduces a very detailed, with reference value.
For a variety of reasons, the binary cannot be manipulated in the browser like Nodejs.
Recently wrote a help class that operates a read-write binary in the browser-side
!function (entrance) {"Use strict";
if ("object" = = = typeof exports && "undefined"!== typeof module) {module.exports = entrance ();
else if ("function" = = typeof define && define.amd) {define ([], entrance ());
else {var F;
if ("Undefined"!== typeof window) {f = window;
else {throw new Error (' Wrong execution Environment ');
} F.tinystream = entrance ();
} (function () {var binarypot = {/** * Initializes the byte stream, changing the interval from 128 to 128 to 0-256. Easy to compute * @param {array} array byte stream
* @return {array} transformed byte stream array */init:function (array) {for (var i = 0; i < Array.Length; i++) {
Array[i] *= 1; if (Array[i] < 0) {Array[i] + = 256} if (array[i]>255) {throw new Error (' illegal byte stream '
)} return array; /** * Writes a string in UTF8 encoding to the buffer * @param {string} str the string * @param {Boolean} that will be written to the buffer will only get content bytes (Excluding the most openTwo-bit bytes in the beginning * @returns {Array} byte stream * * writeutf:function (str, isgetbytes) {var back = [], byte
Size = 0;
for (var i = 0; i < str.length i++) {var code = str.charcodeat (i);
If (code >= 0 && Code <= 127) {bytesize = 1;
Back.push (code);
else if (code >= 128 && Code <= 2047) {bytesize = 2; Back.push ((192 |
(& (Code >> 6))); Back.push ((128 |
(& Code))
else if (Code >= 2048 && Code <= 65535) {bytesize = 3; Back.push ((224 |
(& (Code >> 12))); Back.push ((128 |
(& (Code >> 6))); Back.push ((128 |
(& Code))
for (i = 0; i < back.length i++) {if (back[i) > 255) {back[i] &= 255
} if (isgetbytes) {return back} if (ByteSize <= 255) { return [0, Bytesize].concat (back);
else {return [bytesize >> 8, ByteSize & 255].concat (back); },/** * reads a stream of streams of bytes according to the UTF8 code * @param arr byte stream * @returns the string read out by {string} * * Readutf:fu
Nction (arr) {if (Object.prototype.toString.call (arr) = = "[Object String]") {return arr;
var UTF = "", _arr = This.init (arr); for (var i = 0; i < _arr.length i++) {var one = _arr[i].tostring (2), V = One.match/^1+?
=0)/); if (v && one.length = 8) {var byteslength = v[0].length, store = _arr[i].tostring (2). Slice
(7-byteslength);
for (var st = 1; St < byteslength; st++) {store + + _arr[st + i].tostring (2). Slice (2)}
UTF + = String.fromCharCode (parseint (store, 2)); i + = bytesLength-1} else {UTF + + string.fromcharcode (_arr[i])}UTF},/** * Convert to stream object * @param x * @returns {stream} */Convertstream:function (x) { if (x instanceof Stream) {return x} else {return new Stream (x)}},/** * Convert a string into MQTT format * @param str * @returns {*|
Array} */Tomqttstring:function (str) {return This.writeutf (str)}}; /** * reads the byte stream of the specified length into the specified array * @param {stream} M stream instance * @param {number} I read the length * @param {array} A is stored in an array * @r
Eturns {array} stored in array/function Baseread (M, I, a) {var t = a? A: []; for (var start = 0; start < i; start++) {T[start] = m.pool[m.position++]} return T}/** * Judge browser Support Arraybuffer */var Supportarraybuffer = (function () {return!! Window.
Arraybuffer;
})(); /** * Byte Throttle processing entity class * @param {string| Array} array initializes the byte stream, and if it is a string, writes the buffer in UTF8 format * @constructor/function Stream (array) {if (!) (
This instanceof Stream)) { return new Stream (array);
/** * Byte Throttle buffer * @type {Array} * * This.pool = [];
if (Object.prototype.toString.call (array) = = ' [Object array] ') {This.pool = Binarypot.init (array);
else if (Object.prototype.toString.call (array) = = "[Object Arraybuffer]") {var arr = new Int8array (array);
This.pool = Binarypot.init ([].slice.call (arr));
else if (typeof array = = = ' String ') {This.pool = Binarypot.writeutf (array);
var self = this;
The starting position of the current stream execution this.position = 0;
The number of bytes written by the current stream this.writen = 0;
Returns whether the starting position of the current stream execution is greater than the length of the entire stream this.check = function () {return self.position >= self.pool.length}; /** * Cast to stream object * @param x * @returns {*|
Stream} */stream.parse = function (x) {return binarypot.convertstream (x);
}; Stream.prototype = {/** * reads the length of 4 bytes from the buffer and converts it to an int value, position the number read by the 4-bit * @returns {numbers} * @descriptio n if position is greater than or equal to the length of the bufferReturns-1/readint:function () {if (This.check ()) {return-1} var end = ""; for (var i = 0; i < 4; i++) {end = This.pool[this.position++].tostring (+)} return parseint
16);
/** * Reads 1 bytes from the buffer, position backward 1-bit * @returns {number} * @description returns-1 if position is greater than or equal to the length of the buffer * * Readbyte:function () {if (This.check ()) {return-1} var val = this.pool[this.position++]
;
if (val > 255) {val &= 255;
return Val;
/** * Reads 1 bytes from the buffer, or reads the byte of the specified length into the passed-in array, position moves back 1 or bytesarray.length bit * @param {array|undefined} bytesarray * @returns {array| Number} */Read:function (Bytesarray) {if (This.check ()) {return-1} if (Bytesarray
{return Baseread (this, bytesarray.length | 0, Bytesarray)} else {return this.readbyte (); }},/** * from the posit of the bufferThe ion position reads the string in UTF8 format, position the specified length backward @returns {string} read the string/readutf:function () {var big = (th Is.readbyte () << 8) |
This.readbyte ();
Return Binarypot.readutf (This.pool.slice (this.position, this.position + = big)); /** * Writes a stream of bytes to the buffer, writen the specified bit * @param {number|
Array} _byte bytes written to buffer (stream) * @returns {Array} write stream/Write:function (_byte) {var b = _byte;
if (Object.prototype.toString.call (b). toLowerCase () = = "[Object array]") {[].push.apply (this.pool, b);
This.writen + = B.length;
else {if (+b = = b) {if (b > 255) {b &= 255;
} this.pool.push (b); this.writen++} return B},/** * writes the parameter as a char type to the buffer, Writen backward 2-bit * @param {number} V Bytes written to buffer */writechar:function (v) {if (+v!= v) {throw new Error ("Writechar:arguments type is Error ")}. Write ((v >> 8) & 255);
This.write (v & 255); This.writen + + 2},/** * writes the string in UTF8 format to the buffer, Writen moves the specified bit * @param {string} str String * @return {ARR
AY} buffer */writeutf:function (str) {var val = binarypot.writeutf (str);
[].push.apply (This.pool, Val);
This.writen + = Val.length; /** * Changes the buffer byte stream format from 0 to 256 to 128 to 128 intervals * @returns {Array} converted Bytes/tocomplements:function ()
{var _tpool = This.pool;
for (var i = 0; i < _tpool.length i++) {if (_tpool[i] > 128) {_tpool[i] = 256} Return _tpool},/** * Gets the byte of the entire buffer * @param {Boolean} iscom whether to convert the word throttling interval * @returns {Array} converted The buffer * * Getbytesarray:function (iscom) {if (iscom) {return this.tocomplements ()} RE Turn This.pool},/** * Converts the byte stream of the buffer to Arraybuffer * @returns {arraybuffer} * @throw {Error} does not support ARRAYbuffer */toarraybuffer:function () {if (Supportarraybuffer) {return new Arraybuffer (This.getby
Tesarray ());
else {throw new Error (' not support arraybuffer ');
}, Clear:function () {this.pool = [];
This.writen = this.position = 0;
}
};
return Stream; });
How to use it?
<script src= "Binary.js" ></script>
<script>
var ts = tinystream (' My name is Zhang Yatao ');
Ts.writeutf (' hello ');
Console.log (' Get buffer byte stream: ', Ts.getbytesarray ());
Console.log (' Current buffer position: ', ts.position, ' writen as: ', ts.writen);
Console.log (' Read the first UTF8 byte throttle: ', Ts.readutf ());
Console.log (' Current buffer position: ', ts.position, ' writen as: ', ts.writen);
Console.log (' Read the second UTF8 byte throttle: ', Ts.readutf ());
Console.log (' Current buffer position: ', ts.position, ' writen as: ', ts.writen);
</script>
Later, I can not worry about the browser segment to deal with the binary!!! I hope this article will help you to learn about JavaScript binary knowledge.