The unhex() function in p5.js is used to convert a string representation of any input hexadecimal number to its equivalent integer value.
Syntax:
javascript
Output:
Example-2:
javascript
unhex(String)Parameters: This function accepts a parameter String which is a hexadecimal number and is to be converted into its integer equivalent value. This parameter might also be an array of strings of the hexadecimal numbers. Return Value: It returns the converted integer representation. Below program illustrates the unhex() function in p5.js. Example-1:
function setup() {
// Creating Canvas size
createCanvas(500, 100);
}
function draw() {
// Set the background color
background(220);
// Initializing some strings
let String1 = "F";
let String2 = "FF";
// Calling to unhex() function.
let A = unhex(String1);
let B = unhex(String2);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting integer equivalent
text("Integer equivalent of hexadecimal string 'F' is: "
+ A, 50, 30);
text("Integer equivalent of hexadecimal string 'FF' is: "
+ B, 50, 60);
}
Example-2:
function setup() {
// Creating Canvas size
createCanvas(650, 100);
}
function draw() {
// Set the background color
background(220);
// Initializing some strings
let String1 = ["F", "A", "C"];
let String2 = ["FF", "AC", "DE"];
// Calling to unhex() function.
let A = unhex(String1);
let B = unhex(String2);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting integer equivalent
text("Integer equivalent of array of hexadecimal strings"+
" ['F', 'A', 'C'] is: "
+ A, 50, 30);
text("Integer equivalent of array of hexadecimal strings"+
" ['FF', 'AC', 'DE'] is: "
+ B, 50, 60);
}
Output:
Reference: https://p5js.org/reference/#/p5/unhex
Reference: https://p5js.org/reference/#/p5/unhex