将未签名的char *转换为std :: string [英] convert unsigned char* to std::string

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

问题描述

我的打字能力有点差.我在 xmlChar * 中有一个字符串(它是未签名的char *),我想将此未签名的char转换为 std :: string 类型.

I am little poor in typecasting. 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";

我尽了最大的努力来进行类型转换,但是我找不到转换它的方法.

I tried my best to typecast , but I couldn't find a way to convert it.

推荐答案

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

reinterpret_cast< char *>(名称)以不安全的方式从 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 C-style (not recommended):

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

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

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