Jump to content




Help with strings.


5 replies to this topic

#1 KittenTheEpic

  • Banned
  • 17 posts

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)



#2 CometWolf

  • Members
  • 1,283 posts

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


#3 KittenTheEpic

  • Banned
  • 17 posts

Posted 05 March 2014 - 09:32 PM

View PostCometWolf, on 05 March 2014 - 09:24 PM, said:

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

#4 CometWolf

  • Members
  • 1,283 posts

Posted 05 March 2014 - 09:49 PM

I didn't change anything... Did you even look at it?

#5 KittenTheEpic

  • Banned
  • 17 posts

Posted 07 March 2014 - 12:36 AM

Fixed. I put x:sub instead of string.sub

LOCK THIS,

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 March 2014 - 12:55 AM

View PostKittenTheEpic, on 07 March 2014 - 12:36 AM, said:

LOCK THIS,
threads rarely get locked in Ask a Pro.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users