C++ 风格从 unsigned char * 转换为 const char *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/658913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ style cast from unsigned char * to const char *
提问by Hymanhab
I have:
我有:
unsigned char *foo();
std::string str;
str.append(static_cast<const char*>(foo()));
The error: invalid static_cast from type ‘unsigned char*' to type ‘const char*'
错误: invalid static_cast from type ‘unsigned char*' to type ‘const char*'
What's the correct way to cast here in C++ style?
以 C++ 风格在此处进行转换的正确方法是什么?
采纳答案by Ruben Bartelink
reinterpret_cast
reinterpret_cast
回答by Brian R. Bondy
char *
and const unsigned char *
are considered unrelated types. So you want to use reinterpret_cast
.
char *
并且const unsigned char *
被认为是不相关的类型。所以你想使用reinterpret_cast
.
But if you were going from const unsigned char*
to a non const
type you'd need to use const_cast
first. reinterpret_cast
cannot cast away a const
or volatile
qualification.
但是,如果您要从const unsigned char*
非const
类型转换,则需要先使用const_cast
。reinterpret_cast
不能抛弃 aconst
或volatile
资格。
回答by JaredPar
Try reinterpret_cast
尝试 reinterpret_cast
unsigned char *foo();
std::string str;
str.append(reinterpret_cast<const char*>(foo()));
回答by AJG
unsigned char* is basically a byte array and should be used to represent raw data rather than a string generally. A unicode string would be represented as wchar_t*
unsigned char* 基本上是一个字节数组,通常应该用于表示原始数据而不是字符串。Unicode 字符串将表示为 wchar_t*
According to the C++ standard a reinterpret_cast between unsigned char* and char* is safe as they are the same size and have the same construction and constraints. I try to avoid reintrepret_cast even more so than const_cast in general.
根据 C++ 标准, unsigned char* 和 char* 之间的 reinterpret_cast 是安全的,因为它们具有相同的大小并具有相同的构造和约束。我尽量避免 reintrepret_cast 比一般的 const_cast 更是如此。
If static cast fails with what you are doing you may want to reconsider your design because frankly if you are using C++ you may want to take advantage of what the "plus plus" part offers and use string classes and STL (aka std::basic_string might work better for you)
如果静态转换因您正在执行的操作失败,您可能需要重新考虑您的设计,因为坦率地说,如果您使用 C++,您可能希望利用“加号”部分提供的功能并使用字符串类和 STL(又名 std::basic_string可能更适合你)
回答by Timo Geusch
You would need to use a reinterpret_cast<>
as the two types you are casting between are unrelated to each other.
您需要使用 a ,reinterpret_cast<>
因为您在两者之间进行转换的两种类型彼此无关。
回答by Victor Sergienko
Too many comments to make to different answers, so I'll leave another answer here.
太多的评论无法对不同的答案进行评论,所以我会在这里留下另一个答案。
You can and should use reinterpret_cast<>
, in your case
你可以而且应该使用reinterpret_cast<>
,在你的情况下
str.append(reinterpret_cast<const char*>(foo()));
because, while these two are different types, the 2014 standard, chapter 3.9.1 Fundamental types [basic.fundamental]
says there isa relationship between them:
因为,虽然这两个是不同的类型,但 2014 标准,章节3.9.1 Fundamental types [basic.fundamental]
说它们之间存在关系:
Plain
char
,signed char
andunsigned char
are three distinct types, collectively called narrow character types. Achar
, asigned char
, and anunsigned char
occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation.
平原
char
,signed char
并unsigned char
有三种不同的类型,统称为窄字符类型。Achar
、 asigned char
和 anunsigned char
占用相同的存储量并具有相同的对齐要求(3.11);也就是说,它们具有相同的对象表示。
(selection is mine)
(选择是我的)
Here's an available link: https://en.cppreference.com/w/cpp/language/types#Character_types
这是一个可用的链接:https: //en.cppreference.com/w/cpp/language/types#Character_types
Using wchar_t
for Unicode/multibyte strings is outdated: Should I use wchar_t when using UTF-8?
使用wchar_t
:对Unicode /多字节字符串已经过时使用UTF-8的时候我应该使用wchar_t的?
回答by joi
Hope it help. :)
希望有帮助。:)
const unsigned attribName = getname();
const unsigned attribVal = getvalue();
const char *attrName=NULL, *attrVal=NULL;
attrName = (const char*) attribName;
attrVal = (const char*) attribVal;