#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll DecToBin(int dec){
ll ans = 0;
ll rem,coe = 1;
while(dec != 0){
rem = dec % 2;
ans += coe * rem;
coe *= 10;
dec /= 2;
}
return ans;
}
string LonglongToString(ll longlongcode){
ostringstream os;
os<<longlongcode;
string ans;
istringstream is(os.str());
is>>ans;
return ans;
}
string BinStrToHexStr(string str){
string str_Hex = "";
string temp_Hex = "";
int iHex = 0;
if (str.size() % 4 != 0){
int num = 4 - (str.size() % 4);
for (int i = 0; i < num; i++){
str = "0" + str;
}
}
for (int i = 0; i < str.size(); i += 4){
iHex = (str[i] - '0') * 8 + (str[i + 1] - '0') * 4 + (str[i + 2] - '0') * 2 + (str[i + 3] - '0') * 1;
if (iHex >= 10){
int left = iHex % 16;
temp_Hex = 'A' + left - 10;
}
else{
temp_Hex = to_string(iHex);
}
str_Hex += temp_Hex;
}
return str_Hex;
}
int main(){
cout<<"Input the passage that you need to encrypt it:";
cout<<endl;
string incode;
getline(cin,incode);
ll codelen = incode.length();
cout<<"This is the code you want:";
cout<<endl;
for(ll i = 0;i < codelen;i++){
if(incode[i] >= 'a' && incode[i] <= 'z'){
int code = int(incode[i]);
string hex = BinStrToHexStr(LonglongToString(DecToBin(code)));
cout<<hex.c_str()<<" ";
}
else if(incode[i] >= 'A' && incode[i] <= 'Z'){
int code = int(incode[i]);
string hex = BinStrToHexStr(LonglongToString(DecToBin(code)));
cout<<hex.c_str()<<" ";
}
else if(incode[i] == ' '){
cout<<" ";
}
else{
cout<<" "<<incode[i]<<" ";
}
}
return 0;
}
有想开发解密算法的人可以关注我!!!