Fix asciiToHex for characters < 0x10 (#6702)

This commit is contained in:
Jaco Greeff 2017-10-11 13:58:40 +02:00 committed by Gav Wood
parent 3a9b3b4729
commit f238c97c6f
2 changed files with 15 additions and 4 deletions

View File

@ -75,10 +75,13 @@ export function bytesToAscii (bytes) {
} }
export function asciiToHex (string) { export function asciiToHex (string) {
return '0x' + string.split('') let result = '0x';
.map(s => s.charCodeAt(0))
.map(s => s < 0x10 ? '0' + s.toString(16) : s.toString(16)) for (let i = 0; i < string.length; ++i) {
.join(''); result += ('0' + string.charCodeAt(i).toString(16)).substr(-2);
}
return result;
} }
export function padRight (input, length) { export function padRight (input, length) {

View File

@ -69,6 +69,14 @@ describe('api/util/format', () => {
expect(asciiToHex('abc')).to.equal('0x616263'); expect(asciiToHex('abc')).to.equal('0x616263');
expect(asciiToHex('a\nb')).to.equal('0x610a62'); expect(asciiToHex('a\nb')).to.equal('0x610a62');
}); });
it('correctly converts where charCode < 0x10', () => {
expect(
asciiToHex(
[32, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].map((v) => String.fromCharCode(v)).join('')
)
).to.equal('0x20100f0e0d0c0b0a09080706050403020100');
});
}); });
describe('hexToAscii', () => { describe('hexToAscii', () => {