Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Having a C# string contains hexadecimal value and required to pass it to C++/CLI method that accept unsigned char[]. How can I pass that information.

I tried to convert the string into C# byte [] and then pass it to c++ but only first element of array is passed.

C#
C#
byte[] Byte = StringToByteArray(Data);
      unsafe
      {
          byte* _passData;
          fixed (byte* p = Byte)
          {
              _passData = (byte*)p;
          }
          SCARem.set_briefprivdata(_passData);
      }


C++
C#
void detdata(unsigned char _data[])



Please share if passing an string to c++ and then converting it into unsigned char[] in c++ is more convenient.

End result required is C++ unsigned char[]. Thanks
Posted
Comments
sanisafa 22-Jul-15 23:40pm    
Is there any way that I can loop through string to create unsigned char []

I prefer Stringbuilder for passing strings to C++. Take a look at my article and its code.
 
Share this answer
 
Comments
sanisafa 22-Jul-15 23:17pm    
Thanks for your reply.

I am accessing c++/clr methods directly from my C# code (no import dll). In C++ I have to make a call to an SDK and that SDK methods requires Unsigned Char array. When I pass the byte[] to C++ it passes only first index of the array.

STRING is "210200200000a0980030081000070002380400008007000001010200"

public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}

You can add a C++/CLI class with a function that takes a .NET String and call the detdata function. In that method, you can use marshal_as to convert the string. Something like:


C++
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace msclr::interop;

void detdata(unsigned char _data[]);

public ref class Class1
{
public:
	Class1(void) {}

	void f1(System::String^ str)
	{					
		std::string data = marshal_as<std::string>(str);
		detdata((unsigned char*)data.c_str());
	}
};

Then, you can use the f1 function in your C# code. Something like:


C#
Class1 c1 = new Class1();
c1.f1("Text 1");
c1.f1("Text 2");
c1.f1("Text 3");
 
Share this answer
 
v2
Comments
sanisafa 22-Jul-15 23:17pm    
Receiving error "unresolved token for detdata".
Shmuel Zang 23-Jul-15 0:25am    
In the example above I provided only a forward declaration for the detdata function. You can define it with your implementation:

void detdata(unsigned char _data[])
{
// Your implementation.
}
sanisafa 23-Jul-15 18:58pm    
Thanks, but the same situation I am getting only first character of string.
Thanks guys its done. Using below mentioned code.

C#
void Convert(String^ Data)
        {
            unsigned char array[28];

            array<String^>^ strArray = gcnew array<String^>(28);
            strArray = Data->Split(',');

            int val = 0, index = 0;

            for each (String^ value in strArray)
            {
                val = int::Parse(value);
                array[index] = val;
                index++;
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900