Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Conversioni Native
Forum - C# / VB.NET - Conversioni Native

Avatar
Thejuster (Admin)
Guru^2


Messaggi: 2298
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 12:07
Venerdì, 26/10/2018
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

  1. /**
  2.          * BinaryReader
  3.          *
  4.          * @param mixed buffer
  5.          * @param {number} start optional
  6.          * @param {number} end optional
  7.          */
  8.         function BinaryReader( mixed, start, end )
  9.         {
  10.                 var buffer;
  11.  
  12.                 if( typeof mixed === "string" ) {
  13.                         var uint8;
  14.                         var i, length;
  15.  
  16.                         length = mixed.length;
  17.                         buffer = new ArrayBuffer(length);
  18.                         uint8  = new Uint8Array(buffer);
  19.  
  20.                         for ( i=0; i<length; ++i ) {
  21.                                 uint8[i] = mixed.charCodeAt(i) & 0xff;
  22.                         }
  23.                 }
  24.                 else if( mixed instanceof ArrayBuffer ) {
  25.                         buffer = mixed;
  26.                 }
  27.                 else if ( mixed instanceof Uint8Array ) {
  28.                         buffer = mixed.buffer;
  29.                 }
  30.                 else {
  31.                         throw new Error('BinaryReader() - Undefined buffer type');
  32.                 }
  33.  
  34.                 this.buffer = buffer;
  35.                 this.view   = new DataView( buffer, start || 0 , end || buffer.byteLength);
  36.                 this.offset = 0;
  37.                 this.length = ( end || buffer.byteLength ) - ( start || 0 )
  38.         }



per avere quei tipi in javascript il tizio ha usato un trick
ecco il codice

Codice sorgente - presumibilmente Delphi

  1. /**
  2.          * Struct Constructor
  3.          *
  4.          * @param {string[]} C like structure
  5.          */
  6.         function Struct()
  7.         {
  8.                 var args, unsigned;
  9.                 var i, count, total = 0, size, len;
  10.                 var type, name, func;
  11.                 var out = {};
  12.  
  13.                 len = arguments.length;
  14.  
  15.                 for( i = 0; i < len; ++i ) {
  16.  
  17.                         args     =   arguments[i].match(/(unsigned\s)?(bool|char|short|int|long|float|double)\s([a-zA-Z0-9_-]+)(\[(\d+)\])?;?/);
  18.                         unsigned = !!args[1];
  19.                         type     =   args[2].toLowerCase();
  20.                         name     =   args[3];
  21.                         count    =   args[5] ? parseInt(args[5], 10) : 1;
  22.  
  23.                         switch( type ) {
  24.                                 case "bool":   size=1; func = "int8";    break;
  25.                                 case "char":   size=1; func = "int8";    break;
  26.                                 case "short":  size=2; func = "int16";   break;
  27.                                 case "int":    size=4; func = "int32";   break;
  28.                                 case "long":   size=4; func = "int32";   break;
  29.                                 case "float":  size=4; func = "float32"; break;
  30.                                 case "double": size=8; func = "float64"; break;
  31.                                 default:
  32.                                         throw new Error("Struct() - Undefined type '" + type + "'.");
  33.                         }
  34.        
  35.                         func   = "get" + ( unsigned ? "U" + func : func.charAt(0).toUpperCase() + func.substr(1) );
  36.                         total += size * count;
  37.  
  38.                         out[ name ] = {
  39.                                 func:  func,
  40.                                 count: count
  41.                         };
  42.                 }
  43.  
  44.  
  45.                 this._list = out;
  46.                 this.size  = total;
  47.         }



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#

  1. public void BinaryReader(string mixed, sbyte start = 0, sbyte end = 0)
  2.         {
  3.  
  4.             byte[] buffer;
  5.          
  6.                 int i = 0;
  7.                 int lenght = 0;
  8.                 sbyte[] uint8;
  9.                 lenght = mixed.Length;
  10.                 buffer = new byte[lenght];
  11.                 uint8 = new sbyte[buffer.Length];
  12.  
  13.  
  14.                 for (i = 0; i < lenght; ++i)
  15.                 {
  16.                     uint8[i] = sbyte.Parse(string.Format("{0}", ((int)mixed[i]) & 0xff));
  17.                 }
  18.  
  19.                 buffer = buffer;          
  20.  
  21.         }




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

  1. this.buffer = buffer;
  2.                 this.view   = new DataView( buffer, start || 0 , end || buffer.byteLength);
  3.                 this.offset = 0;
  4.                 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

  1. sbyte[] view = new sbyte[buffer.Length];
  2.  
  3.                 for (int _i = start; _i < end; _i++)
  4.                 {
  5.                     view[i] = uint8[i];
  6.                 }
  7.  
  8.                 lenght = (end != 0 ? end : buffer.Length) - (start != 0 ? start : 0);



Ultima modifica effettuata da Thejuster il 26/10/2018 alle 12:29


https://mire.forumfree.it/ - Mire Engine
C# UI Designer
PM Quote