Read bytes from min and max hex offsets with C#

1

I want to be able to read bytes from a min and max hexadecimal offset that is given through 2 strings string minO and string maxO. After that I just need to convert the bytes to a string.

//Setup byte reader.
FileStream fs = new FileStream(EditOpen.FileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
long length = fs.Length;
//Read bytes to textBox1.
string minO = 0x00001844;
string maxO = ???
br.BaseStream.Position = minO;
byte[] example = br.ReadBytes(0x000003FC);
textBox1.Text = BitConverter.ToString(example);

0x00001844 is the min value and 0x00001C3F is the max value and 0x000003FC is the difference in hex.

This is the output:

73-20-67-6C-61-73-73-5F-62-72-65-61-6B-20-22-30-22-2E-2E-00-73-20-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-20-73-20-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-2E-73-20-70-65-72-6B-5F-65-78-74-72-61-42-72-65-61-74-68-20-22-39-39-39-22-2E-2E-2E-2E-2E-2E-2E-00-73-20-6D-6F-74-69-6F-6E-54-72-61-63-6B-65-72-52-61-6E-67-65-20-22-39-39-39-39-22-2E-2E-2E-2E-2E-2E-00-00-00-73-20-6C-6F-62-62-79-5F-61-6E-69-6D-61-74-69-6F-6E-53-70-65-65-64-20-22-36-30-22-2E-2E-2E-2E-2E-2E-2E-00-00-73-20-67-6C-61-73-73-5F-73-68-61-74-74-65-72-65-64-5F-73-63-61-6C-65-20-22-30-22-2E-2E-2E-2E-2E-2E-2E-00-00-73-20-67-6C-61-73-73-5F-66-72-69-6E-67-65-5F-6D-61-78-73-69-7A-65-20-22-31-30-30-22-2E-2E-2E-2E-2E-2E-2E-00-73-20-75-69-5F-67-61-6D-65-74-79-70-65-20-22-73-64-22-00-00-73-20-67-5F-67-61-6D-65-74-79-70-65-20-22-73-64-22-00-00-00-73-20-70-61-72-74-79-5F-67-61-6D-65-74-79-70-65-20-22-73-64-22-00-00-00-73-20-67-61-6D-65-74-79-70-65-20-22-22-00-00-00-6D-61-70-6E-61-6D-65-00-56-41-5F-46-4F-52-57-41-52-44-00-00-73-20-61-63-74-69-76-65-61-63-74-69-6F-6E-20-22-70-65-72-6B-5F-62-75-6C-6C-65-74-50-65-6E-65-74-72-61-74-69-6F-6E-4D-75-6C-74-69-70-6C-69-65-72-20-33-30-3B-70-65-72-6B-5F-64-69-76-65-47-72-61-76-69-74-79-53-63-61-6C-65-20-2E-30-31-3B-70-65-72-6B-5F-64-69-76-65-56-65-6C-6F-63-69-74-79-20-30-3B-70-65-72-6B-5F-64-69-76-65-44-69-73-74-61-6E-63-65-43-68-65-63-6B-20-39-39-39-3B-70-65-72-6B-5F-65-78-74-65-6E-64-65-64-4D-65-6C-65-65-52-61-6E-67-65-20-39-39-39-3B-70-65-72-6B-5F-67-72-65-6E-61-64-65-44-65-61-74-68-20-61-63-31-33-30-5F-31-30-35-6D-6D-5F-6D-70-3B-70-65-72-6B-5F-77-65-61-70-52-61-74-65-4D-75-6C-74-69-70-6C-69-65-72-20-2E-30-31-3B-63-67-5F-67-75-6E-5F-78-20-36-3B-67-6C-61-73-73-5F-66-72-69-6E-67-65-5F-6D-61-78-63-6F-76-65-72-61-67-65-20-31-30-30-30-30-30-30-3B-6D-6F-74-64-20-5E-31-47-65-74-5E-35-53-68-69-69-74-5E-37-4F-6E-5E-31-4B-69-64-73-20-20-20-5E-39-42-79-20-5E-32-42-55-53-54-45-44-5E-33-54-57-49-4E-4B-49-45-20-20-5E-34-40-7B-3D-33-5E-32-3C-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-5E-31-4A-4F-49-4E-20-5E-35-77-77-77-2E-58-62-6F-78-5E-37-33-36-30-5E-31-44-65-76-65-6C-6F-70-65-72-73-2E-5E-35-63-6F-6D-5E-32-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-3D-38-3B-70-65-72-6B-5F-73-70-72-69-6E-74-4D-75-6C-74-69-70-6C-69-65-72-20-35-3B-70-6C-61-79-65-72-5F-6C-61-73-74-53-74-61-6E-64-43-72-61-77-6C-53-70-65-65-64-53-63-61-6C-65-20-32-3B-70-6C-61-79-65-72-2B-73-70-72-69-6E-74-54-69-6D-65-20-39-39-39

I would like to have the output as plain text instead of hex. How can I do this?

c#
encoding
hex
asked on Stack Overflow Mar 31, 2012 by John_Dong • edited Mar 31, 2012 by aKzenT

2 Answers

1

Binary or Hexadecimal output:

You can use Convert.ToString(byte, base) to convert a byte to a hex string without any separators. Base can be 2 for binary or 16 for hexadecimal.

Use this in a for loop to convert all bytes:

StringBuilder builder = new StringBuilder();
foreach(byte b in example)
{
    builder.Append(Convert.ToString(b, 16).PadLeft(2,'0'));
}
textBox1.Text = builder.ToString();

Plain text encoding:

If you want to have plain text, you have to know the encoding of the file. For UTF8 you would use:

textBox1.Text = System.Text.Encoding.UTF8.GetString(example);

Tell me if this works for you.

answered on Stack Overflow Mar 31, 2012 by aKzenT • edited Mar 31, 2012 by aKzenT
1

If I get you correctly, you want to have binary values instead of hex? You may code this in following way:

var bytes = br.ReadBytes(0x000003FC)
    .Select(x => Convert.ToString(x, 2));

And then use String.Join to concat strings as you want. For example:

textBox1.Text = String.Join(" ", bytes);

To get plain text, ofc, you should use Encoding. In case of UTF8:

textBox1.Text = Encoding.UTF8.GetString(example);
answered on Stack Overflow Mar 31, 2012 by tukaef • edited Mar 31, 2012 by tukaef

User contributions licensed under CC BY-SA 3.0