Skip to content

Commit 44e3712

Browse files
committed
Added #175 added contol char handler in convert.ts
1 parent 709cb4b commit 44e3712

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/core/format/Convert.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ export class Convert {
168168
const rawString = Convert.rstr2utf8(input);
169169
let result = '';
170170
for (let i = 0; i < rawString.length; i++) {
171-
result += rawString.charCodeAt(i).toString(16);
171+
if (rawString.charCodeAt(i) < 16) {
172+
// Add insignificant zero for control chars (0x00 - 0x0f)
173+
result += '0' + rawString.charCodeAt(i).toString(16);
174+
} else {
175+
result += rawString.charCodeAt(i).toString(16);
176+
}
172177
}
173178
return result;
174179
}

test/core/format/Convert.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ describe('convert', () => {
233233
// Assert:
234234
expect(actual).to.equal('e58588e7a7a6e585a9e6bca2');
235235
});
236+
237+
it('utf8 text to hex with control char', () => {
238+
// Act:
239+
const test = String.fromCodePoint(0x0f) + ' Hello World!';
240+
console.log('UTF8', test);
241+
const actual = convert.utf8ToHex(test);
242+
243+
// Assert:
244+
expect(actual).to.equal('0f2048656c6c6f20576f726c6421');
245+
});
236246
});
237247

238248
describe('signed <-> unsigned byte', () => {

0 commit comments

Comments
 (0)