如何在c ++中将字符串转换为字节数组?

时间:2022-09-01 12:00:20

How can I convert this C# code to C++?

如何将此C#代码转换为C ++?

string map = "maps\guardian";
byte[] mapName = Encoding.ASCII.GetBytes(map);

2 个解决方案

#1


Use std::string like this

像这样使用std :: string

std::string myString("Hello World!");
const char* myConstArray = myString.c_str();
char myNonConstArray[100];
strcpy(myNonConstArray, myString.c_str());

both const and non-const versions there.

那里有const和非const版本。

#2


Would not this suffice?:

这不够吗?:

std::string map = "maps\guardian";
char* asciimap = map.c_str();

In C++ there is no byte type. You only have char.

在C ++中没有字节类型。你只有char。

#1


Use std::string like this

像这样使用std :: string

std::string myString("Hello World!");
const char* myConstArray = myString.c_str();
char myNonConstArray[100];
strcpy(myNonConstArray, myString.c_str());

both const and non-const versions there.

那里有const和非const版本。

#2


Would not this suffice?:

这不够吗?:

std::string map = "maps\guardian";
char* asciimap = map.c_str();

In C++ there is no byte type. You only have char.

在C ++中没有字节类型。你只有char。