wilton.js  v202103141
 All Namespaces Functions
Functions
hex Namespace Reference

wilton/hex
Convert strings into hexadecimal encoding and back More...

Functions

String decodeBytes (String hexstr, Function|Undefined callback)
 Decode hex-string into "byte" string.
String decodeUTF8 (String hexstr, Function|Undefined callback)
 Decode UTF-8 hex-string.
String encodeBytes (String str, Function|Undefined callback)
 Encode "byte" string as hexadecimal.
String encodeUTF8 (String str, Function|Undefined callback)
 Encode Unicode string as UTF-8 hexadecimal.
String formatHexAndPlain (String hexstr, Function|Undefined callback)
 Format hex-string as a hex+plain string.
Boolean isPretty (String hexstr, Function|Undefined callback)
 Check whether specified hex-string is "prettified".
String prettify (String hexstr, Function|Undefined callback)
 Pretty-print hex-string.
String replaceNonPrintable (String hexstr, Function|Undefined callback)
 Replace non-printable chars in hex-string.
String uglify (String hexstr, Function|Undefined callback)
 Convert "prettified" hex-string into plain hex-string.

Detailed Description

This module provides utility function to encode arbitrary strings as hexadecimal and to parse hexadecimal data.

When converting to/from JavaScript Strings, data can be handled as a "plain binary" ((encode|decode)Bytes functions - each byte is converted to/from hex as is) or as Unicode data ((encode|decode)UTF8 functions).

In Unicode case, on encoding String is converted to UTF-8 first and then UTF-8 bytes are encoded into hex. On decoding, hex bytes are treated as UTF-8 encoded and converted to UTF-16 before returing a String.

Additonally contains utility functions to pretty-print hex-encoded data.

Usage example:

// encode
var hd = hex.encodeBytes("foo"); // "666f6f"
var phd = hex.prettify(hd); // "66 6f 6f"
var uhd = hex.encodeUTF8("привет"); // "d0bfd180d0b8d0b2d0b5d182"
// decode
var dec1 = hex.decodeBytes("66 6f 6f"); // "foo"
var dec2 = hex.decodeUTF8("d0 bf d1 80 d0 b8 d0 b2 d0 b5 d1 82") // "привет"

Function Documentation

String hex::decodeBytes ( String  hexstr,
Function|Undefined  callback 
)

Decodes hex-string into String treating each hex-encoded byte as a char code.

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
String decoded string
String hex::decodeUTF8 ( String  hexstr,
Function|Undefined  callback 
)

Decodes hex-string using decodeBytes() and then decodes resulting string from UTF-8.

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
String decoded string
String hex::encodeBytes ( String  str,
Function|Undefined  callback 
)

Encodes input string as hexadecimal treating each input symbol as a char code.

Parameters
strString "byte" string to encode
callbackFunction|Undefined callback to receive result or error
Returns
String string in hexadecimal encoding
String hex::encodeUTF8 ( String  str,
Function|Undefined  callback 
)

Encodes input string into UTF-8 and then encodes resulting string using encodeBytes().

Parameters
strString string to encode
callbackFunction|Undefined callback to receive result or error
Returns
String string in hexadecimal encoding
String hex::formatHexAndPlain ( String  hexstr,
Function|Undefined  callback 
)

Formats specified hex-string as a hex [plain] string. Non-printable characters in plain string are replaced with spaces.

Intended to be used for logging.

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
String formatted string
Boolean hex::isPretty ( String  hexstr,
Function|Undefined  callback 
)

Check whether specified hex-string contains spacess after each second symbol.

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
Boolean whether hex-string is "prettified"
String hex::prettify ( String  hexstr,
Function|Undefined  callback 
)

Formats hex-string adding a space after each second symbol.

Does nothing if input string is already "prettified".

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
String "prettified" hex-string
String hex::replaceNonPrintable ( String  hexstr,
Function|Undefined  callback 
)

Replaces non-printable ASCII chars (codes from 0 to 8) with spaces.

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
String hex-string with non-printable chars replaced
String hex::uglify ( String  hexstr,
Function|Undefined  callback 
)

"Un-prettify" hex-string effectively stripping all whitespace symbols from it.

Parameters
hexstrString string in hexadecimal encoding
callbackFunction|Undefined callback to receive result or error
Returns
String "un-prettified" hex-string