Differenze tra le versioni di "Ottenere un numero dalla sua memorizzazione in formato Big-Endian"

Da Gambas-it.org - Wikipedia.
 
(11 versioni intermedie di uno stesso utente non sono mostrate)
Riga 5: Riga 5:
  
  
Mostriamo alcune possibiità, con le quali poter restituire, dalla lettura da un file di suoi 3 dati memorizzati in ''Big-Endian'', l'effettivo valore che fu a suo tempo memorizzato nel file.
+
Mostriamo alcune possibiità, con le quali poter restituire, dalla lettura da un file di suoi 4 dati memorizzati in ''Big-Endian'', l'effettivo valore che fu a suo tempo memorizzato nel file.
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
 
   Dim fl As File
 
   Dim fl As File
   Dim b, c As Byte  
+
   Dim c, b As Byte
 
   Dim i As Integer
 
   Dim i As Integer
    
+
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
+
   fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 +
 +
<FONT Color=gray>' ''Legge i 4 byte:''</font>
 +
  For c = 1 To 4
 +
    Read #fl, b
 +
    i = Shl(i, 8) Or b
 +
  Next
 +
 +
  Print i
 
    
 
    
<FONT Color=gray>' ''Leggerà tre byte:''</font>
+
  fl.Close
  For b = 1 To 3
 
    Read #fl, c
 
    i = Shl(i, 8) Or c
 
  Next
 
 
 
  Print i
 
 
 
  fl.Close
 
 
    
 
    
 
  '''End'''
 
  '''End'''
 
+
oppure:
oppure: <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
 
'''Public''' Sub Main()
 
 
 
  Dim fl As File
 
  Dim i As Integer
 
  Dim a, b As Byte
 
 
 
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 
 
 
  For a = 0 To 2
 
    Read #fl, b
 
    i += b * 2 ^ (8 * (2 - a))
 
  Next
 
 
 
  fl.Close
 
 
 
  Print i
 
 
 
'''End'''
 
 
 
oppure una piccola variante del codice precedente:  
 
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
Riga 53: Riga 32:
 
   Dim c As Short
 
   Dim c As Short
 
   Dim i As Integer
 
   Dim i As Integer
 
 
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 
 
 
  For c = 2 To 0 Step -1
 
    Read #fl, b
 
    i += b * 2 ^ (8 * c)  <FONT Color=gray>' ''o anche: i += Shl(CInt(b), 8 * c)''</font>
 
  Next
 
 
 
  fl.Close
 
 
 
  Print i
 
 
 
'''End'''
 
 
oppure:
 
'''Public''' Sub Main()
 
 
   
 
   
   Dim fl As File
+
   fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
  Dim b, c As Byte
+
  Dim i As Integer
+
  For c = 3 To 0 Step -1
 
