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

3 thoughts on “Convert DNA string to binary

  1. 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?

  2. 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?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>