-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
string <-> hex conversion utilities (#470)
* string <-> hex conversion utilities * Add tests
- Loading branch information
Showing
8 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2017-2019 @polkadot/util authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { hexToString } from '.'; | ||
|
||
describe('hexToString', (): void => { | ||
it('converts an empty to ""', (): void => { | ||
expect( | ||
hexToString() | ||
).toEqual(''); | ||
}); | ||
|
||
it('converts to a string from hex', (): void => { | ||
expect( | ||
hexToString('0x68656c6c6f') | ||
).toEqual('hello'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2017-2019 @polkadot/util authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import u8aToString from '../u8a/toString'; | ||
import hexToU8a from './toU8a'; | ||
|
||
/** | ||
* @name hexToU8a | ||
* @summary Creates a Uint8Array object from a hex string. | ||
* @description | ||
* Hex input values return the actual bytes value converted to a string. Anything that is not a hex string (including the `0x` prefix) throws an error. | ||
* @example | ||
* <BR> | ||
* | ||
* ```javascript | ||
* import { hexToString } from '@polkadot/util'; | ||
* | ||
* hexToU8a('0x68656c6c6f'); // hello | ||
* ``` | ||
*/ | ||
export default function hexToString (_value?: string | null): string { | ||
return u8aToString( | ||
hexToU8a(_value) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2017-2019 @polkadot/util authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { stringToHex } from '.'; | ||
|
||
describe('hexToString', (): void => { | ||
it('converts an empty to ""', (): void => { | ||
expect( | ||
stringToHex() | ||
).toEqual('0x'); | ||
}); | ||
|
||
it('converts to a hex from string', (): void => { | ||
expect( | ||
stringToHex('hello') | ||
).toEqual('0x68656c6c6f'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2017-2019 @polkadot/util authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import u8aToHex from '../u8a/toHex'; | ||
import stringToU8a from './toU8a'; | ||
|
||
/** | ||
* @name stringToHex | ||
* @summary Creates a hex string from a utf-8 string | ||
* @description | ||
* String input values return the actual encoded hex value. | ||
* @example | ||
* <BR> | ||
* | ||
* ```javascript | ||
* import { stringToHex } from '@polkadot/util'; | ||
* | ||
* stringToU8a('hello'); // 0x68656c6c6f | ||
* ``` | ||
*/ | ||
export default function stringToHex (value?: string): string { | ||
return u8aToHex( | ||
stringToU8a(value) | ||
); | ||
} |