Given a selection of bytes in a hex editor shows the file separates text with 00 bytes, it's a coloring file, with the material name, follolwed by a 3 byte hex code that determines colour. which is why it contains bytes like FF. the bytes are shown like this:
00 11 46 6F 6C 69 61 67 65 5F 45 76 65 72 67 72 65 65 6E 00 FF FF FF 00 0D 46 6F 6C 69 61 67 65 5F 42 69 72 63 68 00 80 A7 55
which translates into ascii as such:
Foliage_Evergreen�ÿÿÿ�
Foliage_Birch�€§U
How would I separate these bytes down into a list and convert them into text list of the bytes' values? I'm having trouble understanding how I'd go about doing it... this is what I'm doing right now:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
string line = System.IO.File.ReadAllText(openFileDialog1.FileName);
byte[] bytes = Encoding.ASCII.GetBytes(line);
List<string> listtext = line.Split('.').ToList();
listBox1.DataSource = listtext;
You shouldn’t do that, will crash for some files with encoding exception:
string line = System.IO.File.ReadAllText(openFileDialog1.FileName);
byte[] bytes = Encoding.ASCII.GetBytes(line);
Use File.ReadAllBytes instead, no need to read text then convert to bytes.
Then you’ll need to parse array of bytes into your records.
Based on your example data, your format uses 0 as field separator, and strings are prepended by their lengths. Here’s an example how to parse, untested:
static IEnumerable<(string, Color)> parse( byte[] data )
{
for( int p = 0; p < data.Length; )
{
// '\0'
if( 0 != data[ p++ ] ) throw new ApplicationException();
// String length byte
int length = data[ p++ ];
// The string; assuming the encoding is UTF8
string name = Encoding.UTF8.GetString( data, p, length );
p += length;
// '\0'
if( 0 != data[ p++ ] ) throw new ApplicationException();
// 3 color bytes, assuming the order is RGB
Color color = Color.FromArgb( 0xFF, data[ p ], data[ p + 1 ], data[ p + 2 ] );
p += 3;
yield return (name, color);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With