How to Cast Const Uint8_T* to Char*

cast unsigned char * (uint8_t *) to const char *

It is safe. The error has to do with mixing unsigned integers of 8 bits with characters, that are signed if you use just char.

I see, however, that the function accepts uint8_t and does character arithmetic, so it should accepts chars (or const chars, for the matter). Note that a character constant 'c' is of type char, and you're mixing signed and unsigned in the expressions inside ihex_decode, so you have to be careful to avoid overflows or negative numbers treated as big positive numbers.

A last style note. As in is not modified, it should read const uint8_t* in (or const char* in, as of above) in the parameters. Another style error (that may lead to very bad errors) is that you accept len as size_t, but declare the i loop variable as uint8_t. What if the string is more than 255 bytes in length?

Change uint8_t* to char*?

std::uint8_t ιs equal to unsigned char.

This is different from plain char or signed char, but all of them are 8 bit, therefore casting would techically work.

It's common that many functions that would otherwise need a "buffer" have a char* in their definition instead of the proper unsigned char*. Therefore, casting would most probably be harmless.

In the case that the function actually wants characters but not a buffer, then you have a problem because the types are different, and whether you will have an issue or not is undefined.

Convert from uint8_t * to char * in C

How can I convert the uint8_t * stackHolder into a char *?

By casting:

print((char*) stackHolder);

how to convert const Uint8* to Uint8* [duplicate]

You can use const_cast for this, e.g:

Uint8* foo = const_cast<Uint8*>(bar);

However: Are you sure that you are really doing the correct thing? Please verify that you don't modify the underlying value after removing constness as this is undefined behaviour.

[ Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_­cast that casts away a const-qualifier may produce undefined behavior ([dcl.type.cv]). — end note ]
[expr.const.cast.6]

Converting uint8_t* to char* for use with QIODevice

There is no native type to represent a "byte" in C++, only char that is guaranteed to hold exactly one byte. There are different opinions whether a byte type to represent raw binary data should be signed or not, so some use unsigned char (uint8_t) and others use plain char. In the end, it does not really matter, since you usually don't perform arithmetic operations on binary data, but just read and interpret it.

Therefore, you can just use a type cast to convert between different binary data representations. Since this is C++, you should use reinterpret_cast (in favor of C-style casts):

char* dst = reinterpret_cast<char*>(/* your uint8_t* expression */);

Whether to use reinterpret_cast or C-style casts is obviously disputed. Bjarne Stroustrup, the creator of C++, would certainly advocate for reinterpret_cast, but others don't like it, and that's OK.

C++ Uint8 datatype conversion to const char* datatype

To convert the pointer types, you just need to cast the pointer from one type to the other.

For example,

uint8 *uint8_pointer = ?;

// C style cast
const char *char_pointer = (char*)uint8_pointer;

// newer C++ style cast syntax
const char *char_pointer2 = reinterpret_cast<char*>(uint8_pointer);

You can also do it the other way round:

char *char_pointer = ?;
uint8 *uint8_pointer = reinterpret_cast<uint8*>(char_pointer);

For your function, you can use:

functionX(uint8 *src, uint16 nSrcLen){
write(reinterpret_cast<char*>(src));
}
void write(const char* msg);


Related Topics



Leave a reply



Submit