typings, hex number conversion

This commit is contained in:
Timo Hocker 2020-04-24 16:11:32 +02:00
parent ea08f9739e
commit a5460cd540
2 changed files with 61 additions and 1 deletions

38
index.d.ts vendored Normal file
View File

@ -0,0 +1,38 @@
/**
* encode a string to base64
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} base64
*/
export function to_b64(str: string, encoding?: string): string;
/**
* encode a string to hex
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} hex
*/
export function to_hex(str: string, encoding?: string): string;
/**
* encode a string to utf-8
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} utf-8
*/
export function to_utf8(str: string, encoding: string): string;
/**
* encode a number to hex
*
* @param {number} n number to encode
* @returns {string} hex
*/
export function num_to_hex(n: number): string;
/**
* decode a number from hex
*
* @param {string} h hex to decode
* @returns {number} number
*/
export function hex_to_num(h: string): number;

View File

@ -44,9 +44,31 @@ function to_hex (str, encoding = '') {
return buf.toString ('hex'); return buf.toString ('hex');
} }
/**
* encode a number to hex
*
* @param {number} n number to encode
* @returns {string} hex
*/
function num_to_hex (n) {
return n.toString(16);
}
/**
* decode a number from hex
*
* @param {string} h hex to decode
* @returns {number} number
*/
function hex_to_num (h) {
return parseInt(h, 16);
}
module.exports = { module.exports = {
to_b64, to_b64,
to_hex, to_hex,
to_utf8 to_utf8,
num_to_hex,
hex_to_num
}; };