HMZ
HMZ

Reputation: 11

How to convert hex data from a text file into string file

I have a list of data in a text file, which is written in hex like this:

AE 66 55 78 FF 6A 48 40 CA BC 1B 2C 18 94 28 28 
CF EA 02 00 02 51 23 05 0E F2 DD 5A E5 38 48 48 
CA BC 1B 2C 18 94 28 40 EE B6 65 87 E3 6A 48 48 
..

And I want to convert the values into char in another text file

I have tried this in c#:

 private static void OpenFile()
            {
ASCIIEncoding ascii = new ASCIIEncoding();

                string str = string.Empty;
                using (System.IO.BinaryReader br = new System.IO.BinaryReader
                    (new System.IO.FileStream(
                        "hexa2.txt",
                        System.IO.FileMode.Open,
                        System.IO.FileAccess.Read,
                        System.IO.FileShare.None), Encoding.UTF8))
 using (System.IO.StreamWriter sw = new System.IO.StreamWriter("sms23.txt"))
                {
                   str = @"the path";
                    Byte[] bytes = ascii.GetBytes(str);
                    foreach (var value in bytes)
                        sw.WriteLine("{0:X2}", value);
                    sw.WriteLine();
                    String decoded = ascii.GetString(bytes);
                    sw.WriteLine("Decoded string: '{0}'", decoded);
                }
           }

I expect that every byte will be converted into char. e.g. "EE" is "î"

Upvotes: 0

Views: 1477

Answers (2)

Magnetron
Magnetron

Reputation: 8543

You have a text file, not a binary file, so you must read the hex strings, convert then to the respective number and then get the corresponding char for that number.

// string input = @"EE B6 45 78 FF 6A 48 40 CA BC 1B 2C 18 94 28 28 
// CF EA 02 00 00 00 00 00 0E F2 DD 5A E4 38 48 48 
// CA BC 1B 2C 18 94 28 40 EE B6 45 78 FF 6A 48 48 ";
string input = File.ReadAllText("yourFile.txt");
string output = new string(
    input.Replace("\n"," ").Replace("\r","")
        .Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries)
        .Select(x=>(char)Convert.ToInt32(x,16))
        .ToArray()
);
File.WriteAllText("newFile.txt",output);
//Output: î¶ExÿjH@ʼ←,↑?((Ïê☻     ♫òÝZä8HHʼ←,↑?(@î¶ExÿjHH

You don't specified an encoding, so I'm just casting the hex to char directly. To Specify the encoding you should use the bellow code

byte[] dataArray = 
    input.Replace("\n"," ").Replace("\r","")
        .Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries)
        .Select(x=>(byte)Convert.ToInt32(x,16))
        .ToArray();
string output = Encoding.UTF8.GetString(dataArray);

Where you can replace Encoding.UTF8 for the desired one.

Upvotes: 1

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

string hex = File.ReadAllText("file.txt").Replace(" ","").Replace(Environment.NewLine,"");
var result = Enumerable.Range(0, hex.Length)
             .Where(x => x % 2 == 0)
             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
             .ToArray();


File.WriteAllBytes( "file.bin", result);

Upvotes: 0

Related Questions