To follow along go to a ascii to binary converter and get a binary array for your text.
The text I am using is
Hi
9
This text when converted to binary looks like this
In the java program below these are things I will do
Create a byte array for my data
Create an InputStream object with the byte array
Use an InputStreamReader to interpret that InputStream data as ASCII data
Read the data in InputStreamReader with a BufferedReader
The text I am using is
Hi
9
This text when converted to binary looks like this
In the java program below these are things I will do
Create a byte array for my data
Create an InputStream object with the byte array
Use an InputStreamReader to interpret that InputStream data as ASCII data
Read the data in InputStreamReader with a BufferedReader
import java.io.*; public class Play { public static void main(String[] args) throws IOException { byte[] arrayOfBytes = new byte[] { (byte) 0b01001000, (byte) 0b01101001, (byte) 0b00001101, (byte) 0b00001010, (byte) 0b00111001 }; InputStream is = new ByteArrayInputStream(arrayOfBytes); InputStreamReader isr = new InputStreamReader(is, "ASCII"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); }
}
}
Dont forget to put 0b as a prefix when inputting binary data into the byte array.
You can also print out non ascii characters. You just need to change the setting of your IDE to allow that. In intelliJ this is done by changing File Encoding under your project settings
Here is an example that prints out 您好 (Hello in Chinese)
No comments:
Post a Comment
Comments will appear once they have been approved by the moderator