Based from your last comment Sam I believe I now understand what you require and as you can see in the code below it is relatively easy to accomplish providing specific rules are followed.
One such rule is that the Binary value for each ASCII character must be 8 bits. Because lower ASCII (0 to 127) only truly represents a 7 bit binary value (ie: A = 1000001 and z = 1111010) we must ensure that a 0 is padded to the left most end of the binary value so as to produce a definite 8 bit binary number. We need to do this because our ACGT translation requires two binary digits for each character (ie: A = 00, C = 11, G = 10, T = 01) and therefore all binary values (appended or not) must be dividable by 2 and have no remainder. If we left everything as 7 bit binary values then this can not be accomplish. Now, knowing that we need to append a 0 to the left most of each ASCII binary value to establish 8 bit we will find that the ACGT string will always start with either a 'T' or an 'A'. The ACGT string will never start with a 'C' or a 'G'. If this is unacceptable then the ACGT character to Binary translation must change or the Padding to our ASCII binary value must change. It should be the translation that changes because if a change is made to the ASCII Binary value then it will be a misrepresentation of the ASCII Binary which is not good.
Another rule is that the ACGT Character to Binary Translation always remains the same. It never changes throughout processing.
The new code I provide below carries out the task you described within your last comment. I will leave the previous code from my previous post as it is since someone may find that useful as well.
In this new code I have used a Scanner to receive input from a User for testing purposes. I understand that you will be retrieving strings from a database and I will leave that up to you as to how you will implement that to the code since placing the two conversional sections of this code into methods would be the best way to go here.
As with just about anything with Java, there are about 12 ways to do anything however I particularly used 'for loops' to process things here since it's the easiest to follow in my opinion. You can optimize this code any way you see fit once you have it working exactly the way way you want.
Here is the code (copy/paste/run):
import java.util.Arrays;
import java.util.Scanner;
public class CharacterTranslation {
public static void main(String[] args) {
// Get Input from User...
Scanner in = new Scanner (System.in);
System.out.println("*** CONVERT FROM STRING TO ASCII TO BINARY TO ACGT ***\n");
System.out.println("Please enter a String to Convert to ACGT:");
String inputString = in.nextLine();
// Declare and initialize required variables...
int[] inputAscii = new int[inputString.length()];
String[] inputBinary = new String[inputString.length()];
// Translation Table made from a two dimensional Array:
String[][] ACGTtranslation = {{"A","00"},{"T","01"},{"G","10"},{"C","11"}};
// ------------------------------------------------
// -------- CONVERT FROM STRING TO ACGT ----------
// ------------------------------------------------
//Convert the input string into ASCII numbers...
for (int i = 0; i < inputString.length(); i++) {
char character = inputString.charAt(i);
inputAscii[i] = (int) character;
}
System.out.println("Conversion To ASCII: " + Arrays.toString(inputAscii)
.replace("[","").replace("]",""));
//Convert the ASCII Numbers to 8 bit Binary numbers...
for (int i = 0; i < inputAscii.length; i++) {
String bs = String.valueOf(Integer.toBinaryString(0x100 +
inputAscii[i]).substring(2));
// Pad the left end of the binary number with 0 should
// it not be 8 bits. ASCII Charcters will only produce
// 7 bit binary. We must have 8 bits to acquire a even
// number of digit pairs for our ACGT convertion.
while (bs.length() < 8) { bs = "0" + bs; }
inputBinary[i] = bs;
}
System.out.println("Conversion To 8bit Binary: " + Arrays.toString(inputBinary)
.replace("[","").replace("]",""));
//Convert the Binary String to ACGT format based from
// our translational Two Dimensional String Array.
// First we append all the binary data together to form
// a single string of binary numbers then starting from
// the left we break off 2 binary digits at a time to
// convert to our ACGT string format.
// Convert the inputBinary Array to a single binary String...
String binaryString = "";
for (int i = 0; i < inputBinary.length; i++) {
binaryString+= String.valueOf(inputBinary[i]);
}
// Convert the Binary String to ACGT...
String ACGTstring = "";
for (int i = 0; i < binaryString.length(); i+= 2) {
String tmp = binaryString.substring(i, i+2);
for (int j = 0; j < ACGTtranslation.length; j++) {
if (tmp.equals(ACGTtranslation[j][1])) {
ACGTstring+= ACGTtranslation[j][0];
}
}
}
System.out.println("The ACGT Translation String for the Word '" +
inputString + "' is: " + ACGTstring + "\n");
// ------------------------------------------------
// ----- CONVERT FROM ACGT BACK TO STRING --------
// ------------------------------------------------
System.out.println("*** CONVERT FROM ACGT (" + ACGTstring +
"' TO BINARY TO ASCII TO STRING ***\n");
System.out.println("Press ENTER Key To Continue...");
String tmp = in.nextLine();
// Convert ACGT back to 8bit Binary...
String translation = "";
for (int i = 0; i < ACGTstring.length(); i++) {
String c = Character.toString(ACGTstring.charAt(i));
for (int j = 0; j < ACGTtranslation.length; j++) {
if (ACGTtranslation[j][0].equals(c)) { translation+= ACGTtranslation[j][1]; break; }
}
}
// We divide the translation String by 8 so as to get
// the total number of 8 bit binary numbers that would
// be contained within that ACGT String. We then reinitialize
// our inputBinary Array to hold that many binary numbers.
inputBinary = new String[translation.length() / 8];
int cntr = 0;
for (int i = 0; i < translation.length(); i+= 8) {
inputBinary[cntr] = translation.substring(i, i+8);
cntr++;
}
System.out.println("Conversion from ACGT To 8bit Binary: " +
Arrays.toString(inputBinary).replace("[","")
.replace("]",""));
//Convert 8bit Binary To ASCII...
inputAscii = new int[inputBinary.length];
for (int i = 0; i < inputBinary.length; i++) {
inputAscii[i] = Integer.parseInt(inputBinary[i], 2);
}
System.out.println("Conversion from Binary To ASCII: " + Arrays.toString(inputAscii)
.replace("[","").replace("]",""));
// Convert ASCII to Character String...
inputString = "";
for (int i = 0; i < inputAscii.length; i++) {
inputString+= Character.toString ((char) inputAscii[i]);
}
System.out.println("Conversion from ASCII to Character String: " + inputString);
System.out.println("** Process Complete ***");
}
}
EDIT:
I want to add that I have supplied a bit of a fib within the text. Most characters used within the ASCII character set (which are used for strings - ASCII 32 to 127) are represented by a 7 bit Binary value however, Characters within the Upper ASCII character set (128 to 255) are represented with an actual 8 bit binary value. The code provided takes this into account. I have edited my answer to accommodate this.