Note that there are some explanatory texts on larger screens.

plurals
  1. POPorting code containing unsigned char pointer in C to C#
    primarykey
    data
    text
    <p>I have this code in C that I need to port to C#:</p> <pre><code>void CryptoBuffer(unsigned char *Buffer, unsigned short length) { unsigned short i; for(i=0; i &lt; length; i++) { *Buffer ^= 0xAA; *Buffer++ += 0xC9; } } </code></pre> <p>I tried this:</p> <pre><code>public void CryptoBuffer(byte[] buffer, int length) { for(int i = 0; i &lt; length; i++) { buffer[i] ^= 0xAA; buffer[i] += 0xC9; } } </code></pre> <p>But the outcome doesn't match the one expected.</p> <p>According to the example, this:</p> <pre><code>A5 03 18 01... </code></pre> <p>should become this:</p> <pre><code>A5 6F 93 8B... </code></pre> <p>It also says the first byte is not encrypted, so that's why A5 stays the same.</p> <p><strong>EDIT for clarification</strong>: The specification just says you should skip the first byte, it doesn't go into details, so I'm guessing you just pass the sequence from position 1 until the last position to skip the first byte.</p> <p>But my outcome with that C# port is:</p> <pre><code>A5 72 7B 74... </code></pre> <p>Is this port correct or am I missing something?</p> <p><strong>EDIT 2</strong>: For further clarification, this is a closed protocol, so I can't go into details, that's why I provided just enough information to help me port the code, that C code was the one that was given to me, and that's what the specification said it would do. The real problem was that the "0xAA" was wrong in the specification, that's why the output wasn't the expected one. The C# code provided here and by the accepted answer are correct after all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload