怎么样在vue中把base64转换为16进制字节?

const baseString = 'QswAAAAA';  

const resultByte = ['42','CC','00','00','00','00']; //6位字节

有个不重要但已知的条件是,转换后一定要是6为字节,或者说baseString的值转换后一定也是6位字节,求教大神们应该怎么转

小弟先在这里谢谢各位大神的解答,感激不尽!!!


回答:

6位字节一个字节数组?这个可以把所有字符放在一个字节数组里面,不太知道你想要的6个字节在一组是怎么弄,可以基于此修改输出。看看是否符合你的要求。

function base64ToHex(str) {

const raw = atob(str);

let result = '';

for (let i = 0; i < raw.length; i++) {

const hex = raw.charCodeAt(i).toString(16);

result += (hex.length === 2 ? hex : '0' + hex);

}

return result.toUpperCase();

}

// test

const res = base64ToHex('QswAAAAA')

console.log(res.match(/(.{2})/g)) // ['42', 'CC', '00', '00', '00', '00']

以上是 怎么样在vue中把base64转换为16进制字节? 的全部内容, 来源链接: utcz.com/p/933272.html

回到顶部