Java ASCII control codes to literal values - java

I might be somewhat stupid here, but I can't seem to think of a straightforward solution to this problem.
I've currently got an int[] that contains ASCII character codes, however, with the ASCII table, any value < 32 is a control code. So what I need to do is for any value > 32, put the ASCII character into a char[], however if it's < 32, just put the literal integer value in as a character.
For example:
public static void main(String[] args) {
int[] input = {57, 4, 31}; //57 is the only valid ASCII character '9'
char[] output = new char[3];
for (int i = 0; i < input.length; i++) {
if (input[i] < 32) { //If it's a control code
System.out.println("pos " + i + " Not an ascii symbol, it's a control code");
output[i] = (char) input[i];
} else { //If it's an actual ASCII character
System.out.println("pos " + i + " Ascii character, add to array");
output[i] = (char) input[i];
}
}
System.out.println("\nOutput buffer contains:");
for (int i = 0; i < output.length; i++) {
System.out.println(output[i]);
}
}
Output is:
pos 0 Ascii character, add to array
pos 1 Not an ascii symbol, it's a control code
pos 2 Not an ascii symbol, it's a control code
Output buffer contains:
9 // int value 57, this is OK
As you can see the last two entries in the array are blank, as there isn't actually an ASCII character for either 4, or 31. I know there are methods for converting Strings to char[], however what's the general idea when you've already got a char[] in which you want the value.
There is probably a really easy solution for this, I think I'm just having a dumb moment!
Any advice would be appreciate, thanks!

For classifying characters you should use the Character.getType(char) method.
To store either a character or an integer you could try using a wrapper object to do that.
Alternatively you could wrap your char like this:
static class NiceCharacter {
// The actual character.
final char ch;
public NiceCharacter ( char ch ) {
this.ch = ch;
}
#Override
public String toString () {
return stringValue(ch);
}
public static String stringValue ( char ch ) {
switch ( Character.getType(ch)) {
// See http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters for what the Cc group is.
// See http://en.wikipedia.org/wiki/Control_character for a definition of what are CONTROL characters.
case Character.CONTROL:
return Integer.toString(ch);
default:
return Character.toString(ch);
}
}
}

Change how you print the output buffer
for (int i = 0; i < output.length; i++) {
if (output[i] < 32){
System.out.println("'" + (int)output[i] + "'"); //Control code is casted to int.
//I added the ' ' arround the value to know its a control character
}else {
System.out.println(output[i]); //Print the character
}
}

Related

How to convert Ascii to Unicode in java?

I have a string is half-size font and i want to convert it to full size. I was tried to use this code
final String x = "01589846";
String b = "";
System.out.print("01589846");
int y = 0;
final char[] list = x.toCharArray();
for (int i = 0; i < list.length; i++) {
y = Integer.parseInt(String.valueOf(list[i]));
final char unicode = (char) (y + 65296);
b += unicode;
}
System.out.println(b);
}
it actually working but it only working with number.
Anyone have another way for this ? please help me !!!!!
Java Strings are Unicode. They don't need converting. Java does not natively use ASCII.
You apparently wish to map one set of Unicode characters to another. The appropriate tool for that would be a Map, but you'll have to populate the Map with your desired conversion taken from the Unicode code charts.
There may be some algorithmic way to do this for particular subranges; you seem to have discovered a way that works for (western) digits. Note that the fullwidth digits occupy codepoints 0xFF10 to 0xFF19, so the conversion formula is digit - '0' + 0xff10. 0xFF10 is 65296 decimal, but the hex is clearer, since it's what is used in published code charts.
Actually, it looks to me that the same thing works for all characters in the range SPACE to '~', presumably by design. Thus
for (int i=0; i<list.length; i++)
list[i] += 0xff00 - ' ';
Here, I simply assume without checking that list will only contain characters in the range of SPACE to '~', i.e., the Unicode range that corresponds to graphic (printable) ASCII characters. Dealing with other characters, for example Katakana, is more involved.
final String x = "012345 abcdef ABCDEF";
System.out.println(x);
String b = "";
final char[] list = x.toCharArray();
for (int i = 0; i < list.length; i++) {
if(Character.isDigit(list[i])) {
b += (char)(list[i] - 48 + 0xFF10);
} else if(Character.isUpperCase(list[i])) {
b += (char)(list[i] - 65 + 0xFF21);
} else if(Character.isLowerCase(list[i])) {
b += (char)(list[i] - 97 + 0xFF41);
} else if(Character.isWhitespace(list[i])) {
b += list[i];
} else {
b += (char)(list[i] - 33 + 0xFF01);
}
}
System.out.println(b);
Output:
012345 abcdef ABCDEF
012345 abcdef ABCDEF

