将unsigned char *转换为String [英] convert unsigned char* to String

查看:787
本文介绍了将unsigned char *转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在铸造类型上有点差。我在 xmlChar * (这是unsigned char *)中有一个字符串,我想将这个unsigned char转换为 std :: string type。

I am little poor in type casting. I have a string in xmlChar* (which is unsigned char*), I want to convert this unsigned char to a std::string type.

xmlChar* name = "Some data";

我尽力键入cast,但我不能转换它。 p>

I tried my best to type cast , but I couldn't a way to convert it.

推荐答案

std::string sName(reinterpret_cast<char*>(name));

reinterpret_cast< char *>(name)以不安全的方式从 unsigned char * 转换为 char *,但这是应该在这里使用的那个。然后调用 std :: string 的普通构造函数。

reinterpret_cast<char*>(name) casts from unsigned char* to char* in an unsafe way but that's the one which should be used here. Then you call the ordinary constructor of std::string.

您也可以使用C风格,但不推荐的方式:

You could also do it with a C-style, not recommended way:

std::string sName((char*) name);

这篇关于将unsigned char *转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