String.byte and string.char The most underrated string functions

Let me know if this video is helpful :smiley:

local ZERO_BYTE = string.byte("0")
local NINE_BYTE = string.byte("9")

local A_BYTE = string.byte("A")
local Z_BYTE = string.byte("Z")

local LA_BYTE = string.byte("a")
local LZ_BYTE = string.byte("z")

local UPPER_LOWER_CHAR_OFFSET = LA_BYTE-A_BYTE

local function isNumberChar(char)
  local charByte = string.byte(char)
  -- ZERO_BYTE <= charByte <= NINE_BYTE
  return charByte >= ZERO_BYTE and charByte <= NINE_BYTE
end

local function isUppercaseChar(char)
  local charByte = string.byte(char)
  return charByte >= A_BYTE and charByte <= Z_BYTE
end

local function isLowercaseChar(char)
  local charByte = string.byte(char)
  return charByte >= LA_BYTE and charByte <= LZ_BYTE
end

local function lowerChar(char)
  local charByte = string.byte(char)
  if charByte >= A_BYTE and charByte <= Z_BYTE then
    return string.char(charByte + UPPER_LOWER_CHAR_OFFSET)
  else
    return char
  end
end

local function upperChar(char)
  local charByte = string.byte(char)
  if charByte >= LA_BYTE and charByte <= LZ_BYTE then
    return string.char(charByte - UPPER_LOWER_CHAR_OFFSET)
  else
    return char
  end
end

local function isAlphabetChar(char)
  local charByte = string.byte(upperChar(char))
  return charByte >= A_BYTE and charByte <= Z_BYTE
end

local function warp(value, min, max)
  return ((value-min) % (max-min+1)) + min
end

local function encrypt(str, key)
  local t = {}
  
  for i = 1, #str, 1 do
    local char = string.sub(str, i, i)
    local charByte = string.byte(char)
    local encryptedCharByte = warp(charByte + key, string.byte("!"), string.byte("~"))
    table.insert(t, string.char(encryptedCharByte))
  end
  
  return table.concat(t)
end

local function decrypt(str, key)
  return encrypt(str, -key)
end

print(encrypt("Hello world           !", 546554))
8 Likes

Hi Codes itโ€™s me. Personally I find these really cool and I didnโ€™t now about it until I watched your vid!

2 Likes