Would need a little bit of clarification

Can you guys, please, explain to me what does count[word.charAt(i)]++, exactly do in this code and overall--?
public static void main(String[] args) {
String S = "Some random text to test.";
int count[] = new int[124];
for (int i=0; i< S.length(); i++) {
count[S.charAt(i)]++;
System.out.print(count[S.charAt(i)] + " ");
}
int max = 1;
char result = ' ';
for (int i = 0; i < S.length(); i++) {
if (max < count[S.charAt(i)] && S.charAt(i) != ' ') {
max = count[S.charAt(i)];
result = S.charAt(i);
}
}
System.out.println(result);
}
The printing of count[S.charAt(i)] was just me trying to figure it out.
S.charAt(i) returns the character in the i-th position of that string S.
Then count[S.charAt(i)] will execute like this. Lets say you get 'S' as the character. Then the character value for 'S' will be 83. So, it will take the element of the 83 index in count array and increment it by one.
word.charAt(i) returns the character at the i-th index in the String word.
count is an int array with all zeros automatically : int count[] = new int[124];
count[i]++ increments the value that is in count at index i by 1.
Here, you're passing word.charAt(i) as an index i.e count[word.charAt(i)]++, and what it does is:
-Evaluate word.charAt(i) first, but
note that index i must be an integer!
so automatically gets the ASCII value of the character. For example ('a' = 97, 'b' = 98..)
-then, count[ASCII number returned]++, for example count[97]++, will be incremented and now count[97] = 1
But note that if your String has '}', there will be Index out of bound exception, since its ASCII value is 125; and 125 > 124 the size of count!

Char array printing random characters

