How to convert array of unsigned char to string - winforms

How do I convert the below unsigned char to a string:
arry<unsigned char>^arrayOne = {0,1,2,3};
textbox1->Text = BitConverter::ToString(arrayOne);
I have tried the following
unsigned char data[128];
textbox1->Text = BitConverter::ToString(data);
Which is failing and producing the following error message:
System::BitConverter::ToString
none of the 4 overloads could convert all the argument types

Related

C "Error: Invalid initializer"

the following short code snippet results in a invalid initializer error, and as a beginner in C, I do not understand why.
unsigned char MES[] = { 0x00, .... };
unsigned char *in[] = &MES;
Is this not the correct way to do it?
&MES is a pointer to an array of unsigned char.
in is an array of pointers to unsigned char.
Try instead :
unsigned char (*in)[] = &MES;
which makes in also a pointer to an array of unsigned char.
I think that what you are trying to achieve is the following:
unsigned char MES[] = { 0x00 };
unsigned char *in = MES;
qualifying in as an array (whose size is unknown) as follow
unsigned char (*in2)[] = &MES;
is not going to add valuable information to it aside that it has a finite size (which is true for any data) and if you print in and in2
printf ("%lx\n", (long unsigned int) in);
printf ("%lx\n", (long unsigned int) in2);
the value shall be the same.
Don't confuse the position of the data with the position of its reference.
Using &MES is like trying to read the position in memory where the position of the array is written. But this does not exist.
Consider the counterexample:
void *reference_to_memoryarea = malloc(3);
void **reference_to_the_reference = &reference_to_memoryarea;
Here the position exists and has a position in memory, where it is stored. And you can write into *reference_to_the_reference and generate a good leak

Convert char* to unsigned char arduino

I have a char array
char data[] = "0123456789012345"; //16 chars == 16 bytes
I want to send this data thru BLE shield using this method
void ble_write(unsigned char data);
This is my code in Arduino
ble_write(data);
I am getting this error invalid conversion from 'char*' to 'unsigned char' [-fpermissive]
What can I do to convert char* to unsigned char?
In your case you are sending a pointer to a table of chars ( char *) but the ble_write function takes as argument only a unsigned char ! so you need to first of all declare the data table as unsigned char, and then use a loop to send each element of the table ( data[i]) until you reach the null character '\0'.

C Character from literal Splint warns of incompatible types

I have a program that uses unsigned chars to represent integers with a small range. I find myself needing to clear them to 0 in several different parts of the program, I have also recently started using splint and apparently:
unsigned char c = 0;
gives the warning:
Variable c initialized to type int, expects unsigned char: 0
Types are incompatible.
As there is no suffix for a literal char, How is it best to resolve this? I think I have a few options:
1: Ignore the warning.
2: Cast every time:
unsigned char c = (unsigned char)0;
3: Make a var to cut down the length of the code:
unsigned char uc_0 = (unsigned char)0;
unsigned char c = uc_0;
4: A function:
static inline unsigned char uchar(int in)
{
return (unsigned char)in;
}
unsigned char c = uchar(0);
splint has an option +charint that will treat char int as interchangeable.
You can ignore the warnings and use
unsigned char = 0;
In many cases when there is integer operation in order to save memory instead of using int which obviously consumes extra memory than char people do make use of unsigned char.
unsigned char i = 10;
unsigned char j = 1;
unsigned char k = i +j;
printf("%d",k);

How to convert unsigned char * to byte array in cli/c++

I know how to convert byte array to unsigned char *. How can I do the reverse?
I don't know the length of the unsigned char *.
I am trying to do thinks like--
unsigned char *ptr_data = "The size of this value is unknown";
System::IntPtr ptr = (System::IntPtr)ptr_data;
System::String^ str_data = Marshal::PtrToStringAuto(ptr);
array<Byte>^ bytes = System::Text::Encoding::->GetBytes(str_data);

How to convert signed to unsigned char in OpenCL?

I have a signed char in OpenCL that I need to convert to a unsigned char.
The OpenCL standard defines explicit conversion functions for all the built-in scalar and vector types. So you can do something like this:
char signed_val = 10;
uchar unsigned_val = convert_uchar(signed_val);
C-like casting should work. The only difference is that you use cl_ types. But these are equivalent to the C types.
For example, to convert signed to unsigned char:
cl_char c = 0xF;
cl_uchar uc = c;
To convert a pointer to signed char to pointer to unsigned char:
cl_char* cArr = // Points to char array
cl_uchar* ucArr = ( cl_uchar* ) cArr; // Access char array as uchar array

Resources