Buffer轉 16進制 及 Buffer 轉String

轉16進制

let  a = 48 
  a.toString(16)  // 30
   console.log(a)  //48  a自己沒有變 仍是48  ,a.toString(16) 爲30
let bufTo16 = [1,23,56].reduce(function(accumulator, currentValue, currentIndex, array){
        if(currentValue < 16) {
              return accumulator + (0+ currentValue.toString(16))
        }else{
             return accumulator + currentValue.toString(16)
        }
  }, '')
[1,23,56].reduce((a, b) => a + (b < 16 ? 0 : '') + b.toString(16), '')

buffer轉 字符串 方法例子

let buf = new Uint8Array([65,66,67]);
  arrayBufferToString(buf, 'UTF-8', console.log.bind(console)); //"ABC"

function arrayBufferToString( buffer, encoding, callback ) {
        var blob = new Blob([buffer],{type:'text/plain'});
        var reader = new FileReader();
        reader.onload = function(evt){callback(evt.target.result);};
        reader.readAsText(blob, encoding);
 }


 // function ab2str(buf) {
//        return String.fromCharCode.apply(null, new Uint16Array(buf));
//}

參考文獻 https://stackoverflow.com/questions/26754486/how-to-convert-arraybuffer-to-string http://cnodejs.org/topic/56499568d28aa64101600fdc Node.js的Buffer那些你可能不知道的用法 http://www.javashuo.com/article/p-smnyajjn-dm.html HTML5 Blob與ArrayBuffer、TypeArray和字符串String之間轉換html

https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-stringnode

相關文章
相關標籤/搜索