This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KittenTheEpic's profile picture

Help with strings.

Started by KittenTheEpic, 05 March 2014 - 08:10 PM
KittenTheEpic #1
Posted 05 March 2014 - 09:10 PM
I had tried to make a new encryption algorithim for computercraft. It looked like it would work for me.

And then, it returns:


11


Code:



local function encrypt(str)
local t = string.reverse(str)
local ln = string.len(t)
local lh = ln/2
local lho = lh+1
local aa = string.sub(1, lh)
local ab = string.sub(lho, ln)
local encrypted = ab..""..aa
return encrypted


end
local function decrypt(str)
local t = string.reverse(str)
local ln = string.len(t)
local lh = ln/2
local lho = lh+1
local aa = string.sub(1, lh)
local ab = string.sub(lho, ln)
local decrypted = aa..""..ab
return decrypted


end
local en = encrypt("Hello World")
local de = decrypt(en)
print(en)
print(de)

CometWolf #2
Posted 05 March 2014 - 09:24 PM
You forgot to pass the actual string to your sub functions.

local function encrypt(str)
  local t = string.reverse(str)
  local ln = string.len(t)
  local lh = ln/2
  local lho = lh+1
  local aa = string.sub(1, lh) --returns the string "1" subbed from lh
  local ab = string.sub(lho, ln) --returns iho, which is ih+1, subbed from in
  local encrypted = ab..""..aa
return encrypted
KittenTheEpic #3
Posted 05 March 2014 - 09:32 PM
You forgot to pass the actual string to your sub functions.
 local function encrypt(str) local t = string.reverse(str) local ln = string.len(t) local lh = ln/2 local lho = lh+1 local aa = string.sub(1, lh) --returns the string "1" subbed from lh local ab = string.sub(lho, ln) --returns iho, which is ih+1, subbed from in local encrypted = ab..""..aa return encrypted 

Didn't work
CometWolf #4
Posted 05 March 2014 - 09:49 PM
I didn't change anything… Did you even look at it?
KittenTheEpic #5
Posted 07 March 2014 - 12:36 AM
Fixed. I put x:sub instead of string.sub

LOCK THIS,
theoriginalbit #6
Posted 07 March 2014 - 12:55 AM
LOCK THIS,
threads rarely get locked in Ask a Pro.