I'm really struggling to print an array with random letters. If anybody could help me that would be great :)
public class CharFilter
{
public static void main(String args[])
{
int rows = 10;
int cols = 10;
char grid[][] = new char [rows][cols];
for(int i=0; i<grid.length;i++)
{
for(int j=0; j<grid[i].length;j++)
{
grid[i][j] = (char) (Math.random()*'a')+'b';
String gprint = "";
gprint = gprint + String.format("%2c", grid[i][j]);
System.out.println(gprint);
}
}
}
Your code won't compile for me because your parentheses around Math.random should include 'b', e.g.
grid[i][j] = (char) (Math.random()*'a'+'b');
I would also skip storing the individual characters in a string, and just print each character, unless for some reason you needed to do it like that.
System.out.println(grid[i][j]);
instead of
String gprint = "";
gprint = gprint + String.format("%2c", grid[i][j]);
System.out.println(gprint);
Finally, if you're specifically trying to have the grid only contain lowercase letters, your formula is a little off. Use Math.random() * ('z' - 'a') + 'a'because random will give you a value between 0 and 1, but your desired range is 26 ('z' - 'a'), not 1, and you want the initial value to be 'a', not 0.

converted 4 letters to a specific binary digits how can I convert these binary to ASCII

int re = 0;
for (int t=0; t<mat1.length-1; t++){
if(mat1[t]=="A"){
re=re+00;
}else if(mat1[t]=="T"){
re=re+01;
}else if(mat1[t]=="G"){
re=re+10;
}else if(mat1[t]=="C"){
re=re+11;
}
System.out.println(mat1[t]);
}
I want these codes translated from the binary we choose to ASCII and then the ASCII will know the values
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.
Sam, you'll need to create some sort of translation table so as to know which makeshift binary bit relates to whichever character and I say makeshift because 00 is nothing equivalent to the letter "A" in binary. The same applies to the binary representation for the other characters you provide as well. What the binary representation might be is irrelevant at this point since you can do whatever you want for your specific purpose. If possible though, proper representation is the way to go for more advanced functionality later on down the road AND you wouldn't need a translation table because all you would need to do is convert the character ASCII value to binary, like this:
// 65 is the ASCII value for the letter A.
String letterAis = String.valueOf(Integer.toBinaryString(0x100 + 65).substring(2));
ALPHABET IN (8 bit) BINARY, CAPITAL LETTERS
A 01000001 N 01001110
B 01000010 O 01001111
C 01000011 P 01010000
D 01000100 Q 01010001
E 01000101 R 01010010
F 01000110 S 01010011
G 01000111 T 01010100
H 01001000 U 01010101
I 01001001 V 01010110
J 01001010 W 01010111
K 01001011 X 01011000
L 01001100 Y 01011001
M 01001101 Z 01011010
ALPHABET IN (8 bit) BINARY, LOWER CASE
a 01100001 n 01101110
b 01100010 o 01101111
c 01100011 p 01110000
d 01100100 q 01110001
e 01100101 r 01110010
f 01100110 s 01110011
g 01100111 t 01110100
h 01101000 u 01110101
i 01101001 v 01110110
j 01101010 w 01110111
k 01101011 x 01111000
l 01101100 y 01111001
m 01101101 z 01111010
You already have an Array variable named mat1[] which holds your string letters and what I propose is to make it a two dimensional array, 1 column to hold the letter and a 2nd column to hold the binary translation for that letter. Once you have the translation established you can convert back and forth from letter string to Binary and Binary to letter string. Here is the code (just copy/paste and run):
public class CharacterTranslation {
public static void main(String[] args) {
// Translation Table made from a two dimensional Array:
String[][] mat1 = {{"A","00"},{"T","01"},{"G","10"},{"C","11"}};
String input = "GCAT";
System.out.println("Original Input: " + input);
// Convert each character within the supplied input string
// to our binary character translation.
String translation = "";
for (int i = 0; i < input.length(); i++) {
String c = Character.toString(input.charAt(i));
for (int j = 0; j < mat1.length; j++) {
if (mat1[j][0].equals(c)) { translation+= mat1[j][1]; break; }
}
}
// Display the translation in output console (pane).
System.out.println("Convert To Binary Translation: " + translation);
// Now, convert the binary translation back to our
// original character input. Note: this only works
// if the binary translation is only 2 bits for any
// character.
String origInput = "";
for (int i = 0; i < translation.length(); i+= 2) {
String b = translation.substring(i, i+2);
for (int j = 0; j < mat1.length; j++) {
if (mat1[j][1].equals(b)) { origInput+= mat1[j][0]; break; }
}
}
// Display the converted binary translation back to
// it original characters.
System.out.println("Convert Back To Original Input: " + origInput);
}
}
For what you really want to do Sam, we only need one button. We can make it act like a toggle button between two different functions. One Button and One Text Field. This is pretty basic stuff.
The translation table 2 Dimensional Array should be placed under the constructor for your GUI class so that it's available to all methods, something like this:
public class MyGUIClassName??? extends javax.swing.JFrame {
String[][] ACGTtranslation = {{"A","00"},{"T","01"},{"G","10"},{"C","11"}};
..............................
..............................
..............................
}
Then your jButton3 ActionPerformed event could look somethIng like:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// Skip this event if there is nothing contained
// within jTextField1.
if (jTextField1.getText().isEmpty()) { return; }
// Get the current text in jButton3.
String buttonText = jButton3.getText();
// If the button text reads "Convert To ACGT" then...
if ("Convert To ACGT".equals(buttonText)) {
// change the button text to "Convert To String".
jButton3.setText("Convert To String");
// Convert the string from database now contained in jTextField1
// to ACGT format and place that new ACGT into the same JTextfield.
// We use the StringToACGT() method for this.
jTextField1.SetText(StringToACGT(jTextField1.getText());
}
// The button text must be "Convert To String"...
else {
// so let's change the button text to now be "Convert To ACGT"
// again.
jButton3.setText("Convert To ACGT");
// Take the ACGT string now contained within jTextField1
// from the first button click and convert it back to its
// original String format. We use the ACGTtoString() method
// for this.
jTextField1.SetText(ACGTtoString(jTextField1.getText());
}
}
And here are the methods you also place into your GUI class:
// CONVERT A STRING TO ACGT FORMAT
public static String StringToACGT(String inputString) {
// Make sure the input string contains something.
if ("".equals(inputString)) { return ""; }
// Declare and initialize required variables...
int[] inputAscii = new int[inputString.length()];
String[] inputBinary = new String[inputString.length()];
//Convert the input string into ASCII numbers...
for (int i = 0; i < inputString.length(); i++) {
char character = inputString.charAt(i);
inputAscii[i] = (int) character;
}
//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;
}
//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];
}
}
}
return ACGTstring;
}
// CONVERT A ACGT STRING BACK TO ITS ORIGINAL STRING STATE.
public static String ACGTtoString(String inputString) {
// Make sure the input string contains something.
if ("".equals(inputString)) { return ""; }
String ACGTstring = inputString;
// Declare and initialize required variables...
int[] inputAscii = new int[inputString.length()];
String[] inputBinary = new String[inputString.length()];
// 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++;
}
//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);
}
// Convert ASCII to Character String...
inputString = "";
for (int i = 0; i < inputAscii.length; i++) {
inputString+= Character.toString ((char) inputAscii[i]);
}
return inputString;
}
Like I said earlier Sam, this is pretty basic material and you should already have a very good handle onto the Java programming language to get to this point, especially if you are actually retrieving the data to convert from a Database. My job here is Complete. :o) I hope this has helped you (and others) to obtain your goals.

