Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Array.Value differs from created array in c++

Tags:

c#

integer

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?

like image 891
my2cents Avatar asked Nov 04 '13 18:11

my2cents


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

If you don't like if in your code, you can try LINQ:

public static byte[] GetCBuffer(this byte[] data, int len)
{
    byte[] tmp = Enumerable.Repeat((byte)204, len).ToArray();
    for(int i = 0; i < data.Length; i++)
    {
      tmp[i] = data[i];
    }

    return ret;
}

But conceptually, it's the same, just looks (arguably) a bit nicer.

like image 119
undefined Avatar answered Oct 15 '22 01:10

undefined