2009-03-13 13 views
11

Je dois convertir un hexagone en un nombre décimal en VB.NET. Trouvé plusieurs exemples en C#, mais quand j'ai essayé de convertir en VB.NET je n'ai pas réussi. Un exemple d'un nombre hexadécimal que j'essaye de convertir est "A14152464C203230304232323020572F544947455234352E".Comment convertir un hex en décimal en utilisant VB.NET?

+0

Quel numéro attendez-vous? C'est 192 bits ... beaucoup plus gros que la plupart des types intégrés ... –

+0

(note J'ai ajouté un commentaire sur votre question) –

Répondre

6

C'est un nombre de 24 octets (192 bits); Quelle valeur attendez-vous?

Notez que vous pouvez utiliser Convert faire beaucoup de travail grungy ici - par exemple (en 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); 
    } 

Comment vous obtenez de raw à un certain nombre dépend de ce que vous pensez que le nombre est ...

avec l'aimable autorisation de traduction Visual Basic de .NET Reflector (bien que le "-1" semble étrange):

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 
+0

Je ne sais pas quel devrait être le nombre. Lockhead Martin vient de commencer à expédier ces données et ils n'ont pas fini la documentation. Nous sommes en mode découverte en ce moment. Devrais-je juste résumer les valeurs Raw (i) pour obtenir le numérique? – user38349

+0

Non; ce serait mauvais ... le problème est: que faire avec 192 bits?cela pourrait être 3 longs (Int64), 6 ints (Int32), 2 décimales, un BigInteger, etc ... mais est-ce big-endian, little-endian, etc ... décodage binaire a besoin d'un * peu * peu de connaissances à propos de la source. –

+0

Le -1 est correct, la syntaxe de déclaration de tableau VB donne la limite supérieure du tableau tandis que C# donne le nombre d'éléments dans le tableau. Puisque le tableau est zéro, la limite supérieure est inférieure au nombre d'éléments. – MarkJ

1

Écrivez-en un vous-même.

Vous devrez marquer la chaîne, puis commencer à partir de la droite et continuer vers la gauche.

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; 
} 

Quelque chose comme ça.

1
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 

Cela ne fonctionnera pas pour décimal et hexadécimal est trop long pour entier ... mais vous voyez l'idée. Vous pourriez le diviser et recombiner.

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 
0

Cela vous permet de convertir vos s Tring dans un tableau d'octets:

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 
1
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 
15

Pour les valeurs hexagonales qui ne nécessitent pas réellement une classe bignum pour travailler avec, vous pouvez utiliser la fonction de conversion normale, mais le préfixe du numéro avec « & H ». VB interprète "& H" dans le texte comme signifiant "ceci est un nombre hexadécimal", exactement comme dans le code.

dim n = Cint("&H" & text) 
4

Vous pouvez utiliser la fonction Val dans Visual Basic pour convertir une valeur hexadécimale à une valeur décimale. Ceci est fait en préfixant la chaîne avec "&H", pour indiquer à Visual Basic qu'il s'agit d'une valeur hexadécimale, puis convertissez cela en nombre.

Dim Value As Integer = Val("&H" & YourHexadecimalStringHere) 
0
Dim hex As String = "A1B2C3D4" 
Dim int As Integer = Val("&H" & hex) 
0

Essayé les autres réponses et ce fut celui qui a fonctionné pour moi:

Convert.ToInt32(yourHEXString, 16) 

Documentation

Questions connexes