我取到一个长字符串303130313031303033.....我要把它转换成010101003这样的字符串,我应该如何做呢

yannankai 2003-11-18 02:00:17
?
...全文
111 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsm0001 2003-11-20
  • 打赏
  • 举报
回复
这样效率稍高一点
CString str=="303130313031303033";

void ConvertDat(CString &str)
{
int i,n,nLen=str.GetLength();

n=0;
for(i=0;i<nLen-1;i=i+2)
{
str.SetAt( n,(str[i]-48)*16+(str[i+1]-48) );
n++;
};
str.SetAt(n,0);
}
tglong 2003-11-20
  • 打赏
  • 举报
回复
CString ConvertDat()
{
CString str = "30313031";
CString strTmp,strResult;
int i,Len = str.GetLength();
for( i = 0 ; i < Len - 1 ; i += 2)
{
strTmp.Format("%c",( ( str[i] & 0xF ) << 4 | str[i+1] & 0xF ) );
strResult += strTmp;
}
return strResult;
}
lsm0001 2003-11-20
  • 打赏
  • 举报
回复
CString str="303130313031303033";
CString strTem,strResult;
int i,nLen=str.GetLength();

for(i=0;i<nLen-1;i=i+2)
{
strTem.Format("%c",(str[i]-48)*16+(str[i+1]-48) );
strResult+=strTem;
};

return strResult;
lsm0001 2003-11-20
  • 打赏
  • 举报
回复
CString str="303130313031303033";
CString strTem,strResult;
int i,nLen=str.GetLength();

for(i=0;i<nLen-1;i=i+2)
{
strTem=str.Mid(i,2);
strTem.Format("%c",(str[i]-48)*16+(str[i+1]-48) );
strResult+=strTem;
};

return strResult;
Bandry 2003-11-20
  • 打赏
  • 举报
回复
// TCHAR Version
char x2c(char *what)
{
register char digit;

digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}

void ToStr( char* s )
{
/*ASSERT s is long enough to store new string */
char newstr[6000];
int i;
for( i=0; s[i] != '\0'; i+=2 )
{
newstr[i/2] = x2c( s+i );
}
newstr[i/2] = '\0';
strcpy( s, newstr );
}

// CString Version
char HexToCh(char ch)
{
switch(ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return ch - '0';
break;
default:
return 10 + ch - 'A';
break;
}
return ch;
}

void Hex2Str(CString & strHex, CString & strR)
{
int nLen = strHex.GetLength();
if ((nLen % 2) != 0)
{
nLen -= 1;
}
strR.Empty();
char ch;
for (int i = 0; i < nLen; i+=2)
{
ch = HexToCh(strHex.GetAt(i)) * 16 + HexToCh(strHex.GetAt(i+1));
strR += ch;
}
}
我记得给过你啊
lashengcrh 2003-11-20
  • 打赏
  • 举报
回复
//ASC=>HEX
void ASCTOHEX(BYTE *Dest,BYTE *Source,UINT HEXLen)
{
UINT uicont;
BYTE ByteH,ByteL;

for(uicont=0;uicont<HEXLen;uicont++)
{
ByteH=*(Source+uicont*2);
if(ByteH>='A' && ByteH<='F')
{
ByteH=ByteH-0x41+10;
}
else if(ByteH>='a' && ByteH<='f')
{
ByteH=ByteH-0x61+10;
}
else
{
ByteH=ByteH-0x30;
}
ByteL=*(Source+uicont*2+1);
if(ByteL>='A' && ByteL<='F')
{
ByteL=ByteL-0x41+10;
}
else if(ByteL>='a' && ByteL<='f')
{
ByteL=ByteL-0x61+10;
}
else
{
ByteL=ByteL-0x30;
}
*(Dest+uicont)=ByteH*16+ByteL;
}
}
lashengcrh 2003-11-20
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////
//HEX=>ASC
void HEXTOASC(BYTE *Dest,BYTE *Source,UINT HEXLen)
{
UINT uicont;
BYTE ByteH,ByteL;
for(uicont=0;uicont<HEXLen;uicont++)
{
ByteH=*(Source+uicont)/16;
if(ByteH>=0x0a && ByteH<=0x0f)
{
ByteH=ByteH-10+0x41;
}
else
{
ByteH=ByteH+0x30;
}
ByteL=*(Source+uicont)%16;
if(ByteL>=0x0a && ByteL<=0x0f)
{
ByteL=ByteL-10+0x41;
}
else
{
ByteL=ByteL+0x30;
}
*(Dest+uicont*2)=ByteH;
*(Dest+uicont*2+1)=ByteL;
}

}
  • 打赏
  • 举报
回复
而且,你得保证是单字节英文或为HEX,不然取2位也是会错的。
wuxfBrave 2003-11-18
  • 打赏
  • 举报
回复
只有0-9的话简单,直接取奇数位(0开始)
bohut 2003-11-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char szBuf[] = "313030313030313331";
char sz[20] ;
char szTemp[2];
int nTemp;

memset(sz,0,sizeof(sz));

for(int i = 0; i < strlen(szBuf); i+=2)
{
sprintf(szTemp,"%c%c",szBuf[i],szBuf[i+1]);
nTemp = atoi(szTemp) - 30;
sprintf(szTemp,"%d",nTemp);
strcat(sz,szTemp);
}
printf("%s\n",sz);
return 0;
}
milanshi 2003-11-18
  • 打赏
  • 举报
回复
2位2位取当然行,可是速度........
一条晚起的虫 2003-11-18
  • 打赏
  • 举报
回复
同意 brightboy,2位2位取,然后减30就行了
brightboy 2003-11-18
  • 打赏
  • 举报
回复
减30
yannankai 2003-11-18
  • 打赏
  • 举报
回复
实际上303130313031303033 是 010101003 的Asicc值,这样做肯定不行的
  • 打赏
  • 举报
回复
取偶数位置的字符不就行了吗?
yannankai 2003-11-18
  • 打赏
  • 举报
回复
帮帮忙吧,这两天都上火了

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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