buffer.js 574 B

12345678910111213141516171819202122
  1. function buf2hex(buffer) { // buffer is an ArrayBuffer
  2. return [...new Uint8Array(buffer)]
  3. .map(x => x.toString(16).padStart(2, '0'))
  4. .join('');
  5. }
  6. function hex2ab(hex){
  7. let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
  8. return parseInt(h, 16)
  9. }))
  10. let buffer = typedArray.buffer
  11. return buffer
  12. }
  13. function hexStrToBuffer(str){
  14. // 填充首位
  15. if(str.length % 2){str=`0${str}`}
  16. for(let i=0;i<str.length;i+=2){
  17. hex2ab
  18. }
  19. }
  20. module.exports = {buf2hex,hex2ab}