⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conv.c

📁 用C语言实现的字符型的菜单界面
💻 C
字号:
/*----------------------------------------------------------------------------* * 功    能: 字符串转换操作库.						      * * 版    本: 1.00							      * * 运行环境: SCO UNIX V3.2.4 以上版本.					      * * 历史记录: 日      期	作  者	备    注				      * *----------------------------------------------------------------------------* *           1999/10/20	赖正兴	创建				      	      * *----------------------------------------------------------------------------* * All rights reserved. Copyright (C) 1999-20001.			      * *----------------------------------------------------------------------------*//*  Includes*/#include <port.h>#include <string.h>/*  Macros*//*  Local types*//*  Exported variables*//*  Local variables*//*  Macro functions*/#define hex(x)		((x)<=9 ? ((x)+'0') : ((x)+('A'-0x0a)) )/*  Functions*/static u_1by_t	char_ascii_to_hex( u_1by_t ascii){    u_1by_t	val;    val = ascii;    if( (val>='0') && (val<='9'))    {      val = val & 0x0f;    }    else    if( (val>='a') && (val<='f'))    {      val = val - 'a' + 0x0a;    }    else    if( (val>='A') && (val<='F'))    {      val = val - 'A' + 0x0a;    }    else    {      val = 0;    }    return( val);} /* char_ascii_to_hex */static void	convert_to_leading_spaces( u_1by_t *buf, u_2by_t len){  u_2by_t	i;  i = 0;  while( (i<len) and (*buf == '0'))  {    *buf = ' ';				/* change leading zeroes to spaces. */    buf++;    i++;  }  buf--;				/* got to last byte */  if( (i==len) and (*buf==' '))		/* if all spaces */  {    *buf = '0';				/* change last space back to zero */  }}void	hex_to_ascii( u_1by_t *ascii_buf,	u_1by_t *hex_buf,	u_2by_t ascii_len){  u_2by_t	i;  u_1by_t	val;  for( i=0; i<ascii_len; i++)  {    if( even( i))    {      val = *hex_buf;      *ascii_buf = hex( val >> 4);      ascii_buf++;    }    else    {      *ascii_buf = hex( val & 0x0f);      ascii_buf++;      hex_buf++;    }  }  convert_to_leading_spaces( ascii_buf - ascii_len, ascii_len);} /* hex_to_ascii */void	ascii_to_hex( u_1by_t *hex_buf,	u_1by_t *ascii_buf,	u_2by_t ascii_len){  u_2by_t	i, len;  len = (ascii_len+1) / 2;  for( i=0; i<len; i++)  {    *hex_buf  = char_ascii_to_hex( *ascii_buf) << 4;	/* high nibble */    ascii_buf++;    *hex_buf |= char_ascii_to_hex( *ascii_buf);		/* low  nibble */    ascii_buf++;    hex_buf++;  }  if( odd( ascii_len))  {    hex_buf--;    *hex_buf &= 0xf0;	/* odd lengths have the extra digit set to 0 */  }} /* ascii_to_hex */u_2by_t	bcd_to_bin(	u_1by_t *bcd_buf,	u_2by_t bcd_len){  u_2by_t	rc;  u_2by_t	i;  u_1by_t	val;  rc = 0;  for( i=0; i<bcd_len; i++)  {    val = *bcd_buf;    rc = rc*10 + (val >> 4);    rc = rc*10 + (val & 0x0f);    bcd_buf++;  }  return( rc);} /* bcd_to_bin */void	bin_to_bcd(	u_1by_t *bcd_buf,	u_2by_t bin,	u_2by_t bcd_len){  u_2by_t	i;  u_2by_t	multiple, remainder;  bcd_buf += bcd_len - 1;  for( i=bcd_len; i>0; i--)  {    multiple  = bin / 10;    remainder = bin - (multiple * 10);    bin = multiple;    *bcd_buf  = remainder;    multiple  = bin / 10;    remainder = bin - (multiple * 10);    bin = multiple;    *bcd_buf |= remainder << 4;    bcd_buf--;  }} /* bin_to_bcd */u_2by_t	ascii_to_bin(	u_1by_t *ascii_buf,	u_2by_t ascii_len){  u_2by_t	rc;  u_2by_t	i;  rc = 0;  for( i=0; i<ascii_len; i++)  {    rc = rc*10 + char_ascii_to_hex( *ascii_buf);    ascii_buf++;  }  return( rc);} /* ascii_to_bin */void	bin_to_ascii(	u_1by_t *ascii_buf,	u_2by_t bin,	u_2by_t ascii_len){  u_2by_t	i;  u_2by_t	multiple, remainder;  ascii_buf += ascii_len - 1;  for( i=ascii_len; i>0; i--)  {    multiple  = bin / 10;    remainder = bin - (multiple * 10);    bin = multiple;    *ascii_buf  = hex( remainder);    ascii_buf--;  }  ascii_buf++;		/* go to first byte */  convert_to_leading_spaces( ascii_buf, ascii_len);} /* bin_to_ascii *//*----------------------------------------------------------------------------* * FUNCTION: ascii码字符串转成bcd码字符串. * Argument: *           char *bcd_buf - bcd码字符串. *           char *asc_buf - asc码字符串. *           int asc_len - asc码字符串的长度. * Return  :  *           TRUE - 转换成功.                                   *           FALSE - 转换失败.                                   * Example : "1234567a"  ==> 0x12 0x34 0x56 0x7a *           "1234567"   ==> 0x12 0x34 0x56 0x7f *----------------------------------------------------------------------------*/ascii_to_bcd(char *bcd_buf,const char *asc_buf,int asc_len){	int i,bcd_len;	char ch,ch2,databuf[1025];	if(asc_len > 1024) return(FALSE);	bcd_len = (asc_len+1)/2;	memcpy(databuf,asc_buf,asc_len);	databuf[asc_len]='\0';	while(strlen(databuf) < asc_len)		strcat(databuf,"0");	for(i=0;i<bcd_len;i++)	{		ch =my_tolower(databuf[i*2]);		ch2=my_tolower(databuf[i*2+1]);		if(ch>='a' && ch<='z')		{			ch=(ch >'f') ? (0) : (ch-'a'+10);		}		else		{			ch=(ch<'0' || ch>'9') ? (0):(ch-'0');		}		if(ch2>='a' && ch2<='z')		{			ch2=(ch2 >'f') ? (0) : (ch2-'a'+10);		}		else		{			ch2=(ch2<'0' || ch2>'9') ? (0):(ch2-'0');		}		bcd_buf[i]=ch;		bcd_buf[i]<<=4;		bcd_buf[i]+=ch2;	}	return(TRUE);}/*----------------------------------------------------------------------------* * FUNCTION: ascii码字符串转成ascii码字符串(不补空格). * Argument: *           char *asc_des - asc码字符串. *           char *asc_src - asc码字符串. *           int asc_len - asc码字符串的长度. * Return  :  *           TRUE - 转换成功.                                   * Example : "12345678"  ==> "12345678"  *           "123456"    ==> "123456  " *----------------------------------------------------------------------------*/ascii_to_ascii(char *asc_des,char *asc_src,int asc_len){	int i;	memcpy(asc_des,asc_src,asc_len);	i=strlen(asc_src);	if(i < asc_len)		memset(asc_des+i,' ',asc_len-i);	asc_des[asc_len]='\0';	return(TRUE);}/*----------------------------------------------------------------------------* * FUNCTION: bcd码字符串转成ascii码字符串. * Argument: *           char *asc_buf - asc码字符串. *           char *bcd_buf - bcd码字符串. *           int bcd_len - bcd码字符串的长度. * Return  :  *           TRUE - 转换成功.                                   * Example : "\x12\x34\x56\x7a" ==> "1234567a" *----------------------------------------------------------------------------*/bcd_to_ascii(char *asc_buf,char *bcd_buf,int bcd_len){	int i;	for(i=0;i<bcd_len;i++)	{		sprintf(asc_buf[2*i],"%02x",bcd_buf[i]);	}	return(TRUE);}/* End of conv.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -