数据7位,头部校验1位
#include<iostream> #include<string> using namespace std; string getCode(char c){ string s = ""; int sum = 0; int i=0; while(c!=0){ int digit = c%2; char remain = digit+'0'; s = remain+s; if(digit==1) sum++; i++; c=c>>1; } //带奇偶校验的ascii码,数据7位,前面有多个0时需要补齐 for(;i<7;i++) s='0'+s; //带奇偶校验的ascii码,头部校验位1位 if(sum%2==0){ s = '1'+s; } else { s = '0'+s; } return s; } int main(){ string s; while(cin>>s){ string::iterator it = s.begin(); while(it!=s.end()){ cout<<getCode(*it)<<endl; it++; } } return 0; }