+
    Read #fl, b
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
+
    i += b * 2 ^ (8 * c)   <FONT Color=gray>' ''o anche:'' '''i += Shl(CInt(b), 8 * c)'''</font>
 
+
  Next
  Read #fl, c
+
  i = CInt(c)
+
  fl.Close
 
+
  For b = 1 To 2
+
  Print i
    Read #fl, c
+
    i = Shl(i, 8)
 
    i = i Or (c And 127)
 
  Next
 
 
 
  Print i
 
 
 
  fl.Close
 
 
 
 
  '''End'''
 
  '''End'''
 
 
oppure usando le risorse ''stringa'' con successiva conversione in valore numerico in rappresentazione decimale:
 
oppure usando le risorse ''stringa'' con successiva conversione in valore numerico in rappresentazione decimale:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
Riga 97: Riga 51:
 
   Dim s As String
 
   Dim s As String
 
   Dim i As Integer
 
   Dim i As Integer
 +
 +
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 +
 +
<FONT Color=gray>' ''Legge i 4 byte costitutivi del tipo "Intero":''</font>
 +
  Read #fl, s, 4
 +
 +
  fl.Close
 +
 +
  s = Hex(Asc(s, 1), 2) & Hex(Asc(s, 2), 2) & Hex(Asc(s, 3), 2) & Hex(Asc(s, 4), 2)
 
    
 
    
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
+
  i = Val("&" & s)
 
    
 
    
<FONT Color=gray>' ''Legge tre byte:''</font>
+
  Print i
  Read #fl, s, 3
 
 
 
  fl.Close
 
 
 
  s = Hex(Asc(s, 1), 2) & Hex(Asc(s, 2), 2) & Hex(Asc(s, 3), 2)
 
 
 
  i = Val("&" & s)
 
 
 
  Print i
 
 
    
 
    
 
  '''End'''
 
  '''End'''
  
  
====Usando un vettore di tipo Byte[] come appoggio====
+
===Usando un vettore di tipo Byte[] come appoggio===
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
 
   Dim fl As File
 
   Dim fl As File
   Dim bb As New Byte[3]
+
   Dim bb As New Byte[4]
 
   Dim i As Integer
 
   Dim i As Integer
    
+
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
+
   fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 +
 +
<FONT Color=gray>' ''Legge i 4 byte:''</font>
 +
  bb.Read(fl, 0, bb.Count)
 +
  bb.Reverse()
 +
 +
  i = Int@(bb.Data)
 
    
 
    
<FONT Color=gray>' ''Legge i tre byte:''</font>
+
  Print i
  bb.Read(fl, 0, bb.Count)
+
  bb.Reverse()
+
  fl.Close
 
 
  i = Int@(bb.Data)
 
 
 
  Print i
 
 
 
  fl.Close
 
 
    
 
    
 
  '''End'''
 
  '''End'''
Riga 139: Riga 93:
 
   
 
   
 
   Dim fl As File
 
   Dim fl As File
   Dim bb As New Byte[3]
+
   Dim bb As New Byte[4]
 
   Dim i As Integer
 
   Dim i As Integer
 
 
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 
 
 
  bb.Read(fl, 0, bb.Count)
 
 
 
  fl.Close
 
 
   
 
   
  i = bb[2]
+
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
  i += bb[1] * 256           <FONT Color=gray>' ''&0100''</font>
+
  i += bb[0] * 65536        <FONT Color=gray>' ''&010000''</font>
+
  bb.Read(fl, 0, bb.Count)
 
+
  Print i
+
  fl.Close
 
+
 +
  i = bb[3]
 +
  i += bb[2] * 256         <FONT Color=gray>' ''&0100''</font>
 +
  i += bb[1] * 65536        <FONT Color=gray>' ''&010000''</font>
 +
  i += bb[0] * 16777216    <FONT Color=gray>' ''&01000000''</font>
 +
 +
  Print i
 +
 
  '''End'''
 
  '''End'''
  
Riga 160: Riga 115:
 
    
 
    
 
   Dim fl As File
 
   Dim fl As File
   Dim bb As New Byte[3]
+
   Dim bb As New Byte[4]
 
   Dim i As Integer
 
   Dim i As Integer
 
+
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
+
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 
+
  bb.Read(fl, 0, bb.Count)
+
  bb.Read(fl, 0, bb.Count)
 
+
 
  fl.Close
+
  fl.Close
 
    
 
    
  i = (bb[2] Or ((bb[1] * CInt(2 ^ 8)))) Or (bb[0] * CInt(2 ^ 16))
+
  i = (bb[3] Or ((bb[2] * CInt(2 ^ 8))) Or (bb[1] * CInt(2 ^ 16)) Or (bb[0] * CInt(2 ^ 24)))
 
    
 
    
 
  <FONT Color=gray>' ''oppure anche così:''
 
  <FONT Color=gray>' ''oppure anche così:''
  '     '''(bb[2] Or (Shl(CInt(bb[1]), 8))) Or Shl(CInt(bb[0]), 16)'''</font>
+
  '   '''bb[3] Or (Shl(CInt(bb[2]), 8)) Or (Shl(CInt(bb[1]), 16)) Or (Shl(CInt(bb[0]), 24))'''</font>
 
      
 
      
  Print i
+
  Print i
 
    
 
    
 
  '''End'''
 
  '''End'''
 
+
oppure usando le risorse ''stringa'' con successiva conversione in valore numerico
o usando le risorse ''stringa'' con successiva conversione in valore numerico in rappresentazione decimale:
 
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
 
   Dim fl As File
 
   Dim fl As File
   Dim bb As New Byte[3]
+
   Dim bb As New Byte[4]
 
   Dim s As String
 
   Dim s As String
 
   Dim i As Integer
 
   Dim i As Integer
 +
 +
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
 +
 +
  bb.Read(fl, 0, bb.Count)
 
    
 
    
  fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read
+
  fl.Close
 
 
  bb.Read(fl, 0, bb.Count)
 
 
 
  fl.Close
 
 
    
 
    
  s = Hex(bb[0], 2) & Hex(bb[1], 2) & Hex(bb[2], 2)
+
  s = Hex(bb[0], 2) & Hex(bb[1], 2) & Hex(bb[2], 2) & Hex(bb[3], 2)
 
    
 
    
  i = Val("&" & s)
+
  i = Val("&" & s)
 
      
 
      
  Print i
+
  Print i
 
    
 
    
 
  '''End'''
 
  '''End'''
 
 
 
 
=Nota=
 
[1] Questa soluzione è stata proposta da ''[http://www.gambas-it.org/smf/index.php?action=profile;u=1038 Top Fuel]'', membro del forum www.gambas-it.org
 

Versione attuale delle 19:19, 20 set 2022

La circostanza è quella in cui si intende leggere i dati-byte di un valore all'interno di un file, memorizzati in formato Big-Endian a dimensione fissa, ottenendo così al termine della lettura l'effettivo numero corrispondente (ossia corrispondente esattamente alla disposizione dei dati-byte in formato Big-Endian).

Come sappiamo, se un valore è stato volutamente salvato in modalità Big-Endian all'interno di un file, la sua successiva lettura con un sistema, che opera con una modalità di memorizzazione dei dati in Little-Endian, non restituirà il valore esatto, ossia quello che si era volutamente memorizzato.
Così, ad esempio, se nel file è memorizzato il gruppo di tre byte-dati &h010305, corrispondente in rappresentazione decimale al numero 66309, una lettura in Little-Endian ruoterà i dati-byte di quel valore, come segue: &h050301, ordine di byte che corrisponde invece al numero 328449 !


Mostriamo alcune possibiità, con le quali poter restituire, dalla lettura da un file di suoi 4 dati memorizzati in Big-Endian, l'effettivo valore che fu a suo tempo memorizzato nel file.

Public Sub Main()

 Dim fl As File
 Dim c, b As Byte
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

' Legge i 4 byte:
 For c = 1 To 4
   Read #fl, b
   i = Shl(i, 8) Or b
 Next

 Print i
  
 fl.Close
 
End

oppure:

Public Sub Main()
 
 Dim fl As File
 Dim b As Byte
 Dim c As Short
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

 For c = 3 To 0 Step -1
   Read #fl, b
   i += b * 2 ^ (8 * c)   ' o anche: i += Shl(CInt(b), 8 * c)
 Next

 fl.Close

 Print i

End

oppure usando le risorse stringa con successiva conversione in valore numerico in rappresentazione decimale:

Public Sub Main()

 Dim fl As File
 Dim s As String
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

' Legge i 4 byte costitutivi del tipo "Intero":
 Read #fl, s, 4

 fl.Close

 s = Hex(Asc(s, 1), 2) & Hex(Asc(s, 2), 2) & Hex(Asc(s, 3), 2) & Hex(Asc(s, 4), 2)
  
 i = Val("&" & s)
  
 Print i
  
End


Usando un vettore di tipo Byte[] come appoggio

Public Sub Main()

 Dim fl As File
 Dim bb As New Byte[4]
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

' Legge i 4 byte:
 bb.Read(fl, 0, bb.Count)
 bb.Reverse()

 i = Int@(bb.Data)
  
 Print i

 fl.Close
  
End

oppure:

Public Sub Main()

 Dim fl As File
 Dim bb As New Byte[4]
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

 bb.Read(fl, 0, bb.Count)

 fl.Close

 i = bb[3]
 i += bb[2] * 256          ' &0100
 i += bb[1] * 65536        ' &010000
 i += bb[0] * 16777216     ' &01000000

 Print i

End

oppure:

Public Sub Main()
 
 Dim fl As File
 Dim bb As New Byte[4]
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

 bb.Read(fl, 0, bb.Count)
 
 fl.Close
 
 i = (bb[3] Or ((bb[2] * CInt(2 ^ 8))) Or (bb[1] * CInt(2 ^ 16)) Or (bb[0] * CInt(2 ^ 24)))
  
' oppure anche così:
'    bb[3] Or (Shl(CInt(bb[2]), 8)) Or (Shl(CInt(bb[1]), 16)) Or (Shl(CInt(bb[0]), 24))
   
 Print i
  
End

oppure usando le risorse stringa con successiva conversione in valore numerico

Public Sub Main()

 Dim fl As File
 Dim bb As New Byte[4]
 Dim s As String
 Dim i As Integer

 fl = Open "/percorso/del/file" For Read

 bb.Read(fl, 0, bb.Count)
  
 fl.Close
  
 s = Hex(bb[0], 2) & Hex(bb[1], 2) & Hex(bb[2], 2) & Hex(bb[3], 2)
  
 i = Val("&" & s)
   
 Print i
  
End