2

I am trying to make a string of hex like

4100200062006C0061006E006B002000630061006E0076006100730020007200650063006F006D006D0065006E00640065006400200066006F007200200046006F007200670065002000650064006900740069006E00670020006F006E006C0079002E

Turn into ASCII and look like:

A blank canvas recommended for Forge editing only.

The variable for the hex is collected from a file that I opened into the program, reading a specific address like so:

BinaryReader br = new BinaryReader(File.OpenRead(ofd.FileName));
string mapdesc = null;

for (int i = 0x1C1; i <= 0x2EF; i++)
{
    br.BaseStream.Position = i;
    mapdesc += br.ReadByte().ToString("X2");
}

richTextBox1.Text = ("" + mapdesc);

Now that I have the mapdesc, I made it print into the richtextbox, and it just looked like a line of hex. I wanted it too look like readable ASCII.

In Hex Editor, the other side reading in ANSI looks like

A. .b.l.a.n.k. .c.a.n.v.a.s. .r.e.c.o.m.m.e.n.d.e.d. .f.o.r. .F.o.r.g.e. .e.d.i.t.i.n.g. .o.n.l.y

The dots are 00s in the hex view, so I believe with the ASCII format, they should be nothing so that I get the readable sentence which is how the game reads it. What would I have to do to convert mapdesc into ASCII?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Alex
  • 35
  • 5
  • Possible duplicate of [C# hex to ascii](http://stackoverflow.com/questions/5613279/c-sharp-hex-to-ascii) – Keith Nicholas Apr 03 '17 at 22:41
  • 2
    The data looks like UTF16 (big endian). You should be able to read it directly with a `StreamReader` object, with the only possible complicating being the need to specify `Encoding.BigEndianUnicode` in the constructor. There should be no need to convert binary to hex and then back to some other numeric or text format. – Peter Duniho Apr 03 '17 at 22:41
  • Whoa okay yeah I perfectly get you. I don't know why I was making it into hex then back to text. I didn't think that StreamReader could actually work in this case. I'll try it out and see what happens. – Alex Apr 03 '17 at 22:47
  • Judging from what others figured out it looks like you are starting your `for` loop a byte too far. Even after switching to streamreader I feel like you want to start from `0x1C1-4`, not `0x1C1`. – Quantic Apr 03 '17 at 22:49
  • There is no text but encoded text. When you read text you have to use the encoding it was written with. .NET's Base Class Library supports this very well. First, find the spec for this file format to see which encoding it uses. – Tom Blodget Apr 04 '17 at 00:20

1 Answers1

0

To be fair, the output matches the decoded output exactly, the issue is actually with the input data.

If you look closely, you will notice that ever other pair of characters is 00, using some simple heuristics, we can determine that we have 16 bit words here, 4 hex chars.

The problem that you are facing, and the reason for the . characters, is that while decoding this as UTF-8, every other character will be null.

You have two solutions to solve this:

To continue decoding in UTF-8, remove every other null character from the string, all the _00_s.

Or

Decode at UTF-16

If you choose this option, you still have an issue with your data - the very first word is only 8 bits, which would cause a shift among ever other byte; to decode in UTF-16, prepend an additional 00 at the beginning of the data blob ( or start your loop one position sooner )

Matt Clark
  • 27,671
  • 19
  • 68
  • 123