简体   繁体   中英

Visual c# hex code to text file

I am in the process of trying to create a small program that will convert hex to a text file so I can read what is in it. What I mean is I have a .dat file but I would like to know what it says in the fields. I would then like to have it save each value to line that looks like this

INSERT INTO `database` VALUES ('813000', '12ÐÇXOÀñ°ü', '0', '0', '0', '0', '0', '0', '0', '0', '28', '0', '0', '3900340', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '0', '0', '0', '00', '00', '00', '0000', '0000', '0', '0', '138');

This is the hex code

98B10100476C69747465727942656C740000000014001900000000000000000000000000E505000000000000000000005400000000000000000088138813000000000000000035000000000000000000000000000000000052696768747E636C69636B7E746F7E65717569707E6F6E7E77616973742E0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

I know what this says because I went through and did it by hand but this is a very long way to do it. Above hex tranlates in the form I need it to

INSERT INTO `cq_itemtype` VALUES ('111000', 'GlitteryBelt', '20', '0', '25', '0', '0', '0', '0', '0', '0', '0', '1509', '0', '0', '0', '84', '0', '0', '0', '0', '5000', '5000', '0', '0', '0', '0', '0', '0', '0', '53', '00', '00', '00', '0000', '0000', '0', '0', '0');

I am new to C# and still learning. I have thousands of hex code like above that I need to translate... so even a simple code that will give me what each section of hex means would be appreciated.

You can try this method:

public string ConvertHexToString(string hexInput, System.Text.Encoding encoding)
{
    int numberChars = hexInput.Length;
    byte[] bytes = new byte[numberChars / 2];
    for (int i = 0; i < numberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
    }
    return encoding.GetString(bytes);
}

And use like:

string hex = "Your hex";
string result = ConvertHexToString(hex, System.Text.Encoding.Unicode);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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