Author Topic: Working with binary data, or perhaps just making a zip archive.  (Read 1098 times)

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?

I've never tried this, but see what happens if you use \x## (hex) to try to export byte values.

EDIT:

intToChar.cs

Code: [Select]
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;
}

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();

ByteRead.cs (C#)

Code: [Select]
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();
}
}

Quote
> csc ByteRead.cs
> ByteRead test.txt
135
217
78
13
10

Seems to work. Just remember the writeLine in Torque appends \r\n.
« Last Edit: December 19, 2010, 01:35:08 PM by Truce »

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?

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?

Nope. Eval runs in the scope it's called in.

Nope. Eval runs in the scope it's called in.
Wow.
I'll try this out later.
Might take a while to get a full zip program working, but whatever.

Char 0 can never be stored in a strong, sorry. Find a way to do it without char 0.

As usual, Truce does something amazing that probably won't be used for anything.
eh's like a magician.

As usual, Truce does something amazing that probably won't be used for anything.
eh's like a magician.
Char 0 can never be stored in a strong, sorry. Find a way to do it without char 0.
We'll have to see.
Though it's still cool though.