From 80bddb4f789014569aa919066f78ea765e0a4537 Mon Sep 17 00:00:00 2001 From: Daniel <790119+DanTheMan827@users.noreply.github.com> Date: Tue, 1 Aug 2023 22:39:21 -0500 Subject: [PATCH] Add hexDump function for debugging --- ntag215.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ntag215.js b/ntag215.js index 8b76665..f4c9dfa 100644 --- a/ntag215.js +++ b/ntag215.js @@ -170,6 +170,47 @@ function fixUid() { return false; } +/** + * This function takes an array `inputData` as input and converts its elements into hexadecimal format, displaying them in a formatted way for better visualization. + * + * Terser will optimize the function away because it's not used, but it's useful to have for debugging. + * @param {Uint8Array} inputData + */ +function hexDump(inputData) { + // Initialize an empty string `line`, which will be used to build the output lines containing the hexadecimal values. + var line = ""; + + // Iterate through each element of the `inputData` array. + for (let i = 0; i < inputData.length; i++) { + /* + Convert the decimal value of the current element to a two-digit hexadecimal string. + If the hexadecimal string is less than two digits, pad it with leading zeros. + Convert the result to uppercase for consistency. + */ + const hex = inputData[i].toString(16).padStart(2, '0').toUpperCase(); + + // Append the hexadecimal value followed by a space to the `line` string. + line = line + hex + ' '; + + // Check if the current index is a multiple of 8 (i.e., the end of a line). + if ((i + 1) % 8 === 0) { + // If 8 elements have been added to the `line` string, log the current `line` to the console. + console.log(line.trim()); + + // Reset the `line` string to an empty state, to start building the next line. + line = ''; + } + } + + /* + After the loop, there might be remaining elements in the `line` string that were not enough to form a complete line of 8 elements. + In that case, log the remaining `line` to the console. + */ + if (line != '') { + console.log(line.trim()); + } +} + /** * Generates a random UID. * @returns {Uint8Array} The UID.