/*
* 字节编码转换
* 首先都需要转为二级制数组 (ArrayBuffer)
* 然后才能转换对应的编码字符
* 前端常见编码:
* base64:就是将二进制转为字符串,将每6个字节转为一个特定的字符串(A-Za-z0-9/+=)。
* hex:将二进制每8个字节转为对应的2个十六进制的字符串
* */

// utf8 转为 base64/hex
let output = Buffer.from('utf8的字符串', 'utf8')
console.log(output.toString('base64'))
console.log(output.toString('hex'))


// base64/hex 转为 utf8
output = Buffer.from('75746638e79a84e5ad97e7aca6e4b8b2', 'hex')
console.log(output.toString('utf8'))
output = Buffer.from('dXRmOOeahOWtl+espuS4sg==', 'base64')
console.log(output.toString('utf8'))


// 读取文件传入编码
input = fs.readFileSync('test.txt')  // 默认是二进制 Buffer
console.log(input)
let input = fs.readFileSync('test.txt', 'utf8')
console.log(input)
input = fs.readFileSync('test.txt', 'base64')
console.log(input)
input = fs.readFileSync('test.txt', 'hex')
console.log(input)

  

 

/*
* 加密需注意
* 加密数据类型:Buffer 或者 字符串(hex/base64/utf8)
* 参数传入参数:vi - 填充
* 参数传入参数:mode - 模式
* 参数传入参数:padding - 填充类型
* 加密输出类型:Buffer 或者 字符串(hex/base64/utf8)
* */

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-05-26
  • 2021-11-30
  • 2022-03-14
猜你喜欢
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-07-25
  • 2022-12-23
  • 2021-11-07
相关资源
相似解决方案