function stringToBytes(s: string): Uint8Array {
let arr = new Uint8Array(s.length);
for (let i = 0, l = s.length; i < l; i++) {
arr[i] = s.charCodeAt(i);
}
return arr;
};
// ASCII only
function bytesToString(buffer: ArrayBuffer): string {
return String.fromCharCode.apply(null, new Uint8Array(buffer));
};
function byteArrayToLong( /*byte[]*/ buff: ArrayBuffer): number {
var byteArray = new Uint8Array(buff)
var value = 0;
for (let i = byteArray.length - 1; i >= 0; i--) {
value = (value * 256) + byteArray[i];
}
return value;
};
function byteArrayToLong( /*byte[]*/ buff) {
var byteArray = new Uint8Array(buff)
var value = 0;
for (var i = byteArray.length - 1; i >= 0; i--) {
value = (value * 256) + byteArray[i];
}
return value;
};
寫法2:
function Buf2Str(buf) {
let ss = "< ";
buf.forEach(b => { ss += b.toString(16) + ' '; });
return ss + ">";
}