Hexadecimal display byte stream skill sharing

  • 2020-04-01 03:02:57
  • OfStack

Anyone using UE will find it convenient to display files in hexadecimal format. Why to knead? When you want to encrypt, transcode, and code files, jumping out of a pile of 01 binary looks big. After all, hexadecimal files are short and easy to display. At least in the college entrance examination tu card, 1+2+4+8 can be calculated is a few it. Of course, those in the high school and the college entrance examination can put 1248 yards are painted wrong children's shoes, a look will know that they decisively with the program ape this "god left" career ha......

Because I tried to participate in the science science innovation competition before, I read the file in byte stream, and turned it into binary, quadric, hexadecimal string, and then output it to the console. Then, according to the value of each bit, it is displayed in a 2-color, 4-color, 16-color matrix. The purpose of writing this is to use the camera to recognize, and then restore the byte stream to write to the file. The program is a hybrid of two painful facts -- 1. It requires no transmission of files through any media and USB devices; 2. 2. Sometimes the QRCode cannot be found in zxing and QRCode (QRCode is more error-prone). You can try this one, send and receive files with a camera or a loudspeaker, see who can do it fast, it's really interesting.

Of course, the most important part is the hexadecimal conversion, which reads the byte stream byte 8 bits of the file in two hexadecimal bytes. Because you may encounter byte to int as a complement after the case, so it is best to unify the positive number, the method is actually very simple, and an operation to do!


int result = bytes&0xff;

Don't underestimate this statement, it's actually very interesting. Come to think of it, why does adding 0xff become a positive number? 0xff each bit is 1, so doesn't the operation equal to no change? Hum, I've been asked this question before, and if it's true, it's because the foundation of Java is weak. Byte ranges from -128 to 127, not 0 to 255, so byte b= -42; Such an assignment must not use byte b= 214; Instead.

It's not that hard to get this out of the way and convert it to an n-base string. Come to think of it, is there a familiar but unfamiliar class that simply does this? Yes, with an Integer! But don't worry, before you use it, you have to do something with the int.


( bytes & 0xff ) + 0x100

Know why? This is going to be plus 256, just to make it a little bit more intuitive, it's just going to be plus one digit. Because if you get an int turned into a String, you're probably only going to get one bit, so you're going to lose a byte to hexadecimal, and the whole thing is going to be out of place. Just to be on the safe side, let's make it three.

You can try this one out and see all of the byte hexadecimal output


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

Do you look at this and think, why are you so stupid? Why not change the int in the for loop to byte? You will save one line of code. How cheap it is! Yeah, you try, I'm not going to...

So, the last code to convert byte to binary hexadecimal is


Integer.toString( ( bytes & 0xff ) + 0x100, 16).substring( 1 );

I'm not going to give you an example of the same thing for base two, base four, base eight. A single line of code, isn't that amazing...

The next step is to display the file with a lattice of matrix frames, use the camera to capture the recognition color, then convert the string, and feedback the color to let the other side know that the recognition is finished so that the next picture... This cycle continues until the matrix lattice display file has ended. I won't post this part of the code, the whole process can be created by rich imagination...

Then the string goes back to the byte stream, and that's simple


(byte)Integer.parseInt(string, 16)

You don't even need to do a bit operation, you just get the results, you just put them in a byte array, you just write them over and over again with a FileOutputStream! Don't forget to close the I/o stream


Related articles: