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

HELP! All my \13s are being converted to \10s

Started by EveryOS, 29 June 2018 - 11:59 PM
EveryOS #1
Posted 30 June 2018 - 01:59 AM
I tried the following code in the Lua console:

h = fs.open("x", "w")
h.write("\13")
h.close()
h=fs.open("x", "r")
string.byte(h.read())
h.close()

And the result was 13, even though I expected it to be 10

It's causing major flaws in a program that I am writing, it took a tremendous amount of debugging to find this issue, and I can't really escape it in the auto-generated file that easily
Edited on 30 June 2018 - 12:00 AM
KingofGamesYami #2
Posted 30 June 2018 - 02:52 AM
Carriage return (\r) and line feed (\n) are handled differently at the OS level by eg. OSX vs. Windows. Sometimes git will change these around, if you're using that for versino control.
Bomb Bloke #3
Posted 30 June 2018 - 10:05 AM
I'm not entirely sure that this is an "OS level" thing, but the behaviour is certainly decided according to the OS in use.

If this actually matters to your code, then odds are you should be using binary mode for your file handles instead:

h = fs.open("x", "wb")
h.write(13)
h.close()
h=fs.open("x", "rb")
result = h.read()
h.close()

If you really feel that you must use text mode, then convert through base64 or similar before writing to disk.
EveryOS #4
Posted 30 June 2018 - 07:22 PM
Whoa, binary mode might actually be really useful for what I am writing! It will decrease the amount of code, and increase effeciency
KingofGamesYami #5
Posted 30 June 2018 - 08:47 PM
I'm not entirely sure that this is an "OS level" thing

Yeah, I doubt the OS is actually doing anything. I suspect another program (eg. Git) is changing it for that reason.
EveryOS #6
Posted 30 June 2018 - 11:19 PM
I wasn't even using git, so it couldn't have. But using binary mode fixed it