求将 CString 字符串 转换成 ASC 码后保存到一个 CString 中的代码

aix8848 2008-10-14 01:42:33
求将 CString 字符串 转换成 ASC 码后保存到一个 CString 中的代码

如何ASCII码不够三位,前面补0 ,谢谢!!!
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2008-10-14
  • 打赏
  • 举报
回复
//仅以标准库中的string例
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str = "abcdefg0123456";
string result = "";
char buff[8];
for (int i = 0; i < str.length(); ++i)
result = result + itoa(str[i], buff, 10) + " ";//想要相连的话,直接去掉空格
while (result.length() < 3) result = "0" + result;
cout << result << endl;
for (int j = 0; j < str.length(); ++j)
cout << int(str[j]) << " "; //如果想要把元素要整数的形式访问,直接使用[]就行了
cout << endl;
return 0;
}
jia_xiaoxin 2008-10-14
  • 打赏
  • 举报
回复
写了一个转换函数

CString PrintAscII(CString m_str)
{
CString m_Org;
int nLen = m_str.GetLength();

m_Org.Empty();
for(int i = 0; i < nLen; i++)
{
char ch = m_str.GetAt(i);
char buf[10];
if(ch < 100)
m_Org += "0";
else if(ch < 10)
m_Org += "00";

itoa(ch, buf, 10);
m_Org += buf;
}

return m_Org;

}
wudeshou82666 2008-10-14
  • 打赏
  • 举报
回复
CString covert(CString s1)
{
char ch[2]={0};
int size = s1.GetLength();
char* pch = new char[size/2+1];
for(int i=0;i <size/2;i++)
{
ch[0]=s1.GetAt(2*i);
ch[1]=s1.GetAt(2*i+1);
pch[i]=strtol(ch,NULL,16);
}
CString s2(pch);
delete [] pch;
return s2;
}
wudeshou82666 2008-10-14
  • 打赏
  • 举报
回复
CString covert(CString s1)
{
char ch[2]={0};
int size = s1.GetLength();
char* pch = new char[size/2];
for(int i=0;i<size;i++)
{
ch[0]=s1.GetAt(2*i);
ch[1]=s1.GetAt(2*i+1);
pch[i]=strtol("%c",&ch,16);
}
CString s2(pch);
delete [] pch;
return s2;
}
不知道行不行
wudeshou82666 2008-10-14
  • 打赏
  • 举报
回复
CString 字符串 是HEX码的字符串吧
brightxu 2008-10-14
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080510/08/82f729b8-a8ea-429a-86f4-37939bf2ecf5.html
seese,then find another!
aix8848 2008-10-14
  • 打赏
  • 举报
回复
字符串由数字与英文字母组成

64,654

社区成员

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

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