简体   繁体   English

解码 base64 的问题:解码的字符串为空

[英]issue with decoding base64: Decoded string is empty

I am trying to decode and encode string to base64 using the following code:我正在尝试使用以下代码将字符串解码和编码为 base64:

export function encode(data: string) {
    console.log('entered encode', data)
    return Buffer.from(data, 'utf-8').toString('base64')
}

export function decode(data:string ){
    console.log('entered decode', data)
    return Buffer.from(data, 'base64').toString('utf-8')
}

now when I try to run encode it works perfectly and returns me a base64 string but when I try to convert it back to utf-8 it gives me a empty string.现在,当我尝试运行编码时,它可以完美运行并返回 base64 字符串,但是当我尝试将其转换回 utf-8 时,它给了我一个空字符串。 I've tried doing this using the following code:我尝试使用以下代码执行此操作:

const hash = '=ZWYwMjAyY2UtZWFkNy00ZDk4LWFkOTQtODAzZDJiMWM0ZGI5OnRyeUB0cnkuY29t'
const decoded= decode(hash).split(':')
console.log('decoded string', decoded)
console.log(decode(does this work??))

the output of the console after above code executed:执行上述代码后控制台的output:

entered decode =ZWYwMjAyY2UtZWFkNy00ZDk4LWFkOTQtODAzZDJiMWM0ZGI5OnRyeUB0cnkuY29t
decoded string [ '' ]
entered decode does this work?
v�����

could someone help me out with what am I doing wrong here?有人可以帮我解决我在这里做错了什么吗?

try remove '=', maybe you wrong while encode尝试删除'=',也许你在编码时错了

export function decode(data:string ){
        console.log('entered decode', data)
        return Buffer.from(data, 'base64').toString('utf-8')
}

const hash = 'ZWYwMjAyY2UtZWFkNy00ZDk4LWFkOTQtODAzZDJiMWM0ZGI5OnRyeUB0cnkuY29t'
    const decoded= decode(hash).split(':')
    console.log('decoded string', decoded)
    console.log(decode(does this work??))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM