Differenze tra le versioni di "Ottenere un numero dalla sua memorizzazione in formato Big-Endian"
Riga 6: | Riga 6: | ||
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. | 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 fl As File | ||
Riga 12: | Riga 12: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
<FONT Color=gray>' ''Legge i 4 byte:''</font> | <FONT Color=gray>' ''Legge i 4 byte:''</font> | ||
Riga 24: | Riga 24: | ||
fl.Close | fl.Close | ||
− | + | End | |
oppure: | oppure: | ||
− | + | Public Sub Main() | |
Dim fl As File | Dim fl As File | ||
Riga 33: | Riga 33: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
For c = 3 To 0 Step -1 | For c = 3 To 0 Step -1 | ||
Riga 44: | Riga 44: | ||
Print i | Print i | ||
− | + | 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() | |
Dim fl As File | Dim fl As File | ||
Riga 52: | Riga 52: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
<FONT Color=gray>' ''Legge i 4 byte costitutivi del tipo "Intero":''</font> | <FONT Color=gray>' ''Legge i 4 byte costitutivi del tipo "Intero":''</font> | ||
Riga 65: | Riga 65: | ||
Print i | Print i | ||
− | + | End | |
===Usando un vettore di tipo Byte[] come appoggio=== | ===Usando un vettore di tipo Byte[] come appoggio=== | ||
− | + | Public Sub Main() | |
Dim fl As File | Dim fl As File | ||
Riga 75: | Riga 75: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
<FONT Color=gray>' ''Legge i 4 byte:''</font> | <FONT Color=gray>' ''Legge i 4 byte:''</font> | ||
Riga 87: | Riga 87: | ||
fl.Close | fl.Close | ||
− | + | End | |
− | |||
oppure: | oppure: | ||
− | + | Public Sub Main() | |
Dim fl As File | Dim fl As File | ||
Riga 96: | Riga 95: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
bb.Read(fl, 0, bb.Count) | bb.Read(fl, 0, bb.Count) | ||
Riga 109: | Riga 108: | ||
Print i | Print i | ||
− | + | End | |
− | |||
oppure: | oppure: | ||
− | + | Public Sub Main() | |
Dim fl As File | Dim fl As File | ||
Riga 118: | Riga 116: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
bb.Read(fl, 0, bb.Count) | bb.Read(fl, 0, bb.Count) | ||
Riga 131: | Riga 129: | ||
Print i | Print i | ||
− | + | End | |
− | oppure usando le risorse ''stringa'' con successiva conversione in valore numerico | + | oppure usando le risorse ''stringa'' con successiva conversione in valore numerico: |
− | + | Public Sub Main() | |
Dim fl As File | Dim fl As File | ||
Riga 140: | Riga 138: | ||
Dim i As Integer | Dim i As Integer | ||
− | fl = Open "<FONT Color= | + | fl = Open "<FONT Color=darkgreen>''/percorso/del/file''</font>" For Read |
bb.Read(fl, 0, bb.Count) | bb.Read(fl, 0, bb.Count) | ||
Riga 152: | Riga 150: | ||
Print i | Print i | ||
− | + | End |
Versione attuale delle 08:49, 23 giu 2024
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