如何在visual c ++中将字符串文字转换为无符号字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2206050/
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
How to convert a string literal to unsigned char array in visual c++
提问by Anuya
How to convert a string to Unsigned char in c++...
如何在 C++ 中将字符串转换为无符号字符...
I have,
我有,
unsigned char m_Test[8];
I want to assign a string "Hello world"
to m_Test
.
我想将一个字符串分配"Hello world"
给m_Test
.
how to do it?
怎么做?
回答by
Firstly, the array has to be at least big enough to hold the string:
首先,数组必须至少大到足以容纳字符串:
unsigned char m_Test[20];
then you use strcpy. You need to cast the first parameter to avoid a warning:
然后你使用strcpy。您需要强制转换第一个参数以避免警告:
strcpy( (char*) m_Test, "Hello World" );
Or if you want to be a C++ purist:
或者,如果您想成为 C++ 纯粹主义者:
strcpy( static_cast <char*>( m_Test ), "Hello World" );
If you want to initialise the string rather than assign it, you could also say:
如果你想初始化字符串而不是分配它,你也可以说:
unsigned char m_Test[20] = "Hello World";
回答by Buhake Sindi
回答by Steve Jessop
For all practical purposes, the strcpy answers are correct, with the note that 8 isn't big enough for your string.
出于所有实际目的,strcpy 的答案是正确的,请注意 8 对您的字符串来说不够大。
If you want to be really pedantic, you might need something like this:
如果你想真正学究,你可能需要这样的东西:
#include <algorithm>
int main() {
const char greeting[] = "Hello world";
unsigned char m_Test[sizeof(greeting)];
std::copy(greeting, greeting + sizeof(greeting), m_Test);
}
The reason is that std::copy will convertthe characters in the original string to unsigned char
. strcpy will result in the characters in the original string being reinterpretedas unsigned char
. You don't say which one you want.
原因是 std::copy 会将原始字符串中的字符转换为unsigned char
. strcpy 将导致原始字符串中的字符被重新解释为unsigned char
. 你没有说你想要哪一个。
The standard permits there to be a difference between the two, although it's very rare: you'd need char
to be signed, in an implementation with a 1s' complement or sign-magnitude representation. You can pretty much ignore the possibility, but IMO it's worth knowing about, because it explains the funny warnings that good compilers give you when you mix up pointers to char
and unsigned char
.
该标准允许两者之间存在差异,尽管这种差异非常罕见:char
在使用 1 的补码或符号大小表示的实现中,您需要签名。您几乎可以忽略这种可能性,但 IMO 值得了解,因为它解释了当您混淆指向char
和 的指针时优秀编译器给您的有趣警告unsigned char
。
回答by Naveen
You can use c_str()
function of std::string
to get the char*
out of string. This method basically returns a pointer to c-style string. After this you can use the normal string copying functions such as strcpy
or strncpy
to copy the value in to test
.
您可以使用c_str()
of 函数std::string
来获取char*
字符串。此方法基本上返回一个指向 c 样式字符串的指针。在此之后,您可以使用普通的字符串复制功能,例如strcpy
或strncpy
将值复制到test
.
回答by Paolo Tedesco
回答by AndiDog
You can use strcpy
:
您可以使用strcpy
:
unsigned char m_Test[8];
strcpy((char*)m_Test, "Hello world");
Note that "Hello world" is too long for 8 bytes, so you will probably get a segfault.
请注意,“Hello world”对于 8 个字节来说太长了,因此您可能会遇到段错误。
回答by anthares
you can use strcpy function But have in mind that m_Test is only 8 size and there will be an overflow. Strcpy won't check that and you will get an exception
您可以使用 strcpy 函数但请记住,m_Test 只有 8 个大小,并且会出现溢出。Strcpy 不会检查,你会得到一个例外
char * strcpy ( char * destination, const char * source );
回答by Dracul
string uInput;
cout << "Enter message" << endl;
getline(cin, uInput);
I'm adding 1 here because in c strings we have '\0' at the end
我在这里添加 1 因为在 c 字符串中我们在末尾有 '\0'
unsigned char dataS[uInput.size()+1];
strcpy(reinterpret_cast<char*>(dataS), uInput.c_str());
I think this example will help others more in the future.
我认为这个例子将来会对其他人有更多帮助。