RedSon, I tried using the trim() method, but it didn't do anything at all. I don't want the string to be 256 bytes, I want it to be the length of the actual filename, without the null bytes. I need to use string.Length to get the string length so I can truncate it down to fit the area it's being printed in or add an ellipsis (...) to the end.
And tlhintoq, since the actual filename has the null bytes between each character, using trim would be useless anyway since I would still have a string that is (string.Length + (string.Length - 1)) long. A 15 character string will have 14 null bytes if there is 1 between each character. I need to remove those null bytes. It's not a space, it's a "00" hex byte. Here is an example, the title in this example is "Prepare to Drop Premium Theme", the Hex for that is:
- 00 00 50 00 72 00 65 00 70 00 61 00 72 00 65 00 20 00 74 00
-
6F 00 20 00 44 00 72 00 6F 00 70 00 20 00 50 00 72 00 65 00
-
6D 00 69 00 75 00 6D 00 20 00 54 00 68 00 65 00 6D 00 65 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
-
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
and the ASCII is:
- ..P.r.e.p.a.r.e. .t.o. .D.r.o.p. .P.r.e.
-
m.i.u.m. .T.h.e.m.e.....................
-
........................................
-
........................................
-
........................................
-
........................................
-
................
The dots represent non-printable ASCII characters, the 00 bytes.
I did manage to throw something together, a method that works, but I feel like there's got to be a better way. Here's my method:
- private string getThemeName()
-
{
-
string themeName;
-
br.BaseStream.Position = 0x410;
-
byte[] nameBytesNull = br.ReadBytes(256);
-
List<byte> nameBytesList = new List<byte>();
-
foreach (byte single in nameBytesNull)
-
{
-
if (single.ToString() != "0")
-
{
-
nameBytesList.Add(single);
-
}
-
}
-
byte[] nameBytes = new byte[nameBytesList.Count];
-
nameBytesList.CopyTo(nameBytes);
-
themeName = ASCIIEncoding.ASCII.GetString(nameBytes);
-
return themeName;
-
}
Basically I'm reading the 256 bytes that I need into a byte array, then I iterate through each byte in that array and ask if it's ToString() value is 0, if it isn't then I add it to the byte List. Then I create another byte array based on the length of the byte List, and copy that byte List to that byte array. THEN I use the ASCIIEncoding GetString() method to convert the resulting byte array to it's ASCII string value and return it.
This works, but like I said, I feel like there must be a simpler way. Any ideas?