Replacing a searching character in Char array by its index value if it is found

// here i am try to find the location of a char in char array.
if character is resent in the array replace it's original value by its index that is an integer. but when i am try to compile it i got some non- printable out put.so please help me. i mean , when i try access the array element then effect of above operation must be seen.
class Re{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
String enter = input.nextLine();
char [] array = enter.toCharArray();
char find = input.next().charAt(0);
char find2 = Character.toUpperCase(find);
char find3 = Character.toLowerCase(find);
for (int i=0;i<array.length;i++)
{
if((find2==array[i])|| ( find3==array[i]))
{
array[i] = (char)(i);
}
else
{
array[i] =array[i];
}
}
for(i=0;i<array.length;i++)
{
System.out.print(array[i]);
}
}
}
Unicode has a lot of non-printable characters. See ASCII table for the ones you are probably getting (first 127 characters of UTF-16 are same as ASCII). If you want to convert an integer to its corresponding character value what you can do is add '0' to it.
Ex:
if (find2 == array[i] || find3 == array[i]) {
array[i] = (char)(i + '0');
} // omit your else self-assignment, it is redundant
If i > 9 then it is not convertable to a numerical character (only single digit) so you would have to use Strings.
Ex:
String[] out = new String[array.length];
for (int i = 0; i < array.length; i++) {
if (array[i] == findUp || array[i] == findLo) {
out[i] = String.valueOf(i);
} else {
out[i] = String.valueOf(array[i]);
}
}
You can also use Character#forDigit in the form of Character.forDigit(i, 10) which either returns the above expression ('0' + digit), an alphabetical character if the digit is > 9 ('a' + digit - 10) or a null character if the digit is outside the radix. IE Character.forDigit(10, 10) returns a null character but Character.forDigit(10, 16) (hex) returns 'a'.
When you cast between char and primitive number types (int, short, etc) what you are working with is the Unicode codepoint. For example (char)9 is actually the tab character. Unicode numbers correspond to codes 48-57. Because the numbers are contiguous (0 corresponds to 48, 1 corresponds to 49, 2 corresponds to 50, etc) you can add the value of '0' and get the corresponding character. For example 48 + 2 = 50 so '0' + 2 = '2'. Due to algebra you can also of course convert a character to an integer by subtraction IE '2' - '0' = 2.
You can make like this:
if ((find2 == array[i]) || (find3 == array[i])) {
array[i] = find;
}
else {
array[i] = array[i];
}

Categories

Resources