2017-10-20 19 views
-1

Je suis actuellement assez nouveau pour C# programmation. Sur stackoverflow j'ai trouvé cette façon de convertir un nombre hexadécimal en tableau d'octets, mais pour des raisons inconnues quand je l'ai imprimé en console, j'ai obtenu quelque chose comme System.Byte[]. Comment puis-je imprimer les valeurs de ce tableau d'octets?C# lire le nombre hexadécimal et le convertir en octets, puis l'imprimer

public static void Main(string[] args) 
    { 
     string hex = "010000015908178fd8000f0fcaf1209a953c006900dd0e0022000e050101f0015001150351220142375308c700000036f10000601a520000000053000069de54000000205500000387570d4a9e50640000006f0001"; 
     byte[] array = null; 
     array = Enumerable.Range(0, hex.Length) // Converts hex string to byte array or am I worng? 
         .Where(x => x % 2 == 0) 
         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) 
         .ToArray(); 
     Console.WriteLine(array); 
    } 
+1

Votre question est trop large. Ce n'est pas clair du tout comment vous voulez imprimer les valeurs du tableau. Une façon serait d'imprimer simplement la valeur originale de 'hex'. Une autre méthode consiste à écrire 'foreach' pour imprimer chaque valeur. Une autre méthode consiste à utiliser 'string.Join()' pour générer une chaîne appropriée. Il n'est pas clair non plus si vous voulez imprimer les valeurs en hexadécimal ou en décimal. Pris littéralement, vous semblez être confus sur la façon d'imprimer des valeurs à partir d'un tableau. Voir par exemple marqué en double –

+0

string.Join a parfaitement fonctionné pour moi. Merci! – CaL17

+0

Dupliquer de https://stackoverflow.com/questions/32694766/how-to-print-array-informat-1-2-3-4-5-with-string-join-in-c et de https: //stackoverflow.com/questions/37663663/print-array-in-console-gives-system-int32. –

Répondre

0

Si vous préférez Linq:

string hex = "010000015908178fd8000f0fcaf1209a953c006900dd0e0022000e050101f0015001150351220142375308c700000036f10000601a520000000053000069de54000000205500000387570d4a9e50640000006f0001"; 

byte[] result = Enumerable 
    .Range(0, hex.Length/2) // The range should be hex.Length/2, not hex.Length 
    .Select(index => Convert.ToByte(hex.Substring(index * 2, 2), 16)) 
    .ToArray(); 

Tests. Lors de l'impression sur une collection (tableau dans le cas), s'il vous plaît, ne pas oublier de Concat ou Join éléments dans un string

// Let's concat (no separator) result back to string and compare with the original 
string test = string.Concat(result.Select(b => b.ToString("x2"))); 

Console.WriteLine(test == hex ? "passed" : "failed"); 

// Let's print out the array (we join items with a separator - ", " in the case) 
Console.WriteLine($"[{string.Join(", ", result)}]");  

Résultat:

passed 
[1, 0, 0, 1, 89, 8, 23, 143, 216, 0, ... , 100, 0, 0, 0, 111, 0, 1]  
+0

Nous avons déjà beaucoup d'exemples d'utilisation de 'String.Join()' pour imprimer un tableau. Comme https://stackoverflow.com/questions/37663663/print-array-in-console-gives-system-int32 et https://stackoverflow.com/questions/32694766/how-to-print-array-in- format-1-2-3-4-5-avec-string-join-in-c. Vous devriez voter pour fermer cette question en double, sans ajouter au fouillis. –

0

si vous voulez imprimer tous les octet, vous pouvez utiliser foreach, qui est le code correspondant.

foreach (byte bt in array) 
{ 
    Console.Write(bt+" "); 
} 

Sortie:

1 0 0 1 89 8 23 143 216 0 15 15 202 241 32 154 149 60 0 105 0 221 14 0 34 0 14 5 1 1 240 1 80 1 21 3 81 34 1 66 55 83 8 199 0 0 0 54 241 0 0 96 26 82 0 0 0 0 83 0 0 105 222 84 0 0 0 32 85 0 0 3 135 87 13 74 158 80 100 0 0 0 111 0 1