Differenze tra le versioni di "Ottenere un numero dalla sua memorizzazione in formato Big-Endian"
Riga 14: | Riga 14: | ||
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>' '' | + | <FONT Color=gray>' ''Legge i 4 byte:''</font> |
For c = 1 To 4 | For c = 1 To 4 | ||
Read #fl, c | Read #fl, c | ||
Riga 34: | Riga 34: | ||
fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read | fl = Open "<FONT Color=gray>''/percorso/del/file''</font>" For Read | ||
− | For a = 0 To | + | For a = 0 To 3 |
Read #fl, b | Read #fl, b | ||
i += b * 2 ^ (8 * (2 - a)) | i += b * 2 ^ (8 * (2 - a)) | ||
Riga 57: | Riga 57: | ||
For c = 2 To 0 Step -1 | For c = 2 To 0 Step -1 | ||
Read #fl, b | Read #fl, b | ||
− | i += b * 2 ^ (8 * c) <FONT Color=gray>' ''o anche: i += Shl(CInt(b), 8 * c)''</font> | + | i += b * 2 ^ (8 * c) <FONT Color=gray>' ''o anche: '''i += Shl(CInt(b), 8 * c)'''''</font> |
Next | Next | ||
fl.Close | fl.Close | ||
− | + | ||
Print i | Print i | ||
− | + | ||
'''End''' | '''End''' | ||
o anche: | o anche: | ||
Riga 77: | Riga 77: | ||
i = CInt(c) | i = CInt(c) | ||
− | For b = 1 To | + | For b = 1 To 4 |
Read #fl, c | Read #fl, c | ||
i = Shl(i, 8) | i = Shl(i, 8) |
Versione delle 18:22, 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, c i = Shl(i, 8) Or b Next Print i fl.Close End
oppure: [Nota 1]
Public Sub Main() Dim fl As File Dim i As Integer Dim a, b As Byte fl = Open "/percorso/del/file" For Read For a = 0 To 3 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() 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 = 2 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
o anche:
Public Sub Main() Dim fl As File Dim b, c As Byte Dim i As Integer fl = Open "/percorso/del/file" For Read Read #fl, c i = CInt(c) For b = 1 To 4 Read #fl, c i = Shl(i, 8) i = i Or (c And 127) Next Print i fl.Close 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[3] Dim i As Integer fl = Open "/percorso/del/file" For Read ' Legge i tre 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[3] Dim i As Integer fl = Open "/percorso/del/file" For Read bb.Read(fl, 0, bb.Count) fl.Close i = bb[2] i += bb[1] * 256 ' &0100 i += bb[0] * 65536 ' &010000 Print i End
oppure:
Public Sub Main() Dim fl As File Dim bb As New Byte[3] Dim i As Integer fl = Open "/percorso/del/file" For Read bb.Read(fl, 0, bb.Count) fl.Close i = (bb[2] Or ((bb[1] * CInt(2 ^ 8)))) Or (bb[0] * CInt(2 ^ 16)) ' oppure anche così: ' (bb[2] Or (Shl(CInt(bb[1]), 8))) Or Shl(CInt(bb[0]), 16) 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[3] 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) i = Val("&" & s) Print i End
Note
[1] Questa soluzione è stata proposta da Top Fuel, membro del forum www.gambas-it.org