Arn*_*Roy 10 express typescript tobase64string
我有一个用打字稿编写的快速服务器。
作为atob()或btoa()适用于浏览器,在 Nodejs 上。
我们一般用
Buffer.from("some-string").toString('base64') 将字符串编码为 base64。
但是,当我在 TypeScript 中编写代码时,这似乎不起作用。我需要一些帮助。
Joh*_*ohn 13
在节点打字稿中:
const b64 = "SGVsbG8sIFdvcmxkIQ==";
const str = 'Hello, World!'
const decode = (str: string):string => Buffer.from(str, 'base64').toString('binary');
const encode = (str: string):string => Buffer.from(str, 'binary').toString('base64');
test('base64 decode', () => {
expect(decode(b64)).toEqual(str)
});
test('base64 decode', () => {
expect(encode(str)).toEqual(b64)
});
test('base64 encode/decode', () => {
expect(decode(encode(str))).toEqual(str)
});
Run Code Online (Sandbox Code Playgroud)
小智 6
请使用btoa编码字符串
console.log(btoa("abc")); // YWJjRun Code Online (Sandbox Code Playgroud)
用于atob解码相同的字符串
console.log(atob("YWJj")); // abcRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17204 次 |
| 最近记录: |