const ethers = require('ethers')
const utils = ethers.utils

const inBytes = utils.formatBytes32String("test");

const inBytes = utils.formatBytes32String("hello");
console.log(inBytes);

const string = utils.parseBytes32String(inBytes);
console.log(string);
// 前补零(str不能为空,否则会少一位TT)
function fixZeroStart(str, n) {
    return (Array(n).join(0) + str).slice(-n);
}
 
// 后补零(str不能为空,否则会少一位TT)
function fixZeroEnd(str, n) {
    return (str + Array(n).join(0)).slice(0, n);
}

// 32 * 2 + 2 (0x)
text = bytes32FixZeroEnd(text, 64+2);
const string = utils.parseBytes32String(text);
console.log(string);