中文截取全角处理

如果一个字串中含有中文和字母,笨的方法还是有一个,对所有的进行全角处理,如a一位转成2位,那样取值截取就不会有什么问题了,我暂时上传一个笨的程序,大家看了以后不要笑我啊。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/*打印时对字符转换 wuhongxing*/
void HvpsConvertChar(const char smsg,char * ret_msg)
{
switch (smsg)
{
case '0':
strcpy(ret_msg,"0");
break;
case '1':
strcpy(ret_msg,"1");
break;
case '2':
strcpy(ret_msg,"2");
break;
case '3':
strcpy(ret_msg,"3");
break;
case '4':
strcpy(ret_msg,"4");
break;
case '5':
strcpy(ret_msg,"5");
break;
case '6':
strcpy(ret_msg,"6");
break;
case '7':
strcpy(ret_msg,"7");
break;
case '8':
strcpy(ret_msg,"8");
break;
case '9':
strcpy(ret_msg,"9");
break;
case 'a':
strcpy(ret_msg,"a");
break;
case 'b':
strcpy(ret_msg,"b");
break;
case 'c':
strcpy(ret_msg,"c");
break;
case 'd':
strcpy(ret_msg,"d");
break;
case 'e':
strcpy(ret_msg,"e");
break;
case 'f':
strcpy(ret_msg,"f");
break;
case 'g':
strcpy(ret_msg,"g");
break;
case 'h':
strcpy(ret_msg,"h");
break;
case 'i':
strcpy(ret_msg,"i");
break;
case 'j':
strcpy(ret_msg,"j");
break;
case 'k':
strcpy(ret_msg,"k");
break;
case 'l':
strcpy(ret_msg,"l");
break;
case 'm':
strcpy(ret_msg,"m");
break;
case 'n':
strcpy(ret_msg,"n");
break;
case 'o':
strcpy(ret_msg,"o");
break;
case 'p':
strcpy(ret_msg,"p");
break;
case 'q':
strcpy(ret_msg,"q");
break;
case 'r':
strcpy(ret_msg,"r");
break;
case 's':
strcpy(ret_msg,"s");
break;
case 't':
strcpy(ret_msg,"t");
break;
case 'u':
strcpy(ret_msg,"u");
break;
case 'v':
strcpy(ret_msg,"v");
break;
case 'w':
strcpy(ret_msg,"w");
break;
case 'x':
strcpy(ret_msg,"x");
break;
case 'y':
strcpy(ret_msg,"y");
break;
case 'z':
strcpy(ret_msg,"z");
break;
case 'A':
strcpy(ret_msg,"A");
break;
case 'B':
strcpy(ret_msg,"B");
break;
case 'C':
strcpy(ret_msg,"C");
break;
case 'D':
strcpy(ret_msg,"D");
break;
case 'E':
strcpy(ret_msg,"E");
break;
case 'F':
strcpy(ret_msg,"F");
break;
case 'G':
strcpy(ret_msg,"G");
break;
case 'H':
strcpy(ret_msg,"H");
break;
case 'I':
strcpy(ret_msg,"I");
break;
case 'J':
strcpy(ret_msg,"J");
break;
case 'K':
strcpy(ret_msg,"K");
break;
case 'L':
strcpy(ret_msg,"L");
break;
case 'M':
strcpy(ret_msg,"M");
break;
case 'N':
strcpy(ret_msg,"N");
break;
case 'O':
strcpy(ret_msg,"O");
break;
case 'P':
strcpy(ret_msg,"P");
break;
case 'Q':
strcpy(ret_msg,"Q");
break;
case 'R':
strcpy(ret_msg,"R");
break;
case 'S':
strcpy(ret_msg,"S");
break;
case 'T':
strcpy(ret_msg,"T");
break;
case 'U':
strcpy(ret_msg,"U");
break;
case 'V':
strcpy(ret_msg,"V");
break;
case 'W':
strcpy(ret_msg,"W");
break;
case 'X':
strcpy(ret_msg,"X");
break;
case 'Y':
strcpy(ret_msg,"Y");
break;
case 'Z':
strcpy(ret_msg,"Z");
break;
case ',':
strcpy(ret_msg,",");
break;
case '.':
strcpy(ret_msg,"。");
break;
case '/':
strcpy(ret_msg,"/");
break;
case ' ':
break;
case '<':
strcpy(ret_msg,"<");
break;
case '>':
strcpy(ret_msg,">");
break;
case '?':
strcpy(ret_msg,"?");
break;
case '\\':
strcpy(ret_msg,"\");
break;
case '|':
strcpy(ret_msg,"|");
break;
case ':':
strcpy(ret_msg,":");
break;
case ';':
strcpy(ret_msg,";");
break;
case '[':
strcpy(ret_msg,"[");
break;
case ']':
strcpy(ret_msg,"]");
break;
case '{':
strcpy(ret_msg,"{");
break;
case '}':
strcpy(ret_msg,"}");
break;
case '`':
strcpy(ret_msg,"`");
break;
case '~':
strcpy(ret_msg,"~");
break;
case '!':
strcpy(ret_msg,"!");
break;
case '@':
strcpy(ret_msg,"@");
break;
case '#':
strcpy(ret_msg,"#");
break;
case '$':
strcpy(ret_msg,"¥");
break;
case '%':
strcpy(ret_msg,"%");
break;
case '^':
strcpy(ret_msg,"^");
break;
case '&':
strcpy(ret_msg,"&");
break;
case '*':
strcpy(ret_msg,"*");
break;
case '(':
strcpy(ret_msg,"(");
break;
case ')':
strcpy(ret_msg,")");
break;
case '-':
strcpy(ret_msg,"-");
break;
case '_':
strcpy(ret_msg,"_");
break;
case '=':
strcpy(ret_msg,"=");
break;
case '+':
strcpy(ret_msg,"+");
break;
case '\'':
strcpy(ret_msg,"'");
break;
case '\"':
strcpy(ret_msg,""");
break;
default :
ret_msg[0] = smsg;
ret_msg[1] = 0;
break;
}
}
void HvpsConvertStr(char *buf)
{
char temp[3];
int i, len = strlen(buf);
char *ret_msg = (char*) malloc(2 * len + 1);
memset(ret_msg, 0, 2 * len + 1 );

for (i = 0; i< len ; i++ )
{
if ( (unsigned char)buf[i] < 0x80 ) {
if ( (unsigned char)buf[i] != ' ' )
{
HvpsConvertChar(buf[i], temp);
strcat(ret_msg, temp);
}
} else
strncat(ret_msg, buf + i, 1);
}

strcpy( buf, ret_msg );
free(ret_msg);
}


int substring(char *str,char *substr,int start,int len)
{
char temp[1000];
int i=0;
memset( temp, 0, sizeof(temp));
if(strlen(str)<(start+len))
return (0);
for(i=0;i<len;i++)
temp[i]=str[start+i];
strcpy(substr,temp);

return (0);
}


int formatechar(char *str ,int type)
{
char substr[1000];
int len=0;
int ok=0;
memset( substr, 0, sizeof( substr ));

HvpsConvertStr(str);
len=strlen(str);

switch(type)
{
case 1:
ok=substring(str,substr,0,82);
strcpy(str,substr);
break;
case 2:
ok=substring(str,substr,82,82);
strcpy(str,substr);
break;
case 3:
ok=substring(str,substr,164,82);
strcpy(str,substr);
break;
case 4:

if(len>246)
{
if(len<320)
ok=substring(str,substr,246,len-246);
else
ok=substring(str,substr,246,82);
}
else
strcpy(substr,"");
strcpy(str,substr);
break;
default :
if(len>328)
ok=substring(str,substr,328,len-328);
else
strcpy(substr,"");
strcpy(str,substr);
break;
}
return (0);
}


int formatechar1(char *str )
{
int ok=0;
ok=formatechar(str,1);
return (0);
}

int formatechar2(char *str )
{
int ok=0;
ok=formatechar(str,2);
return (0);
}

int formatechar3(char *str )
{
int ok=0;
ok=formatechar(str,3);
return (0);
}

int formatechar4(char *str )
{
int ok=0;
ok=formatechar(str,4);
return (0);
}

int formatechar5(char *str )
{
int ok=0;
ok=formatechar(str,5);
return (0);
}


void main()
{
char str[1000];
int ok=0;
memset( str, 0, sizeof( str ));
strcpy(str,"汇票号码:08866228出票金额:100000.00出票日期:20090728汇票到期日:20100128出票人名称:慈溪市百花电器厂收款人名称:上海娜美电器有限公司付款行名称名称:宁波交行是否你行签发有无挂止付冻他查,上述信息给予核实,为谢!");
//ok=formatechar1(str);
ok=formatechar2(str);
//ok=formatechar3(str);
//ok=formatechar4(str);
printf(str);
printf("\n");
exit(0);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值