CString数据如何转换成16进制数据

chenyuechong 2009-02-10 05:11:30
RTU帧需要数据为16进制的形式发送,如果在输入框中以CString形式读取数据,那么得到的数据如何转换为16进制数据,这16进制数据存储格式可以是char数组也可以是BYTE数组!
...全文
1585 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2009-02-11
  • 打赏
  • 举报
回复
up
太乙 2009-02-11
  • 打赏
  • 举报
回复
up
独孤过儿 2009-02-11
  • 打赏
  • 举报
回复
如果你用的是微软的IDE,可以试试这个函数(copy自MSDN):

Converts a string representing a decimal or hexadecimal number to an integer.

Syntax

BOOL StrToIntEx( LPCTSTR pszString,
STIF_FLAGS dwFlags,
int *piRet
);
Parameters

pszString
[in] A pointer to the null-terminated string to be converted. For further details concerning the
valid forms of the string, see the Remarks section.
dwFlags
[in] One of the following values specifying how pszString should be parsed for its conversion to an
integer.
STIF_DEFAULT
The string at pszString contains the representation of a decimal value.
STIF_SUPPORT_HEX
The string at pszString contains the representation of either a decimal or hexadecimal value. Note
that in hexadecimal representations, the characters A-F are case-insensitive.
piRet
[out] A pointer to a variable of type int that receives the integer value of the converted string.
For instance, in the case of the string "123", the integer pointed to by this value receives the
integer value 123.
If this function returns FALSE, this value is undefined.

If the value returned is too large to be contained in a variable of type int, this parameter
contains the 32 low-order bits of the value. Any high-order bits beyond that are lost.

Return Value

Returns TRUE if the string is converted, or FALSE otherwise.

Remarks

The string pointed to by the pszString parameter must have one of the following forms to be parsed
successfully.

This form is accepted as a decimal value under either flag.
(optional white space)(optional sign)(one or more decimal digits)

These forms are required for hexadecimal values when the STIF_SUPPORT_HEX flag is passed.
(optional white space)(optional sign)0x(one or more hexadecimal digits)

(optional white space)(optional sign)0X(one or more hexadecimal digits)


The optional sign can be the character '-' or '+'; if omitted, the sign is assumed to be positive.

Note If the value is parsed as hexadecimal, the optional sign is ignored, even if it is a '-'
character. For example, the string "-0x1" is parsed as 1 instead of -1.
If the string pointed to by pszString contains an invalid character, that character is considered
the end of the string to be converted and the remainder is ignored. For instance, given the invalid
hexadecimal string "0x00am123", StrToIntEx only recognizes "0x00a", converts it to the integer
value 10, and returns TRUE.

netlib 2009-02-10
  • 打赏
  • 举报
回复
十六进制字符串转换成十六进制数字

char* CHAR2HEX(int length, unsigned char* a,char *temp)

{

bzero(temp,sizeof(temp));

int i, b;

for(i = 0; i < length; i ++)

{

char c[3];

c[0] = a[0 + 2 * i];

c[1] = a[1 + 2 * i];

c[2] = '\0';

sscanf(c, "%x", &b); //把每两个字节的十六进制字符串转换成十六进制数字

temp[i] = ((b / 16) << 4) | (b % 16); //把十六进制数字压缩成单字节并转换成字符

}

return temp;

}


hearoequal 2009-02-10
  • 打赏
  • 举报
回复 1
整那么麻烦干嘛
#include <stdlib.h>

long strtol( const char *start, char **end, int base );

第一个参数是你的 字符串
第二个参数填0
第三个参数填16

long num = strtol("fff", 0, 16)

num的值就是0xfff
waizqfor 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 chenyuechong 的帖子:]
RTU帧需要数据为16进制的形式发送,如果在输入框中以CString形式读取数据,那么得到的数据如何转换为16进制数据,这16进制数据存储格式可以是char数组也可以是BYTE数组!
[/Quote]
转一个函数 字符串和16进制转换的
也可以用
CString s="aa";
int b;
sscanf(s, "%x",&b); //可以这样强制转换

*------------------------------------------------------------

函数名 : TransData(CString InStr, PUCHAR OutStr)

功 能 : 将字符串转换成十六进制数据

参 数 : CString InStr [in] 输入字符串

PUCHAR OutStr [out] 输出转换后数据的指针

返回值 : BOOL,正确返回TRUE,错误返回FALSE

------------------------------------------------------------*/
BOOL TransData(CString InStr, PUCHAR OutStr)
{
UINT i = 0, j = 0;
UINT len = 0;
UCHAR Str[KEY_LEN * 2];

len = InStr.GetLength();

if ((len % 2) != 0)
{
MessageBox("请按要求输入!", "错误", MB_OK | MB_ICONERROR);
return FALSE;
}

memset(OutStr, 0, sizeof(UCHAR[KEY_LEN]));
memset(Str, 0, sizeof(UCHAR[KEY_LEN * 2]));
strcpy((char*)(Str), InStr.GetBuffer(0));
InStr.ReleaseBuffer();

for(i = 0; i < len; i++)
{
if ((Str[i] > 47) && (Str[i] < 58))
{
Str[i] -= 48;
}
else if ((Str[i] > 64) && (Str[i] < 71))
{
Str[i] -= 55;
}
else if ((Str[i] > 96) && (Str[i] < 103))
{
Str[i] -= 87;
}
else
{
MessageBox("数据格式错误!", "错误", MB_OK | MB_ICONERROR);
return FALSE;
}
}

i = 0;
while(i < len)
{ // 转换
Str[j] = Str[i] * 16 + Str[i + 1];
i += 2;
j++;
}
scott_2009 2009-02-10
  • 打赏
  • 举报
回复
求CString字符串的长度写一个循环。循环中用CSting::GetAt()函数将字符复制到char或byte数组中然后再转换成16进制的形式。
Jack_xiao 2009-02-10
  • 打赏
  • 举报
回复
UP
fibbery 2009-02-10
  • 打赏
  • 举报
回复
具体呢?什么样的数据,需要什么样的输出,最好给出例子,大家才能给你算法。
cppfaq 2009-02-10
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20060215/16/4557677.html

4 楼joinclear()回复于 2006-02-15 16:49:29 得分 10
DWORD H2D(CString B)
{
DWORD D;
DWORD tmpD;
B.Remove (' ');
CString tmpBit;
int BitLen = B.GetLength();
for (int i=0; i<BitLen; i++)
{
tmpBit = B.Mid(BitLen-i-1,1);
if (tmpBit == "A" || tmpBit == "a") tmpD = 10;
else if (tmpBit =="B" || tmpBit == "b") tmpD =11;
else if (tmpBit =="C" || tmpBit == "c") tmpD =12;
else if (tmpBit =="D" || tmpBit == "d") tmpD =13;
else if (tmpBit =="E" || tmpBit == "e") tmpD =14;
else if (tmpBit =="F" || tmpBit == "f") tmpD =15;
else sscanf( tmpBit, "%d", &tmpD );
if (i==0)
D = tmpD;
else
{
tmpD = tmpD * (2<<(i*4-1));
D = D + tmpD;
}

}
return D;
}
hhyttppd 2009-02-10
  • 打赏
  • 举报
回复
顶,不知道什么意思
xxweilw 2009-02-10
  • 打赏
  • 举报
回复
http://hi.baidu.com/mooncbz/blog/item/4f3393d10ee204d5562c846e.html

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