如何将CString转换为UCHAR阵列? [英] How to convert CString to UCHAR array?

查看:146
本文介绍了如何将CString转换为UCHAR阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cstring对象str =5043,现在我想转换为Hex并把它放在UCHAR数组像

I have a cstring object str = "5043", now i want to convert to Hex and put it in UCHAR array like

UCHAR sample[2];
Sample[0] = 0X50 
Sample[1] = 0X43

我可以这样做吗?请通知

How can i do this? please advice me

推荐答案

您可以直接从字符串中使用 sscanf code>,如下所示:

You can scan the hex values directly from the string using sscanf(), something like below:

UCHAR sample[2];
for ( int i = 0; i < str.length() / 2 ; i++) {
    sscanf( (str.substr(i*2,2)).c_str(), "%hx", &sample[i]);
}

h

此外,假设 UCHAR <$> x / code>数组被声明为 string size的一半。

Also, this assumes that the UCHAR array is declared to be half as large as the string size.

这篇关于如何将CString转换为UCHAR阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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