跳到正文

科技·工程

中文转ASCLL码转进制

I_Hate_Maths约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年7月14日
收录
2026年7月14日
更新
2026年11月17日
篇幅
约 3 分钟

版本

  1. v1当前

八进制

C++
#include<bits/stdc++.h>
using namespace std;
int a[200005];
char HX[20]={'0','1','2','3','4','5','6','7'};
string tt(int x){
	int y=x;
	stack<char> st;
	while(y){
		st.push(HX[y%8]);
		y/=8;
	}
	string t;
	while(!st.empty()){
		t+=st.top();
		st.pop();
	}
	if(t.length()==2) t='0'+t;
	string th="";
	return th+t;
}
int main(){
	string s;
	getline(cin,s);
	for(int i=0;i<s.length();i++)
		a[i]=int(s[i]);
	for(int i=0;i<s.length();i++){
		if(s[i]==' '){
			cout<<"\n";
			continue;
		}
		cout<<tt(a[i])<<' ';
	}
	return 0;
}

二进制

C++
#include<bits/stdc++.h>
using namespace std;
int a[200005];
string tt(int x){
	int y=x;
	stack<char> st;
	while(y){
		if(y%2) st.push('1');
		else st.push('0');
		y/=2;
	}
	string t;
	while(!st.empty()){
		t+=st.top();
		st.pop();
	}
	int tl=t.length();
	string th="";
	while(tl!=8) th+='0',tl++;
	return th+t;
}
int main(){
	string s;
	getline(cin,s);
	for(int i=0;i<s.length();i++)
		a[i]=int(s[i]);
	for(int i=0;i<s.length();i++){
		if(s[i]==' '){
			cout<<"\n";
			continue;
		}
		cout<<tt(a[i])<<' ';
	}
	return 0;
}

十六进制

C++
#include<bits/stdc++.h>
using namespace std;
int a[200005];
char HX[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
string tt(int x){
	int y=x;
	stack<char> st;
	while(y){
		st.push(HX[y%16]);
		y/=16;
	}
	string t;
	while(!st.empty()){
		t+=st.top();
		st.pop();
	}
	int tl=t.length();
	string th="";
	return th+t;
}
int main(){
	string s;
	getline(cin,s);
	for(int i=0;i<s.length();i++)
		a[i]=int(s[i]);
	for(int i=0;i<s.length();i++){
		if(s[i]==' '){
			cout<<"\n";
			continue;
		}
		cout<<tt(a[i])<<' ';
	}
	return 0;
}
原文