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

int main() { char str[100];//定义字符数组 int i,j,len; printf("请输入字符串:\n"); scanf("%s",str);//输入字符串

len=strlen(str);//获取字符串长度

for(i=0;i<len;i+=2)//每两个字符为一个字符进行处理
{
    char temp=str[i];//取出第一个字符
    if(temp>='a'&&temp<='f')//如果是小写字母,转化为大写字母
        temp-=32;
    int num1=temp-'0';//将字符转化为数字
    if(temp>='A'&&temp<='F')//如果是大写字母,减去'A'再加上10,得到对应数字
        num1=temp-'A'+10;

    temp=str[i+1];//取出第二个字符
    if(temp>='a'&&temp<='f')//同上
        temp-=32;
    int num2=temp-'0';
    if(temp>='A'&&temp<='F')
        num2=temp-'A'+10;

    int num=num1*16+num2;//计算得到新字符对应的数字
    str[i]=num;//将新字符(数字)存入字符串中
    for(j=i+1;j<len-1;j++)//将后面的字符依次前移
        str[j]=str[j+1];
    str[len-1]='\0';//将字符串最后一位置为'\0',表示结束

    len--;//字符串长度减1
    i--;//循环变量i回退1,因为后面还有一次i++,相当于没变
}

len=strlen(str);//重新获取字符串长度
for(i=0;i<len;i++)//每个字符拆分成两个字符
{
    char temp=str[i];//取出字符
    int num=temp/16;//计算高4位
    if(num>=0&&num<=9)//数字
        str[i*2]=num+'0';//转化为字符
    else//字母
        str[i*2]=num-10+'A';

    num=temp%16;//计算低4位
    if(num>=0&&num<=9)//数字
        str[i*2+1]=num+'0';//转化为字符
    else//字母
        str[i*2+1]=num-10+'A';
}

printf("复原后的字符串为:%s\n",str);//输出字符串

return 0;

原文地址: https://www.cveoy.top/t/topic/eB6D 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录