如何在c ++中将字符串转换为unsigned char数组 [英] How to convert a string to unsigned char array in c++

查看:1263
本文介绍了如何在c ++中将字符串转换为unsigned char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string temp;
temp = line.substr(0, pos);



我需要将字符串temp转换为unsigned char数组。有人可以告诉我这是怎么做的吗?我尝试了下面链接中提到的方法,但是没有工作。

I need to convert the string temp to an unsigned char array. Could someone please tell me how this can be done? I tried the methods mentioned in the below link but they didn't work.

How to convert a string literal to Unsigned char in visual c++

unsigned char* val=new[temp.size() + 1](); //Error: Expected a type.
copy(temp.begin(), temp.end(), val);


推荐答案

char缓冲区,你不应该这样做,但这是堆栈溢出,我们必须回答。

It is ugly to put a string in a raw unsigned char buffer and you should not do that, but this is stack overflow and we have to answer.

unsigned char *val=new unsigned char[temp.length()+1];
strcpy((char *)val,temp.c_str());

这篇关于如何在c ++中将字符串转换为unsigned char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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