Advertisement
karlakmkj

Node.js - buffer module

Oct 8th, 2021
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Allocate buffer of size 15 filled with 'b'
  2. const bufferAlloc = Buffer.alloc(15, 'b');
  3.  
  4. // Create buffer1 with 'hello' and buffer2 with 'world'
  5. const buffer1 = Buffer.from('hello');
  6. const buffer2 = Buffer.from('world');
  7.  
  8.  
  9. // Combine buffer1 and buffer2
  10. const bufferArray = [buffer1, buffer2]
  11. const bufferConcat = Buffer.concat(bufferArray);
  12.  
  13. // Translate buffer to string
  14. const bufferString = bufferConcat.toString();
  15.  
  16.  
  17. console.log(bufferAlloc);  // will print 62 hexadecimal code for letter 'b'
  18. console.log('Buffer 1:', buffer1, 'Buffer 2:', buffer2)  // each printed represents the hexadecimal of the letters
  19. console.log(bufferConcat);
  20. console.log(bufferString); //then show the words combined in string form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement