From 719a088d00de9fde34c56eb326a7cd389203a891 Mon Sep 17 00:00:00 2001 From: Heinrich Weichert Date: Wed, 7 Jul 2021 12:40:10 +0200 Subject: [PATCH 1/3] Add hex lib, +doc --- README.md | 8 ++++++++ hex.lua | 17 +++++++++++++++++ lualib.lua | 4 ---- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 hex.lua delete mode 100644 lualib.lua diff --git a/README.md b/README.md index 1d8a92d..0575155 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # lua-simulation-libraries + Lua libraries to be used in CarSimulator simulation scripts. + +## Supported functions + +### hex.lua + +- hexToAscii (convert string from hex to ASCII) +- asciiToHex (convert string from ASCII to hex) diff --git a/hex.lua b/hex.lua new file mode 100644 index 0000000..728539d --- /dev/null +++ b/hex.lua @@ -0,0 +1,17 @@ +--[[ + Convert the given string from hexadecimal to ASCII notation +]] +function hexToAscii(str) + return (str:gsub('..', function (cc) + return string.char(tonumber(cc, 16)) + end)) +end + +--[[ + Convert the given string from ASCII to hexadecimal notation +]] +function asciiToHex(str) + return (str:gsub('.', function (c) + return string.format('%02X', string.byte(c)) + end)) +end \ No newline at end of file diff --git a/lualib.lua b/lualib.lua deleted file mode 100644 index 161c176..0000000 --- a/lualib.lua +++ /dev/null @@ -1,4 +0,0 @@ - -function toHex(intValue, numBytes) - return string.format('%0' .. numBytes*2 .. 'X', intValue) -end From 5a4a63c8f69adc4ea3e2b5993bf9b6e9302546f0 Mon Sep 17 00:00:00 2001 From: Heinrich Weichert Date: Wed, 7 Jul 2021 12:40:33 +0200 Subject: [PATCH 2/3] Add hex lib, +doc From 4e11ffc62dc0aa7049c5af32264496736c451a19 Mon Sep 17 00:00:00 2001 From: Heinrich Weichert Date: Wed, 7 Jul 2021 20:14:25 +0200 Subject: [PATCH 3/3] Add hex functions --- hex.lua | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/hex.lua b/hex.lua index 728539d..43325a0 100644 --- a/hex.lua +++ b/hex.lua @@ -1,17 +1,14 @@ --[[ - Convert the given string from hexadecimal to ASCII notation + Convert the given number to hexadecimal notation ]] -function hexToAscii(str) - return (str:gsub('..', function (cc) - return string.char(tonumber(cc, 16)) - end)) +function numberToHex(num) + return string.format('%x', num) end --[[ - Convert the given string from ASCII to hexadecimal notation + Convert the given ASCII string to hexadecimal notation ]] function asciiToHex(str) - return (str:gsub('.', function (c) - return string.format('%02X', string.byte(c)) - end)) -end \ No newline at end of file + return (str:gsub(".", function(char) return string.format("%2x", char:byte()) end)) +end +