0

I want to migrate a c++ code to c# , in my c++ code i am using a public key for testing using a hard coded value like this :

static unsigned char PubKeyModulus[] = {
"\xCA\x68\x77\....."
"\x17\x55\x79\..."
"\xF5\xD2\...."
"\x2B\xE4\..."
"\x7F\xC5\..."
"\xEA\x19\..."
"\x83\x67\..."
"\x68\xEF\..."
"\x57\x72\..."
"\x0F\xE5\..."
"\xD0\xBD\..."
"\x21\x21\..."
"\x11\x63\..."
"\x05\xFB\..."
"\x44\x7A\..."
"\xD3\x19\..."
};

how can i use this value in C# code.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
leonidas79
  • 437
  • 2
  • 7
  • 19
  • possible duplicate of [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) – Matt Coubrough Dec 07 '14 at 21:44
  • possible duplicate of [Multiline String Literal in C#](http://stackoverflow.com/questions/1100260/multiline-string-literal-in-c-sharp) – Alex Shesterov Dec 07 '14 at 21:46

1 Answers1

1

Note that while char is a single-byte type in C++, it's a two-byte type in C#. You want byte in C#.

As for the specific question, wouldn't the following work?

static readonly byte PubKeyModulus[] = {
0xCA, 0x68, 0x77, ...
0x17, 0x55, 0x79, ...
0xF5, 0xD2, ...
0x2B, 0xE4, ...
0x7F, 0xC5, ...
0xEA, 0x19, ...
0x83, 0x67, ...
0x68, 0xEF, ...
0x57, 0x72, ...
0x0F, 0xE5, ...
0xD0, 0xBD, ...
0x21, 0x21, ...
0x11, 0x63, ...
0x05, 0xFB, ...
0x44, 0x7A, ...
0xD3, 0x19, ...
};

(Where ... is replaced by more byte values, of course)

If not, please explain why not.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • this is the mothode's signature in c++ `extern __declspec( dllexport ) int CardAuthenticate( IN const unsigned char* PubAuthKey, IN unsigned int PubAuthKeySize ); ` @PeterSmith i tried your solution but it's not working. this is my calling for the method in my C# code `[DllImport("MyDLL.dll")] public static extern Int32 CardAuthenticate(byte[] Data, ref int CleSize);` – leonidas79 Dec 08 '14 at 00:38
  • 1
    Declaring a `byte[]` data structure is different from passing it to some unmanaged code via interop. If the above addresses your question about how to do the declaration, but you still have a question about how to pass it, you should accept this and post a new question showing what you've tried, explaining what it did and how that's different from what you wanted. – Peter Duniho Dec 08 '14 at 00:49