Thejuster (Admin)
Guru^2
Messaggi: 2298
Iscritto: 04/05/2008
|
Buongiorno
sto cercando di convertire delle strutture native in c#.
ho qualche problemino di tipi ma sto cercando di venirne a capo.
Ho trovato un progetto, che è riuscito a convertire in javascript strutture native.
ora vorrei convertire questo codice per poterlo fare anche da C#
il codice è il segente
Codice sorgente - presumibilmente VB.NET |
/** * BinaryReader * * @param mixed buffer * @param {number} start optional * @param {number} end optional */ function BinaryReader( mixed, start, end ) { var buffer; if( typeof mixed === "string" ) { var uint8; var i, length; length = mixed.length; buffer = new ArrayBuffer(length); uint8 = new Uint8Array(buffer); for ( i=0; i<length; ++i ) { uint8[i] = mixed.charCodeAt(i) & 0xff; } } else if( mixed instanceof ArrayBuffer ) { buffer = mixed; } else if ( mixed instanceof Uint8Array ) { buffer = mixed.buffer; } else { throw new Error('BinaryReader() - Undefined buffer type'); } this.buffer = buffer; this.view = new DataView( buffer, start || 0 , end || buffer.byteLength); this.offset = 0; this.length = ( end || buffer.byteLength ) - ( start || 0 ) }
|
per avere quei tipi in javascript il tizio ha usato un trick
ecco il codice
Codice sorgente - presumibilmente Delphi |
/** * Struct Constructor * * @param {string[]} C like structure */ function Struct() { var args, unsigned; var i, count, total = 0, size, len; var type, name, func; var out = {}; len = arguments.length; for( i = 0; i < len; ++i ) { args = arguments[i].match(/(unsigned\s)?(bool|char|short|int|long|float|double)\s([a-zA-Z0-9_-]+)(\[(\d+)\])?;?/); unsigned = !!args[1]; type = args[2].toLowerCase(); name = args[3]; count = args[5] ? parseInt(args[5], 10) : 1; switch( type ) { case "bool": size=1; func = "int8"; break; case "char": size=1; func = "int8"; break; case "short": size=2; func = "int16"; break; case "int": size=4; func = "int32"; break; case "long": size=4; func = "int32"; break; case "float": size=4; func = "float32"; break; case "double": size=8; func = "float64"; break; default: throw new Error("Struct() - Undefined type '" + type + "'."); } func = "get" + ( unsigned ? "U" + func : func.charAt(0).toUpperCase() + func.substr(1) ); total += size * count; out[ name ] = { func: func, count: count }; } this._list = out; this.size = total; }
|
Di conseguenza uint8 che è un tipo primitivo in C# è definito come sbyte
al momento ho fatto qualcosa del genere
basandomi su quel primo esempio
Codice sorgente - presumibilmente C# |
public void BinaryReader(string mixed, sbyte start = 0, sbyte end = 0) { byte[] buffer; int i = 0; int lenght = 0; sbyte[] uint8; lenght = mixed.Length; buffer = new byte[lenght ]; uint8 = new sbyte[buffer. Length]; for (i = 0; i < lenght; ++i) { uint8[i] = sbyte.Parse(string.Format("{0}", ((int)mixed[i]) & 0xff)); } buffer = buffer; }
|
Ora per quanto riguarda la parte string, funziona senza problemi.
Il problema succede quando devo implementare la parte binaria.
Come posso convertire
questo
Codice sorgente - presumibilmente C# / VB.NET |
this.buffer = buffer;
this.view = new DataView( buffer, start || 0 , end || buffer.byteLength);
this.offset = 0;
this.length = ( end || buffer.byteLength ) - ( start || 0 )
|
in c#?
qualche aiutino?
Ho provato in questo modo
ma non sò se è corretto
Codice sorgente - presumibilmente C# / VB.NET |
sbyte[] view = new sbyte[buffer.Length];
for (int _i = start; _i < end; _i++)
{
view[i] = uint8[i];
}
lenght = (end != 0 ? end : buffer.Length) - (start != 0 ? start : 0);
|
Ultima modifica effettuata da Thejuster il 26/10/2018 alle 12:29
|