简体   繁体   English

嵌入式系统中的c char字符串和stdint.h uint8_t兼容性

[英]c char string and stdint.h uint8_t compatability in embedded system

I need to convert const char[] to uint8_t in C++. 我需要在C ++中将const char []转换为uint8_t。 What is the best method 最好的方法是什么

a) 一种)

const uint8_t* a = (const uint8_t*)"string";  // I hate this cast, but it works

b) b)

const uint8_t* a = reinterpret_cast<const uint8_t*>("string");  // is this OK, is it safe?

Why will this not work? 为什么这不起作用?

const uint8_t* a = static_cast<const uint8_t*>("string");  // will not compile

The second solution is the "Most Correct" way to do it. 第二种解决方案是“最正确”的方法。 The reinterpret_cast will be handled entirely by the compiler and simply point at the resulting bits with a different type. reinterpret_cast将完全由编译器处理,并仅指向具有不同类型的结果位。 The first solution is old-style and generally should not be used in modern C++. 第一个解决方案是旧式的,通常不应在现代C ++中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM