Ho bisogno di convertire esadecimale in un decimale in VB.NET. Ho trovato diversi esempi in C#, ma quando ho provato a convertire in VB.NET non ho avuto successo. Un esempio di numero esadecimale che sto cercando di convertire è "A14152464C203230304232323020572F544947455234352E".Come si converte hex in decimale usando VB.NET?
risposta
Questo è un numero di 24 byte (192 bit); che valore ti aspetti?
noti che è possibile utilizzare Convert
di fare un sacco di lavoro grungy qui - per esempio (in C#):
string hex = "A14152464C203230304232323020572F544947455234352E";
byte[] raw = new byte[hex.Length/2];
for (int i = 0; i < raw.Length ; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2,2), 16);
}
Come si ottiene da raw
ad un numero dipende da ciò che si ritiene che il numero è ...
visivo traduzione cortesia di base di .NET Reflector (anche se il "-1" sembra strano):
Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
Dim raw As Byte() = New Byte((hex.Length/2) - 1) {}
Dim i As Integer
For i = 0 To raw.Length - 1
raw(i) = Convert.ToByte(hex.Substring((i * 2), 2), &H10)
Next i
Non so quale dovrebbe essere il numero. Lockhead Martin ha appena iniziato a spedire questi dati e non ha terminato la documentazione. In questo momento siamo in una modalità di scoperta. Dovrei semplicemente riassumere i valori di Raw (i) per ottenere il valore numerico? – user38349
No; sarebbe male ... il problema è: cosa fare con 192 bit?che potrebbe essere 3 long (Int64), 6 ints (Int32), 2 decimali, un BigInteger, ecc ... ma è big-endian, little-endian, ecc ... il binario di decodifica ha bisogno di un * piccolo * bit di conoscenza sulla fonte. –
Il valore -1 è corretto, la sintassi della dichiarazione dell'array VB fornisce il limite superiore dell'array mentre C# fornisce il numero di elementi nell'array. Poiché l'array è a base zero, il limite superiore è uno in meno del numero di elementi. – MarkJ
Scrivine uno tu stesso.
È necessario eseguire il tokenize della stringa, quindi iniziare da destra e proseguire verso sinistra.
int weight = 1;
While Looping
{
If (token(i) == "F") { DecimalValue += 15 * weight; }
If (token(i) == "E") { DecimalValue += 14 * weight; }
If (token(i) == "D") { DecimalValue += 13 * weight; }
If (token(i) == "C") { DecimalValue += 12 * weight; }
If (token(i) == "B") { DecimalValue += 11 * weight; }
If (token(i) == "A") { DecimalValue += 10 * weight; }
else { DecimalValue += token(i) * weight; }
weight = weight * 16;
}
Qualcosa del genere.
Dim hex As String
hex = "A14152464C203230304232323020572F544947455234352E"
Dim dec As Long
Dim hexpart As String
For x As Integer = 1 To (hex.Length/2)
hexpart = hex.Substring((x * 2) - 2, 2)
dec = Int32.Parse(hexpart, System.Globalization.NumberStyles.HexNumber)
Debug.Print("Hex = " + hex + ",HexPart = " + hexpart + ", Dec = " + dec.ToString + Environment.NewLine)
Next
Questo non funziona per Decimal e l'esadecimale è troppo lungo per l'intero ... ma si ottiene l'idea. Potresti dividerlo e ricombinarlo.
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = A1, Dec = 161
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 41, Dec = 65
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 52, Dec = 82
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 46, Dec = 70
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 4C, Dec = 76
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 20, Dec = 32
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 42, Dec = 66
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 32, Dec = 50
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 30, Dec = 48
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 20, Dec = 32
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 57, Dec = 87
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 2F, Dec = 47
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 54, Dec = 84
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 49, Dec = 73
Hex = A14152464C203230304232323020572F544947455234352E,HexPart = 47, Dec = 71
Ciò convertire i vostri s tring in un array di byte:
Dim hex As String = "A14152464C203230304232323020572F544947455234352E"
Dim len As Integer = hex.Length \ 2
Dim data(len - 1) As Byte
For i As Integer = 0 to len - 1
data(i) = Convert.ToByte(hex.Substring(i * 2, 2), 16)
Next
Private Function toByte(ByVal Shex As String) As List(Of Byte)
Const cvtCH As Integer = 2
Dim retval As New List(Of Byte)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToByte(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Function toU32(ByVal Shex As String) As List(Of UInt32)
Const cvtCH As Integer = 8
Dim retval As New List(Of UInt32)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToUInt32(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Function toU64(ByVal Shex As String) As List(Of UInt64)
Const cvtCH As Integer = 16
Dim retval As New List(Of UInt64)
Dim rmndr As Integer
rmndr = Shex.Length Mod cvtCH
If rmndr <> 0 Then Shex = Shex.PadLeft(Shex.Length + cvtCH - rmndr, "0"c)
For x As Integer = 0 To Shex.Length - 1 Step cvtCH
retval.Add(Convert.ToUInt64(Shex.Substring(x, cvtCH), 16))
Next
Return retval
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'unsigned 32 bit max = FFFFFFFF
'unsigned 64 bit max = FFFFFFFF
'signed 32 bit max = 7FFFFFFF
'signed 64 bit max = 7FFFFFFF
Dim hexS As String = "A14152464C203230304232323020572F544947455234352E"
Dim hexS2 As String = "A14152464C203230304232323020572F54494745523435"
toByte(hexS)
toU32(hexS)
toU64(hexS)
End Sub
Per valori esadecimali che in realtà non richiedono una classe bignum da lavorare, è possibile utilizzare la funzione di conversione normale, ma al numero il prefisso "& H". VB interpreta "& H" nel testo come "questo è un numero esadecimale", proprio come fa nel codice.
dim n = Cint("&H" & text)
È possibile utilizzare la funzione Val
in Visual Basic per convertire un valore esadecimale in un valore decimale. Questo viene fatto prefissando la stringa con "&H"
, per indicare a Visual Basic che questo è un valore esadecimale e convertirlo in un numero.
Dim Value As Integer = Val("&H" & YourHexadecimalStringHere)
Dim hex As String = "A1B2C3D4"
Dim int As Integer = Val("&H" & hex)
provato le altre risposte e questo è stato quello che ha funzionato per me:
Convert.ToInt32(yourHEXString, 16)
- 1. Come convertire hex in rgb usando Java?
- 2. Chrome DevTools converte tutti i colori HEX in RGB
- 3. Come si converte un oggetto in CSV usando JavaScript?
- 4. efficiente convertire tra Hex, binario e decimale in C/C++
- 5. Come si converte un numero decimale in un numero intero in C#?
- 6. Come si converte Int/Decimal in float in C#?
- 7. Converte una stringa in intero con decimale in Python
- 8. Come si digita il binario letterale in VB.NET?
- 9. Come si converte cdt in makefile?
- 10. Come si aggiunge una clausola 'where' usando VB.NET e LINQ?
- 11. Come posso convertire da esadecimale a decimale in Python?
- 12. dump hex galleggiante in C++
- 13. Come convertire String in Hex e Hex in String?
- 14. VB.NET Come posso ridurre int a 1 decimale?
- 15. Come si converte un'immagine base64?
- 16. GhostText usando VB.Net
- 17. Come si converte NSString in NSData?
- 18. Come si converte Double [] in double []?
- 19. Come si converte la cstring in NSString?
- 20. Come si converte LPCWSTR in const char *?
- 21. Come si converte SYS_GUID() in varchar?
- 22. Come si converte LF in CRLF?
- 23. Come si formatta un decimale?
- 24. Come si converte un numero binario in un BigInteger in Java?
- 25. Come si rappresenta una griglia hextile/hex in memoria?
- 26. come convertire hex in base64
- 27. Converte le stringhe Python in float esplicitamente usando la virgola o il punto come separatori
- 28. Come posso convertire da int a hex in Golang?
- 29. Cosa succede quando si converte double (o float) in ints?
- 30. come analizzare hex int o decimal in Python
Che numero si aspetta? Questo è 192 bit ... molto più grande della maggior parte dei tipi insiti ... –
(nota che ho aggiunto un commento alla tua domanda) –