如何将视觉c ++中的字符串字面量转换为unsigned char数组 [英] How to convert a string literal to unsigned char array in visual c++

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

问题描述

如何将字符串转换为unsigned char在c ++ ...

How to convert a string to Unsigned char in c++...

我有,

unsigned char m_Test[8];

我想分配一个字符串Hello world

I want to assign a string "Hello world" to m_Test.

如何处理?

推荐答案

首先,数组必须至少足够容纳字符串:

Firstly, the array has to be at least big enough to hold the string:

 unsigned char m_Test[20];

那么你使用strcpy。您需要转换第一个参数以避免警告:

then you use strcpy. You need to cast the first parameter to avoid a warning:

 strcpy( (char*) m_Test, "Hello World" );

或者如果你想成为一个C ++纯粹主义者:

Or if you want to be a C++ purist:

 strcpy( static_cast <char*>( m_Test ), "Hello World" );

如果您要初始化字符串而不是分配字符串,您还可以说:

If you want to initialise the string rather than assign it, you could also say:

 unsigned char m_Test[20] = "Hello World";

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

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