Add Strings.toHexString(bytes) (#5761)
This commit is contained in:
@ -128,6 +128,23 @@ library Strings {
|
||||
return string(buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Converts a `bytes` buffer to its ASCII `string` hexadecimal representation.
|
||||
*/
|
||||
function toHexString(bytes memory input) internal pure returns (string memory) {
|
||||
unchecked {
|
||||
bytes memory buffer = new bytes(2 * input.length + 2);
|
||||
buffer[0] = "0";
|
||||
buffer[1] = "x";
|
||||
for (uint256 i = 0; i < input.length; ++i) {
|
||||
uint8 v = uint8(input[i]);
|
||||
buffer[2 * i + 2] = HEX_DIGITS[v >> 4];
|
||||
buffer[2 * i + 3] = HEX_DIGITS[v & 0xf];
|
||||
}
|
||||
return string(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns true if the two strings are equal.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user