如何在 C++ 中将 0x70、0x61、0x73 ... 等转换为 Pas ... 等?
我正在使用 MSVC++ 2010 Express,我很想知道如何转换
BYTE Key[] = {0x50,0x61,0x73,0x73,0x77,0x6F,0x72,0x64};
为“密码”,但我在执行此操作时遇到了很多麻烦。 :( 我将利用这些知识来获取诸如...
BYTE Key[] { 0xC2, 0xB3, 0x72, 0x3C, 0xC6, 0xAE, 0xD9, 0xB5, 0x34, 0x3C, 0x53, 0xEE, 0x2F, 0x43, 0x67, 0xCE };
和其他各种变量并相应地转换它们。
我希望最终将“密码”存储在字符中。
I am using MSVC++ 2010 Express, and I would love to know how to convert
BYTE Key[] = {0x50,0x61,0x73,0x73,0x77,0x6F,0x72,0x64};
to "Password" I am having a lot of trouble doing this. :( I will use this knowledge to take things such as...
BYTE Key[] { 0xC2, 0xB3, 0x72, 0x3C, 0xC6, 0xAE, 0xD9, 0xB5, 0x34, 0x3C, 0x53, 0xEE, 0x2F, 0x43, 0x67, 0xCE };
And other various variables and convert them accordingly.
Id like to end up with "Password" stored in a char.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
键是一个字节数组。例如,如果要将其存储在
string
中,则应使用其范围构造函数构造该字符串,即:或者如果可以使用 C++11 进行编译:
要获取
char*
我会采用 C 方式并使用strndup
:但是,如果您使用 C++,我强烈建议您使用
string
而不是char*
并且仅转换为char const*
当绝对必要时(例如,当调用 C API 时)。请参阅此处了解首选std::string
的充分理由。Key is an array of bytes. If you want to store it in a
string
, for example, you should construct the string using its range constructor, that is:Or if you can compile using C++11:
To get a
char*
I'd go the C way and usestrndup
:However, if you're using C++ I strongly suggest you use
string
instead ofchar*
and only convert tochar const*
when absolutely necessary (e.g. when calling a C API). See here for good reasons to preferstd::string
.您所缺少的只是一个空终止符,因此执行此操作后:
Key_str 将可用作常规 char * 样式字符串。
All you are lacking is a null terminator, so after doing this:
Key_str will be usable as a regular char * style string.