中文转ASCLL码转进制

· · 科技·工程

八进制

#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;
}

二进制

#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;
}

十六进制

#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;
}