Why do I need typecast hex?
-
This is silly , but most I/O devices spec sheets data are written as "hex". If I want to pass this char *TXBuffer = (char*) 0x04; to a function it has to be type casted. Like so
char \*TXBuffer = (char\*) 0x04; int \*TEST = (int\*)0x04;
If not I'll get "invalid conversion". Cheers Vaclav
-
This is silly , but most I/O devices spec sheets data are written as "hex". If I want to pass this char *TXBuffer = (char*) 0x04; to a function it has to be type casted. Like so
char \*TXBuffer = (char\*) 0x04; int \*TEST = (int\*)0x04;
If not I'll get "invalid conversion". Cheers Vaclav
Vaclav_ wrote:
char *TXBuffer = (char*) 0x04;
No you don't need to do that, and you shouldn't because it is wrong. You are trying to pass the value 0x04 as the buffer address, which will cause an access violation. If you need a buffer containing the hex value, and you want to send that buffer's address to a function then you need to do one or other of the following:
unsigned char TXBuffer = 0x04; // a single character
function(&TXBuffer); // use the addressOf operator to pass the address of the character to the function// or
unsigned char TXBuffer[] = { 0x04, 0 }; // multiple characters
function(TXBuffer); // TXBuffer is an array, so its name will be translated to its address by the compiler.// the receiving function should be coded as
xxx function(unsigned char* buffer)
{
// xxx is the return type
// the function must know how many characters will be sent in the buffer
// either a number that is always the same, or the length passed as a second parameter.
// to access items from the buffer you use:
unsigned char value = *buffer; // get the first value
buffer++; // increment the pointer to the next element (if more than 1)
value = *buffer; // get the next value
} -
Vaclav_ wrote:
char *TXBuffer = (char*) 0x04;
No you don't need to do that, and you shouldn't because it is wrong. You are trying to pass the value 0x04 as the buffer address, which will cause an access violation. If you need a buffer containing the hex value, and you want to send that buffer's address to a function then you need to do one or other of the following:
unsigned char TXBuffer = 0x04; // a single character
function(&TXBuffer); // use the addressOf operator to pass the address of the character to the function// or
unsigned char TXBuffer[] = { 0x04, 0 }; // multiple characters
function(TXBuffer); // TXBuffer is an array, so its name will be translated to its address by the compiler.// the receiving function should be coded as
xxx function(unsigned char* buffer)
{
// xxx is the return type
// the function must know how many characters will be sent in the buffer
// either a number that is always the same, or the length passed as a second parameter.
// to access items from the buffer you use:
unsigned char value = *buffer; // get the first value
buffer++; // increment the pointer to the next element (if more than 1)
value = *buffer; // get the next value
}