[英]issue with decoding base64: Decoded string is empty
我正在嘗試使用以下代碼將字符串解碼和編碼為 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')
}
現在,當我嘗試運行編碼時,它可以完美運行並返回 base64 字符串,但是當我嘗試將其轉換回 utf-8 時,它給了我一個空字符串。 我嘗試使用以下代碼執行此操作:
const hash = '=ZWYwMjAyY2UtZWFkNy00ZDk4LWFkOTQtODAzZDJiMWM0ZGI5OnRyeUB0cnkuY29t'
const decoded= decode(hash).split(':')
console.log('decoded string', decoded)
console.log(decode(does this work??))
執行上述代碼后控制台的output:
entered decode =ZWYwMjAyY2UtZWFkNy00ZDk4LWFkOTQtODAzZDJiMWM0ZGI5OnRyeUB0cnkuY29t
decoded string [ '' ]
entered decode does this work?
v�����
有人可以幫我解決我在這里做錯了什么嗎?
嘗試刪除'=',也許你在編碼時錯了
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.