File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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' , ( ) => {
You can’t perform that action at this time.
0 commit comments