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

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);

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

Trying to Assign Pointer out of a struct to a two-dimensional array

have an assignment where I should use the following struct:
typedef struct natset{
unsigned long min;
unsigned int size;
unsigned char* bits;
}natset_t;
Now I have to assign that pointer to the following two-dimensional array of unsigned chars:
unsigned char bArrayRoll[7][byteNum];
Which I have tried doing like so:
unsigned long min = 20;
unsigned int size = 20;
unsigned char bArrayRoll[7][byteNum];
natset_t newNatset;
newNatset.min = min;
newNatset.size = size;
newNatset.bits = &bArrayRoll;
Variations of that newNatset.bits which I have tried include:
newNatset.bits = bArrayRoll;
newNatset.bits* = &bArrayRoll;
However, the compiler either returns "Assignment from incompatible pointer type" or "Initialization from incompatible pointer type".
How can I assign the bits pointer to this array correctly?
newNatset.bits* = &bArrayRoll;
You are trying to multiply newNatset.bits with the address of bArrayRoll, this is undefined behavior.
newNatset.bits = bArrayRoll;
This one here is trying to assign unsigned char (*)[byteNum] to unsigned char* which is again something about which compiler complained. Types are incompatible clearly.
Correct one would be (correct one as per the compiler)
newNatset.bits = bArrayRoll[0];
wouldn't it have to be: newNatset.bits = bArrayRoll[0][0]?
This is once again a type incompatible assignment. You are assigning unsigned char to unsigned char*. You can do this though,
newNatset.bits = &bArrayRoll[0][0];
Trying to Assign Pointer out of a struct to a two-dimensional array? (The thing you wanted).
Then you need to do this,
unsigned char (*p)[byteNum] = bArrayRoll;
but in the structure you have declared bits to be of type unsigned char* not of type unsigned char (*)[byteNum].
Well if you didn't understand what happened when we assigned in the last case and how did the types match - then here is the key idea.
The 2d array decays into the pointer to the first element - which is a single dimensional array of byteNum unsigned characters. Now we are assigning it to p which is again
unsigned char (*)[byteNum]
^ ^
1 2
\-----------/
3
Pointer to (1) an array of bytenum (2) unsigned characters (3).

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'.

How do you convert an unsigned int[16] of hexidecimal to an unsigned char array without losing any information?

I have a unsigned int[16] array that when printed out looks like this:
4418703544ED3F688AC208F53343AA59
The code used to print it out is this:
for (i = 0; i < 16; i++)
printf("%X", CipherBlock[i] / 16), printf("%X",CipherBlock[i] % 16);
printf("\n");
I need to pass this unsigned int array "CipherBlock" into a decrypt() method that only takes unsigned char *. How do correctly memcpy everything from the "CipherBlock" array into an unsigned char array without losing information?
My understanding is an unsigned int is 4 bytes and unsigned char 1 byte. Since "CipherBlock" is 16 unsigned integers, the total size in bytes = 16 * 4 = 64 bytes. Does this mean my unsigned char[] array needs to be 64 in length?
If so, would the following work?
unsigned char arr[64] = { '\0' };
memcpy(arr,CipherBlock,64);
This does not seem to work. For some reason it only copies the the first byte of "CipherBlock" into "arr". The rest of "arr" is '\0' thereafter.
An int is at least 16 bits, same as a short in that regard.
It looks like every unsigned int has values 0-255 or 00-FF in your case, which is a safe range for an unsigned char. However, the proper way to convert one to the other is a cast:
for (int i=0; i<16; ++i) arr[i] = (unsigned char) CipherBlock[i];
But you have not specified what kind of data decrypt() expects. From the signature, I suspect integral data (strings are usually char* or const char*) but it's hard to be sure without a context.
Note that you could also do printf("%02X", CipherBlock[i]); for printing.
Why don't you just cast the CipherBlock pointer to unsigned char * and pass that?
decrypt((unsigned char *)CipherBlock);
You need to repack the numbers so you can not use memcpy or cast it directly. Aib has it correct.
unsigned char array[16];
for(int i = 0; i < 16; i++) {
array[i] = CipherBlock[i];
}

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