Skip to content

Instantly share code, notes, and snippets.

@uqmessias

uqmessias/dec2hex.ts

Last active Feb 1, 2020
Embed
What would you like to do?
Converts an input parameter from Decimal to Hexadecimal
/**
* If you want to use it, you first need to install
* Deno at https://deno.land and run:
* deno https://gist.github.com/uqmessias/66f3de0da2f60419b39f26ca243f3e2a/raw 222
*/
function throwError(wrongArgument = null) {
const argumentError =
wrongArgument === null
? ""
: `"${wrongArgument}" is not a valid argument!\n\n`;
throw new Error(
`${argumentError}You must provide at least one decimal number as an argument.
# If you wish to convert only one decimal number, you can provide it as the only argument.
deno https://gist.github.com/uqmessias/66f3de0da2f60419b39f26ca243f3e2a/raw 222
# And you still can provide serveral decimal numbers as arguments in order to convert them.
deno https://gist.github.com/uqmessias/66f3de0da2f60419b39f26ca243f3e2a/raw 234 199 02`
);
}
if (!Deno.args.length) {
throwError();
} else {
Deno.args.forEach(function(arg) {
const dec = parseInt(arg, 10);
if (isNaN(dec)) {
throwError(arg);
return;
}
console.log(`"${dec}" converted to "${dec.toString(16).toUpperCase()}"`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.