People who use UE will find it very convenient to display files in hexadecimal. Why pinch? When you want to encrypt, transcode, and encode the file, a bunch of 01 binary will pop up and look at it. After all, the hexadecimal display file is short and convenient. At least I have applied the card during the junior high school entrance examination and college entrance examination. 1+2+4+8 can be calculated as follows. Of course, those children's shoes that can paint 1248 in the middle school entrance examination and college entrance examination can tell at a glance that they will definitely have no chance of being the "sacred" profession of programmers...
Because I tried to participate in the science popularization innovation competition before, I did Dongdong at that time, read the file in byte streams and convert it into binary, quad, and hexadecimal strings, and then output it to the console at once . Then, according to the value of each bit, it is displayed in a matrix of 2 colors, 4 colors and 16 colors respectively. The purpose of writing this is to use the camera to identify it, and then restore it to a byte stream and write it to the file. This program design is the product of a hybrid of two painful facts - 1. It requires no transfer of files through any media and USB devices; 2. zxing and QRCode sometimes cannot find the QR code (QRCode is more prone to errors). You can try this question, use a camera or speaker to send and receive files to see who can transmit it quickly. The question is really interesting.
Of course, the most important part is that the byte stream of the file is read into 8 bits bytes, and displayed in two hexadecimals. Because you may encounter the situation where byte is converted to int and complement code, it is best to unify it into a positive number first. The method is actually very simple, and you can do it with one and the operation!
The code copy is as follows:
int result = bytes&0xff;
Don't underestimate this sentence, it's actually very interesting. Think about it carefully, if you add a 0xff to Shenma and it becomes a positive number? 0xff is 1, so doesn’t it mean that there is no change in operation? Humph, someone has asked me this before. If there is such a problem, it is that the Java foundation is not solid. The range of byte is -128~127, not 0~255, so, assignments like byte b= -42; must not be replaced by byte b= 214;.
It's not difficult to make this and convert it into an N-digit string. If you think about it carefully, is there a familiar but unfamiliar class that directly brings this function? That's right, it's just using Integer! But don't worry, before using it, you have to do some small moves on this int.
The code copy is as follows:
( bytes & 0xff ) + 0x100
Do you know why? This is +256, just to see it more intuitively, just add one in front. Because if the int you get is converted to String, it is likely that there is only one bit, that is, if byte is lost when hexadecimal is converted to hexadecimal, then will the entire program be completely misplaced? To be safe, it’s better to unify three.
You can try this to see all the outputs after the byte is changed to hexadecimal
The code copy is as follows:
public static void main(String[] args) {
for (int i = -128; i < 128; i++) {
byte b=(byte)i;
System.out.println( Integer.toString( ( b & 0xff ), 16));
}
}
Seeing this, would you think: Why are you so stupid? Why didn’t the int in the for loop be changed to byte? Isn’t it just a line of code saved? How cheap! OK, you can try it, I won't try it anyway...
So, the code that finally converts byte into two-bit hexadecimal is
The code copy is as follows:
Integer.toString( ( bytes & 0xff ) + 0x100, 16).substring( 1 );
For binary, quad and octal, all of them are the same, so I won't give an example. It's implemented in just one sentence of code, it's amazing...
Next, use the matrix lattice to display the file frame by frame, use the camera to capture the recognition color, then convert the string, and feedback the color to let the other party know that the recognition is completed and then replace the next picture... This cycle is until the matrix lattice The file has ended. I won't post this part of the code, the whole process can be produced by YY with rich imagination...
Then the string is turned back into the byte stream, which is simple to live
The code copy is as follows:
(byte)Integer.parseInt(string, 16)
You don’t even need bit operations, just produce the results, put them in a byte array, and write them repeatedly with FileOutputStream’s write! Don't forget to turn off the input and output stream