Question

I am having the problem that whitespace of some sort is being inserted between characters when I am converting a Queue<byte> list into a string for comparison. I do not think that they are actual whitespace characters, however, because the Queue retains only seven values and when debugging I am still able to see the seven character values. See image:

Relevant code:

Queue<byte> bufKeyword = new Queue<byte>(7);

    // Remove old byte from queue and add new one
    if (bufKeyword.Count == 7) bufKeyword.Dequeue();
    bufKeyword.Enqueue((byte)fsInput.ReadByte());

    // Check buffer string for match
    StringBuilder bufKeywordString = new StringBuilder();

    foreach (byte qByte in bufKeyword) { 
        bufKeywordString.Append(Encoding.ASCII.GetString(BitConverter.GetBytes(qByte))); 
    }

    string _bufKeywordString = bufKeywordString.ToString();
    Console.WriteLine("{0}", _bufKeywordString); //DEBUG - SEE IMAGE

    StringBuilder bufWriteString = new StringBuilder();
    if (_bufKeywordString.StartsWith("time=")) //Does not work because of 'whitespace'
    {
        for (int i = 1; i < 25; i++) { bufWriteString.Append(fsInput.ReadByte()); }     // Read next 24 bytes
        fileWriteQueue.Enqueue(bufWriteString.ToString());                              // Add this data to write queue
        fileWriteQueueCount++;
        fileBytesRead += 24;                                                            // Change to new spot in file
    }
Was it helpful?

Solution

There is no BitConverter.GetBytes for byte argument. byte gets converted to short, and BitConverter.GetBytes(short) returns an array of two elements.

So instead of

bufKeywordString.Append(Encoding.ASCII.GetString(BitConverter.GetBytes(qByte)));

try

bufKeywordString.Append(Encoding.ASCII.GetString(new byte[] {qByte});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top