2

So I was wondering if there any possibility to read a plain text bytes sequence in hexadecimal from a text file?

The Bytes Are Saved On to A Text File in Text Format

e.g. :

string text = 
  @"0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
    0xFF, 0xFF, 0x00, 0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00";

Here I don't mean File.ReadAllBytes(filename)

What I tried is reading the text file with File.ReadAllText(filename) but sadly thats in a string Format not in the byte format...

We need to convert these text sequence to a byte array.

These text sequence are actual bytes stored in a text file.

Thanks in Advance..

Harsh Raj
  • 33
  • 4
  • Just for clarity: does file include the commas and spaces as shown? Any line breaks? – Joel Coehoorn Sep 22 '20 at 15:34
  • 2
    Are you being pranked? Whoever gave you this cannot be serious. – Fildor Sep 22 '20 at 15:34
  • 1
    For reference, that is almost certainly _not_ a text file. Looks like the beginnings of an exe. – cHao Sep 22 '20 at 15:34
  • yes file does include commas and spaces. yes after every 12 comma there is a line break – Harsh Raj Sep 22 '20 at 15:36
  • 1
    Oh, so the file is those actual characters ( ie: a text editor shows you "0x4D, 0x5A, ..."), and you have to convert the text to a sequence of bytes? – cHao Sep 22 '20 at 15:38
  • Dimitry i already stated above the strings are the actual bytes... we cant use encoding there – Harsh Raj Sep 22 '20 at 15:39
  • cHao yes bro thats what i need – Harsh Raj Sep 22 '20 at 15:40
  • Need to clarify that, then. I (and i'm sure others) have been assuming you're listing out the literal byte values in the file, and that's a very different question. – cHao Sep 22 '20 at 15:41
  • 6
    Can we all just quietly agree that whoever devised this file needs to be stared at mercilessly until they ask why, and then be educated? – Marc Gravell Sep 22 '20 at 15:42
  • cHao yes dude the text sequence listed in the text file are actual bytes... and i need a method in **C#** to read those as byte array from the text file. – Harsh Raj Sep 22 '20 at 15:44
  • 1
    @MarcGravell Had to read your comment 3x and now I am laughing tears. Excellent suggestion, though! Let's do that! – Fildor Sep 22 '20 at 15:44

2 Answers2

5

If I understand you right, you have a string like this

  string text =
    @"0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
      0xFF, 0xFF, 0x00, 0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00";

and you want to obtain byte[] (array). If it's your case, you can try matching with a help of regular expressions:

  using System.Linq;
  using System.Text.RegularExpressions; 

  ...

  byte[] result = Regex
    .Matches(text, @"0x[0-9a-fA-F]{1,2}")     // 0xH or 0xHH where H is a hex digit
    .Cast<Match>()
    .Select(m => Convert.ToByte(m.Value, 16)) // convert each match to byte
    .ToArray();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
3

Try this:

var result = 
    File.ReadAllText(filename)
   .Split(',')
   .Select(item => byte.Parse(item.Trim().Substring(2), NumberStyles.AllowHexSpecifier))
   .ToArray();
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276