Posted 21 June 2013 - 03:41 PM
Hello Pro's,
I just encountered a very strange problem : I was building a string representing bytes I wanted to write to a file. For faster loading and saving, I decided on using fs.open() file handles. I added the byte 0xED (278d) to the string (with string.char(0xED)), which produced the expected results, but after saving the string to the file a HEX-Editor showed me that insted of 0xED the handle actually comitted 0x3D to the file. Messing around a bit I found out that every value >127 will be strangely deformed when writing to a file, both with fs.open() and io.open(). The only solution I found was using fs/io.open(.., "wb") to write in binary mode, which is (sadly) much slower but in this case precise. The whole reason I was using strings is because it allowed me to read my bytes from a fs.readAll() result string.
Is this problem related to the CC Java implementation or is the textfile loading/saving function actually supposed to work this way?
I just encountered a very strange problem : I was building a string representing bytes I wanted to write to a file. For faster loading and saving, I decided on using fs.open() file handles. I added the byte 0xED (278d) to the string (with string.char(0xED)), which produced the expected results, but after saving the string to the file a HEX-Editor showed me that insted of 0xED the handle actually comitted 0x3D to the file. Messing around a bit I found out that every value >127 will be strangely deformed when writing to a file, both with fs.open() and io.open(). The only solution I found was using fs/io.open(.., "wb") to write in binary mode, which is (sadly) much slower but in this case precise. The whole reason I was using strings is because it allowed me to read my bytes from a fs.readAll() result string.
Is this problem related to the CC Java implementation or is the textfile loading/saving function actually supposed to work this way?
local hFile = fs.open("/test", "w")
hFile.write(string.char(0xED))
hFile.close()
.
.
.
local hFile = fs.open("/test", "r")
local iByte = string.byte(hFile.readAll(), 1)
hFile.close()
assert(iByte == 0xED, "It will fail...")