Convert DNA string to binary
Getting fed up with writing this, so here’s some basic code to do it:
#include <iostream> using namespace std; uint32_t dna_number(string s) { uint32_t num = 0; for(size_t n=0;n<s.size();n++) { num = num << 2; if(s[n] == 'A') num += 0; if(s[n] == 'C') num += 1; if(s[n] == 'G') num += 2; if(s[n] == 'T') num += 3; } return num; } int main(int argc,char **argv) { cout << dna_number("AAAAA") << endl; cout << dna_number("AAAAC") << endl; cout << dna_number("AAAAG") << endl; cout << dna_number("AAAAT") << endl; cout << dna_number("AAACA") << endl; cout << dna_number("AAACC") << endl; cout << dna_number("AAACG") << endl; cout << dna_number("AAACT") << endl; cout << dna_number("AAAGA") << endl; cout << dna_number("AAAGC") << endl; cout << dna_number("AAAGG") << endl; cout << dna_number("AAAGT") << endl; cout << dna_number("AAATA") << endl; cout << dna_number("AAATC") << endl; cout << dna_number("AAATG") << endl; cout << dna_number("AAATT") << endl; cout << dna_number("AACAA") << endl; }