Hi all,
I'm creating a binary file to transmit to a third party that contains images and information about each image. The file uses a record length format, so each record is a particular length. The beginning of each record is the Record Length Indicator, which is 4 characters long and represents the length of the record in Big Endian format.
I'm using a BinaryWriter to write to the file, and for the Record Length Indicator I'm using Encoding.Default.
The problem I'm having is that there is one character in one record that is displaying as a "?" because it is unrecognized. My algorithm to build the string for the record length indicator is this:
private string toBigEndian(int value)
{
string returnValue = "";
string binary = Convert.ToString(value, 2).PadLeft(32, '0');
List<int> binaryBlocks = new List<int>();
binaryBlocks.Add(Convert.ToInt32(binary.Substring(0, 8), 2));
binaryBlocks.Add(Convert.ToInt32(binary.Substring(8, 8), 2));
binaryBlocks.Add(Convert.ToInt32(binary.Substring(16, 8), 2));
binaryBlocks.Add(Convert.ToInt32(binary.Substring(24, 8), 2));
foreach (int block in binaryBlocks)
{
returnValue += (char)block;
}
Console.WriteLine(value);
return returnValue;
}
It takes the length of the record, converts it to 32-bit binary, converts that to chunks of 8-bit binary, and then converts each chunk to its appropriate character. The string that is returned here does contain the correct characters, but when it's written to the file, one character is unrecognized. This is how I'm writing it:
//fileWriter is BinaryWriter and record is Encoding.Default
fileWriter.Write(record.GetBytes(toBigEndian(length)));
Perhaps I'm using the wrong type of encoding? I've tried UTF-8, which should work, but it gives me extra characters sometimes.
Thanks in advance for your help.