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

string to binary (and back)

Started by KingofGamesYami, 08 April 2014 - 12:40 AM
KingofGamesYami #1
Posted 08 April 2014 - 02:40 AM
Is it possible to convert a string (ei "PA55W0RD") to a binary sequence and back easily? By that I mean without creating a function like this:
 string.gsub(s, a, "01100001")
string.gsub(s, b, "01100010")
string.gsub(s, c, "01100011")
string.gsub(s, d, "01100100")
I have been looking around, and the only thing I found anything to is a mention of the Bit API.
theoriginalbit #2
Posted 08 April 2014 - 02:46 AM
what is the need for you to do this? I ask as the solution may vary depending on what you want it for.
KingofGamesYami #3
Posted 08 April 2014 - 02:47 AM
what is the need for you to do this? I ask as the solution may vary depending on what you want it for.
I was thinking of using it for storing passwords and/or user data.
Edit: possibly used with a function like this:

function getPass()
 local pFile = io.open("pass", "r")
 return pFile:read()
 pFile:close()
end
Edited on 08 April 2014 - 08:02 PM
KingofGamesYami #4
Posted 09 April 2014 - 04:36 AM
Since nobody has replied, I will explain a bit more about my situation:
I want to convert a string (eg. password) into a binary number, which I will then use some sort of equation on to make a number. That number will be saved to a file, and that file will be read when the login refers to a password. Each password is customised to its own file, which are going to be saved as the username's binary sequence. I hope to make it as hard as possible for a human to decode what the passwords are.
edit: by username & login i mean the user supplies a username, when the computer asks for a username and password, it will save the (correct) data as a local variable, stored as "SessionID".
Edited on 09 April 2014 - 02:38 AM
theoriginalbit #5
Posted 09 April 2014 - 04:41 AM
sorry about not responding, didn't get a notification, nor see your reply.

You're better to use hashing for this. it is far more secure, there is no way for a user or program to read the hash and know the password. just search the forums for SHA256 and you'll find a couple of implementations. then when you want to check the user input against the hashed password, just hash their input, if their input was the correct password the two hashes will match.

There are other advantages to hashing passwords which you could also find with a simple search on these forums, I've typed out the advantages many a time, and don't really have to time to now.
Edited on 09 April 2014 - 02:43 AM
Dog #6
Posted 11 April 2014 - 03:24 AM
I was thinking of using it for storing passwords and/or user data.
Edit: possibly used with a function like this:

function getPass()
local pFile = io.open("pass", "r")
return pFile:read()
pFile:close()
end
I may be wrong, but won't that function leave the file handle open by using 'return pFile:read()' ? I'm guessing you'd have to move pFile:close to immediately after the call to the function.
theoriginalbit #7
Posted 11 April 2014 - 03:45 AM
I was thinking of using it for storing passwords and/or user data.
Edit: possibly used with a function like this:

function getPass()
local pFile = io.open("pass", "r")
return pFile:read()
pFile:close()
end
I may be wrong, but won't that function leave the file handle open by using 'return pFile:read()' ? I'm guessing you'd have to move pFile:close to immediately after the call to the function.
or place a comma after the reading

local function getPass()
  local h = fs.open('pass', 'r')
  return h.readAll(), h.close()
end
Dog #8
Posted 11 April 2014 - 03:54 AM
or place a comma after the reading

local function getPass()
  local h = fs.open('pass', 'r')
  return h.readAll(), h.close()
end
Didn't know you could do that - thanks, again, bit :)/>
theoriginalbit #9
Posted 11 April 2014 - 04:08 AM
Didn't know you could do that - thanks, again, bit :)/>
its to do with order of operations, it invokes the functions left to right. so basically its just multiple return values. example:

local function doStuff()
  return "hello", "world"
end

local foo, bar = doStuff()
however the close function doesn't return anything, so in the case of reading all and closing the file, you'd only get one return value; the file contents.
Edited on 11 April 2014 - 02:09 AM