Conversion d'hexagone en chaîne
-
05-09-2019 - |
Question
Je dois vérifier un string
situé à l'intérieur d'un paquet que je reçois sous forme de tableau de byte
. Si j'utilise BitConverter.ToString()
, je reçois les octets comme string
avec des tirets (f.e .: 00-50-25-40-A5-FF).
J'ai essayé la plupart des fonctions que j'ai trouvé après une googler rapide, mais la plupart d'entre eux ont le paramètre d'entrée de type string
et si je les appelle à la string
avec des tirets, il lève une exception.
I besoin d'une fonction qui se hex (comme string
ou comme byte
) dans le string
qui représente la valeur hexadécimale (f.e .: 0x31 = 1). Si le paramètre d'entrée est string
, la fonction doit reconnaître des tirets (exemple « 47-61-74-65-77-61-79-53-65-72-76-65-72 »), parce que BitConverter
ne convertit pas correctement .
La solution
Comme ça?
static void Main()
{
byte[] data = FromHex("47-61-74-65-77-61-79-53-65-72-76-65-72");
string s = Encoding.ASCII.GetString(data); // GatewayServer
}
public static byte[] FromHex(string hex)
{
hex = hex.Replace("-", "");
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);
}
return raw;
}
Autres conseils
string str = "47-61-74-65-77-61-79-53-65-72-76-65-72";
string[] parts = str.Split('-');
foreach (string val in parts)
{
int x;
if (int.TryParse(val, out x))
{
Console.Write(string.Format("{0:x2} ", x);
}
}
Console.WriteLine();
Vous pouvez diviser la chaîne au -
Convertir le texte à ints (int.TryParse)
Sortie int comme chaîne hexadécimale {0: x2}
Pour le support Unicode:
public class HexadecimalEncoding
{
public static string ToHexString(string str)
{
var sb = new StringBuilder();
var bytes = Encoding.Unicode.GetBytes(str);
foreach (var t in bytes)
{
sb.Append(t.ToString("X2"));
}
return sb.ToString(); // returns: "48656C6C6F20776F726C64" for "Hello world"
}
public static string FromHexString(string hexString)
{
var bytes = new byte[hexString.Length / 2];
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return Encoding.Unicode.GetString(bytes); // returns: "Hello world" for "48656C6C6F20776F726C64"
}
}
Votre référence à « 0x31 = 1 » me fait penser que vous êtes en train d'essayer de convertir les valeurs ASCII en chaînes - dans ce cas, vous devriez utiliser quelque chose comme Encoding.ASCII.GetString (Byte [])
string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);
//Output: 2274
De https://msdn.microsoft.com/en-us/ bibliothèque / bb311038.aspx
Si vous avez besoin du résultat en tant que tableau d'octets, vous devez passer directement sans changer une chaîne, puis changer de nouveau à octets.
Dans votre exemple, le (f.e .: 0x31 = 1
) est le code ASCII. Dans ce cas, pour convertir une chaîne (des valeurs hexagonales) à des valeurs ASCII utiliser:
Encoding.ASCII.GetString(byte[])
byte[] data = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
string ascii=Encoding.ASCII.GetString(data);
Console.WriteLine(ascii);
La console affiche: 1234567890