1

I've currently written a code for converting a string to an array of bytes and then writing those bytes to an buffer byte array. However, for some reason, the alignment part of the code seems to stop the execution of the program. I've testing it enough to know that it's the "int DynamicAlign.." part, but I can't figure out why it's happening.

public void WriteStr( string myString )
{
    byte[] myBytes = System.Text.Encoding.ASCII.GetBytes( myString );

    for( int i = 0; i < myBytes.Length; i ++ )
    {
        Buffer[ BytePeek ] = myBytes[ i ];
        BytePeek ++;
    }

    int DynamicAlign = ((myBytes.Length + 1) % ByteAlign != 0)
        ? ByteAlign - ((myBytes.Length + 1) % ByteAlign)
        : 0;
    BytePeek += (ushort)(1 + DynamicAlign);
}

If you don't know how byte alignment works, I found this as extra info: http://pastebin.com/tXzLWpBG

The extra "+ 1" and "1 +" are for taking into account the null terminating string at the end of the read sequence.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
user3519915
  • 129
  • 1
  • 3
  • 13
  • 3
    `seems to stop the execution of the program` - what do you mean, does it crash, give an error, loop forever (unlikely), what happens? – 500 - Internal Server Error May 07 '14 at 19:57
  • Is there a reason the solution found in [Converting a string to bytes](http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array) doesn't mean your needs? Why reinvent the wheel? – mason May 07 '14 at 19:58
  • @mason: I believe the reason that doesn't work is because it's not actually handling padding the buffer. The OP is buffering bytes and has to pad them to align them properly, which tells me this method is run multiple times with many different values. – Mike Perrenoud May 07 '14 at 20:00
  • What do you do with `BytePeek`? That's not obvious from the code you posted.. – Mike Dinescu May 07 '14 at 20:04
  • What's the value of `ByteAlign` here? – Mike Perrenoud May 07 '14 at 20:05
  • ByteAlign is a user inputted value. Currently its set to one, but it can be changed to change the alignment of the buffer. BytePeek is the current "peek"(write/read) position in the buffer. The execution simply stops, the program won't crash, and it doesn't seem to freeze either. The point of the dynamic alignment is force empty bytes in front of the data to add padding(alignment). – user3519915 May 07 '14 at 20:13
  • 1
    There is no real reason why some basic math (as in `int DynamicAlign` line) can "stop" execution. Clearly `% 1` is constant, but it should have no impact on the code - you need to get better details of what "simply stops" mean or show small standalone piece of code that reproduces the issue. – Alexei Levenkov May 07 '14 at 20:17
  • I honestly wish I could. However, the code will continue to run when I comment out the DynamicAlign code. It literally simply stops execution, the console cursor still seems to blink though. -- Just found out the DynamicAlign forces a first chance divide by zero exception. – user3519915 May 07 '14 at 20:25

1 Answers1

0

Alright, so the issue was that I was not setting the alignment for the write buffer, thus it gave the division by zero error with the modulo operation, since the byte alignment was preset to 0...

user3519915
  • 129
  • 1
  • 3
  • 13