简体   繁体   English

如何在 C# 中从十六进制转换为转储

[英]How to convert From Hex To Dump in C#

I convert my Hex to dump to get special character like symbol but when I try to convert my "0x18" i "\" this value.我将我的十六进制转换为转储以获得像符号这样的特殊字符,但是当我尝试转换我的“0x18”我“\”这个值。 Can anyone give me solution regarding this matter.谁能给我关于这个问题的解决方案。

Here is my code:这是我的代码:

    public static string FromHexDump(string sText)
    {
        Int32 lIdx;
        string prValue ="" ;
        for (lIdx = 1; lIdx < sText.Length; lIdx += 2)
        {
            string prString = "0x" + Mid(sText, lIdx, 2);
            string prUniCode = Convert.ToChar(Convert.ToInt64(prString,16)).ToString();
            prValue = prValue + prUniCode;
        }
        return prValue;
    }

I used VB language.我用的是VB语言。 I have a database that already encrypted text to my password and the value is BAA37D40186D like this so I loop it by step 2 and it will like this 0xBA,0xA3,0x7D,0x40,0x18,0x6D and the VB result getting like this º£}@m我有一个数据库,它已经将文本加密到我的密码中,值是这样的BAA37D40186D所以我按步骤 2 循环它,它会喜欢这个0xBA,0xA3,0x7D,0x40,0x18,0x6D并且 VB 结果变得像这样º£}@m

You can use this code:您可以使用此代码:

var myHex = '\x0633';
var formattedString += string.Format(@"\x{0:x4}", (int)myHex);

Or you can use this code from MSDN ( https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types ):或者您可以使用 MSDN 中的此代码( https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types ):

string hexValues = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
string[] hexValuesSplit = hexValues.Split(' ');
foreach (string hex in hexValuesSplit)
{
    // Convert the number expressed in base-16 to an integer.
    int value = Convert.ToInt32(hex, 16);
    // Get the character corresponding to the integral value.
    string stringValue = Char.ConvertFromUtf32(value);
    char charValue = (char)value;
    Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
                        hex, value, stringValue, charValue);
}

The question is unclear - what is the database column's type?问题不清楚 - 数据库列的类型是什么? Does it contain 6 bytes, or 12 characters with the hex encoding of the bytes?它包含 6 个字节,还是包含 12 个字节的十六进制编码字符? In any case, this has nothing to do with special characters or encodings.无论如何,这与特殊字符或编码无关。

First, 0x18 is the byte value of the Cancel Character in the Latin 1 codepage, not the pound sign .首先,0x18 是拉丁语 1 代码页中取消字符的字节值,而不是井号 That's 0xA3.那是 0xA3。 It seems that the byte values in the question are just the Latin 1 bytes for the string in hex.问题中的字节值似乎只是十六进制字符串的拉丁语 1 字节。

.NET strings are Unicode (UTF16LE specifically). .NET 字符串是 Unicode(特别是 UTF16LE)。 There's no UTF8 string or Latin1 string.没有 UTF8 字符串或 Latin1 字符串。 Encodings and codepages apply when converting bytes to strings or vice versa.编码和代码页适用于将字节转换为字符串,反之亦然。 This is done using the Encoding class and eg Encoding.GetBytes这是使用Encoding类完成的,例如Encoding.GetBytes

In this case, this code will convert the byte to the expected string form, including the unprintable character :在这种情况下,此代码会将字节转换为预期的字符串形式,包括不可打印的字符:

new byte[] {0xBA,0xA3,0x7D,0x40,0x18,0x6D};
var latinEncoding=Encoding.GetEncoding(1252);
var result=latinEncoding.GetString(dbBytes);

The result is :结果是:

º£}@m

With the Cancel character between @ and m .@m之间使用取消字符。

If the database column contains the byte values as strings :如果数据库列包含字节值作为字符串:

  1. it takes double the required space and它需要两倍所需的空间和
  2. the hex values have to be converted back to bytes before converting to strings在转换为字符串之前,必须将十六进制值转换回字节

The x format is used to convert numbers or bytes to their hex form and vice versa. x格式用于将数字或字节转换为它们的十六进制形式,反之亦然。 For each byte value, ToString("x") returns the hex string.对于每个字节值, ToString("x")返回十六进制字符串。

The hex string can be produced from the original buffer with :十六进制字符串可以从原始缓冲区生成:

var dbBytes=new byte[] {0xBA,0xA3,0x7D,0x40,0x18,0x6D};
var hexString=String.Join("",dbBytes.Select(c=>c.ToString("x")));

There are many questions that show how to parse a byte string into a byte array .有很多问题展示了如何将字节字符串解析为字节数组 I'll just steal Jared Parson's LINQ answer :我只会窃取 Jared Parson 的 LINQ 答案:

public static byte[] StringToByteArray(string hex) {
    return Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}

With that, we can parse the hex string into a byte array and convert it to the original string :有了这个,我们可以将十六进制字符串解析为字节数组并将其转换为原始字符串:

var bytes=StringToByteArray(hexString);
var latinEncoding=Encoding.GetEncoding(1252);
var result=latinEncoding.GetString(bytes);

First of all u don't need dump but Unicode, I would recomend to read about unicode/encoding etc and why this is a problem with strings.首先,你不需要转储而是 Unicode,我建议阅读有关 unicode/encoding 等的信息,以及为什么这是字符串的问题。

PS: solution : StackOverflow PS:解决方案: StackOverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM