Blockland Forums > Modification Help
How might one convert a BINARY FILE to a base64 string?
Lugnut:
so I just used a default base64 conversion program in the linux distro i'm on on the attached picture, got this output
http://pastebin.com/raw.php?i=zamXunXK
I took said output, decoded it with the same program, and the picture turned out fine.
When I encoded the same image with the base64encode function posted in the OP, it gave me an entirely different string, which could not be decoded with the program in the distro.
Next tests:
encode string with PROGRAM, decode with torque
encode string with torque, decode with torque
retry encoding with torque, decode with program
Lugnut:
result; encoded with torque, decoded with program
didn't work. randomly, I decided to check it in a text editor
turns out it has no /r/n line return things anywhere, its just one big string, unlike the correct .png, which has returns all over the place. this might be why it isn't turning up properly.
encode with torque, decode with torque
...
the file vanished? the source encoded file remained, but the decoded copy seems to have never been created.
attempting encode with program, decode with torque...
crashed the server. beautiful. no it lagged processing it, I'm getting forgetloads of errors now...
error pertains to getSubStr... starting position must be greater than 0, starting position is -32.
what tha fack
Fluff-is-back:
--- Quote from: Lugnut1206 on June 15, 2012, 05:00:51 AM ---result; encoded with torque, decoded with program
didn't work. randomly, I decided to check it in a text editor
turns out it has no /r/n line return things anywhere, its just one big string, unlike the correct .png, which has returns all over the place. this might be why it isn't turning up properly.
encode with torque, decode with torque
...
the file vanished? the source encoded file remained, but the decoded copy seems to have never been created.
attempting encode with program, decode with torque...
crashed the server. beautiful. no it lagged processing it, I'm getting forgetloads of errors now...
error pertains to getSubStr... starting position must be greater than 0, starting position is -32.
what tha fack
--- End quote ---
Post your code.
Lugnut:
http://pastebin.com/raw.php?i=qH7znDG4
Destiny/Zack0Wack0:
--- Quote from: Lugnut1206 on June 15, 2012, 04:00:51 AM ---why is torquescript like that? can it cause crashes or some stuff?
--- End quote ---
Because in traditional C derived languages (which Torque is built on) an 0x00 byte, also known as the null character, is placed in a string in order to signify that the string is ended. It has to be like this, because in C a string is just a block of characters (bytes) and there is no reference to the actual length of a string, so instead it just traverses the block of characters until it hits a null character, which it assumes is the end of the string.
So when you read the bytes in a file, the file reader assumes you are reading a text file and stores the bytes into a string, so any null characters (0x00) will trigger the string to be cut off, so anything after the null character is ignored.
The PNG specification uses the null character for separation sections of the file: http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html
So you can't use Torque to read the file unless there is some unheard of way to read a file without Torque turning it into an ASCII string. Attempting to base64 encode or decode will result in a different or malformed base64 string because data has been lost.