Convert DNA string to binary
Getting fed up with writing this, so here’s some basic code to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #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; } |
What if my string is > 32 bits? if it is shorter than 32 than won’t it accidentally add extra As in the beginning of the sequence?
What if my string is > 32 bits? if it is shorter than 32 than won’t it accidentally add extra As in the beginning of the sequence?
eg. AAAAAAAATTTTTTTT == TTTTTTTT?
will u pl z tell me the code for converting binary to dna