关于中文、字符互转

知周所众
在计算机中,所有的数据在存储和运算时都要使用二进制数表示
例如,像a、b、c、d这样的52个字母(包括大写)以及0、1等数字还有一些常用的符号(例如*、#、@等)在计算机中存储时也要使用二进制数来表示
于是就产生了编码

而我国字符编码也有几套标准,如GB2312、GBK、GB18030等

脑子一抽突发奇想 遂产生此代码

中文转字符

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mst(a,b) memset(a,b,sizeof(a))
#define re register
#define Elaina 0
void ct(int x){
	int x1=x/16,x2=x%16;
	if(x1>9){
        x1+='a'-10;
    }else x1+='0';
	if(x2>9){
        x2+='a'-10;
    }else{
        x2+='0';
    }
	char c1=x1,c2=x2;
	cout<<c1<<c2;
}
int main(){
	string a;
	cin>>a;
	int len=a.size();
	for(int i=0;i<len;++i){
		int x=a[i];
		ct(x+256);
	}
    return Elaina;
}

字符转中文

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mst(a,b) memset(a,b,sizeof(a))
#define re register
#define Elaina 0
int ct(char c){
	int x=0;
	if(c>='a'&&c<='f'){
        x=c-'a'+10;
    }else{
        x=c-'0';
    }
	return x;
}
int main(){
	string a;
	cin>>a;
	int sz=a.size();
	for(int i=0;i<sz;i+=2){
		int xx=ct(a[i])*16+ct(a[i+1]);
		char s=xx;
		cout<<s;
	}
    return Elaina;
}

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

夹带私货()

posted @ 2024-04-14 10:16  Elaina_0  阅读(24)  评论(1编辑  收藏  举报
浏览器标题切换
浏览器标题切换end