0

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 "î"

2
  • 1
    So.. What is the problem you're encountering? An Exception? Strange results? Commented Mar 25, 2019 at 12:58
  • in the new text file, the values ​​remain in the hex format Commented Mar 25, 2019 at 13:02

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

i have no idea, why the output is so strange !! it should be coordinates
@HMZ either you are using the wrong encoding, or your data is wrong. I Updated my code with a way to specify which encoding you're using.
How does the output look like ?
@HMZ With UTF8 is ?Ex?jH@'←,↑?((??☻ ♫??Z?8HH'←,↑?(@?Ex?jHH. None of the encodings I tried gave coordinates as result. Maybe your data is wrong. Where did you get the file?
@HMZ well, I don't know this file type, but maybe it doesn't give what you're expecting it to give. How do you know it gives coordinates as an hex string that represents chars?
|
0
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);

1 Comment

there is an OutOfRangeException here Select(x => Convert.ToByte(hex.Substring(x, 2), 16) it should be Enumerable.Range(0, hex.Length-1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.