| Blockland Forums > Modification Help |
| Working with binary data, or perhaps just making a zip archive. |
| (1/2) > >> |
| DrenDran:
For all I know, the standard file handles only work with text data, so is it possible to export binary data, even if it's byte by byte, to a file? |
| Truce:
I've never tried this, but see what happens if you use \x## (hex) to try to export byte values. EDIT: intToChar.cs --- Code: ---function intToChar(%val) { eval("%chr = \"\\x" @ convertBase(%val,"0123456789","0123456789ABCDEF") @ "\";"); return %chr; } function convertBase(%val,%atype,%btype) { %vlen = strLen(%val); %alen = strLen(%atype); %blen = strLen(%btype); for(%i = 0; %i < %vlen; %i++) %sum += striPos(%atype,getSubStr(%val,%i,1)) * mPow(%alen,%vlen - %i - 1); while(1) { %rem = %sum % %blen; %new = getSubStr(%btype,%rem,1) @ %new; %sum = mFloor(%sum / %blen); if(!%sum) break; } return %new; } --- End code --- --- Quote ---==>exec("config/intToChar.cs"); Executing config/intToChar.cs. ==>$f = new FileObject(); ==>$f.openForWrite("config/test.txt"); ==>$f.writeLine(intToChar(135) @ intToChar(217) @ intToChar(78)); ==>$f.close(); ==>$f.delete(); --- End quote --- ByteRead.cs (C#) --- Code: ---using System; using System.IO; public class ByteRead { public static void Main(string[] args) { BinaryReader reader = new BinaryReader(File.Open(args[0],FileMode.Open)); while(true) { try { Console.WriteLine(reader.ReadByte()); } catch(EndOfStreamException e) { break; } } reader.Close(); } } --- End code --- --- Quote ---> csc ByteRead.cs > ByteRead test.txt 135 217 78 13 10 --- End quote --- Seems to work. Just remember the writeLine in Torque appends \r\n. |
| DrenDran:
This line: eval("%chr = \"\\x" @ convertBase(%val,"0123456789","0123456789ABCDEF") @ "\";"); Wouldn't it crash the server because it accesses a local variable in a global scope? |
| Truce:
--- Quote from: DrenDran on December 19, 2010, 10:13:34 PM ---This line: eval("%chr = \"\\x" @ convertBase(%val,"0123456789","0123456789ABCDEF") @ "\";"); Wouldn't it crash the server because it accesses a local variable in a global scope? --- End quote --- Nope. Eval runs in the scope it's called in. |
| DrenDran:
--- Quote from: Truce on December 19, 2010, 10:18:09 PM ---Nope. Eval runs in the scope it's called in. --- End quote --- Wow. I'll try this out later. Might take a while to get a full zip program working, but whatever. |
| Navigation |
| Message Index |
| Next page |