What is the best way to handle the following situation in C#?
I have a server application written in C/C++.
For example
It creates a unsigned char
buffer with length 256
.
In this buffer the server stores the data the client sends to it. After storing, there are some cryptography checks with the received buffer.
I'm writing a client for this server in C#.
The problem is the buffer the server is expecting at fixed length of 256.
When I create a byte[]
array with content and total length of 256 the cryptography checks are failing.
The reason I found out is simple. The server expects a buffer of 256 bytes long. For example if the message is "Hello World"
, the rest of the buffer has to be 0-padded.
Or, better explained: the bytes need to be (unsigned) "204"
or (signed) "-52"
.
I think this is a C/C++ concept issue/problem.
To solve my problem, I am setting that value explicitly.
public static byte[] GetCBuffer(this byte[] data, int len)
{
byte[] tmp = new byte[len];
for(int i = 0; i < len; i++)
if(i < data.Length)
tmp[i] = data[i];
else
tmp[i] = 204;
return tmp;
}
Are there better ways to work with these art expected bytes? Am I not seeing something essential?