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'.
Related
This is my function:
void eeprom_read_page(unsigned int address, unsigned char lengh, unsigned char *data[40])
{
//unsigned char data[lengh] , i;
unsigned char i;
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS_W);
i2c_write(address>>8); //high byte address
i2c_write(address*0xff); //low byte address
i2c_start();
i2c_write(EEPROM_BUS_ADDRESS_R);
for(i=0 ; i<(lengh-1) ; i++)
{
*data[i+4]=i2c_read(1);
}
*data[lengh+3]=i2c_read(0);
i2c_stop();
}
And this is how I use it somewhere in my code:
eeprom_read_page( ( (rx_buffer1[1]*256)+rx_buffer1[2] ) , rx_buffer1[3] , &tx_buffer1 );
And this is my array define:
#define RX_BUFFER_SIZE1 40
char rx_buffer1[RX_BUFFER_SIZE1],tx_buffer1[RX_BUFFER_SIZE1];
but tx_buffer1 doesn't get values I give in data[]. I want to change tx_buffer1 but don't use return. Any help?
The array declared the following way
#define RX_BUFFER_SIZE1 40
char rx_buffer1[RX_BUFFER_SIZE1],tx_buffer1[RX_BUFFER_SIZE1];
used in the expression
&tx_buffer1
makes the expression type char ( * )[RX_BUFFER_SIZE1].
At the same time the corresponding function parameter
unsigned char *data[40]
has the type unsigned char ** because the compiler implicitly adjusts a parameter having an array type to pointer to an object of the element type of the array.
And moreover the function parameter uses the specifier unsigned char while the array declared with the specifier char.
So the function call is invalid. There is no implicit conversion between the pointer types.
There is no any sense to pass the array to a function by reference because in any case arrays are non-modifiable lvalues.
If you want to pass the array by reference to know its size in the function then the function parameter shall be declared like
char ( *data )[40]
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
what is the meaning of (unsigned char*)&ch in the following function call ?
HAL_UART_Transmit(&UartHandle, (unsigned char *)&ch, 1, 0xFFFF);
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, unsigned char *pData, int Size, long Timeout)
{
/*...........Function Body ........*/
}
&ch is an address of some variable, type is unknown given this code. (unsigned int*)&ch is simply casting the result of this expression to a pointer to int.
It takes the address of the variable ch using the address-of operator &. The resulting address is then converted (cast) to the type unsigned int *, i.e. a pointer to an unsigned int. This only makes sense if
The type of ch was not unsigned int to begin with
The size of ch is at least as large as the size of unsigned int
The called function accepts an unsigned int * argument
Since we can see that the type of the argument is in fact uint8_t *, this is very likely a bug. The cast should either be removed (if ch is of type uint8_t already, which it should be) or changed to uint8_t *. Also, the function's parameter should be const, a transmit function shouln't change its argument.
Let's say you have a function that takes an argument of type unsigned int *.
You have an object of type int and you want to pass a pointer to this object the function.
int ch = 42;
Then &ch get you an int * to the int object. You cannot pass &ch directly to your function as &ch is of type int * but the function wants a unsigned int *, so you convert it to unsigned int * with a cast:
(unsigned int *) &ch
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);
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);