Byte array to float implementation and byte conversion to other types of operation principle, bytefloat

Source: Internet
Author: User

Byte array to float implementation and byte conversion to other types of operation principle, bytefloat

The following is an implementation of converting byte arrays to float.

    public static float getFloat(byte[] b) {         int accum = 0;         accum = accum|(b[0] & 0xff) << 0;        accum = accum|(b[1] & 0xff) << 8;         accum = accum|(b[2] & 0xff) << 16;         accum = accum|(b[3] & 0xff) << 24;         System.out.println(accum);        return Float.intBitsToFloat(accum);     }

Note: The above byte array to float implementation is actually BitConvetor. toSingle () in C #; method!

 

When byte is converted to another type & Operation Principle:

See the following code before profiling the problem:

    public static String bytes2HexString(byte[] b) {        String ret = "";        for (int i = 0; i < b.length; i++) {            String hex = Integer.toHexString(b[i] & 0xFF);            if (hex.length() == 1) {                hex = "0" + hex;            }            ret += hex.toUpperCase();        }        return ret;    }

The above is to convert byte [] to a hex string. Note that here B [I] & 0xFF is used to calculate a byte and 0xFF, and then Integer is used. toHexString gets the hexadecimal string. We can see that
B [I] & 0xFF still produces an int. Why should I perform the operation with 0xFF? Directly Integer. toHexString (B [I]);, can I convert byte to int? The answer is no.

The reason is:
1. The byte size is 8 bits, and the int size is 32 bits.
2. the binary code of java uses the complement form.

Here we will first review basic computer Theory

A byte is saved in one byte and has eight bits, that is, eight 0 s and one S.
The first bit of the 8-digit is the symbol bit,
That is to say, 0000 0001 represents number 1.
1000 0000 represents-1
So the maximum number of positive numbers is 0111, Which is 1111.
The maximum number of negative numbers is 1111, which is-1111.

The above is the binary original code, but in java it adopts the form of complement code. What is the complement code below?

1. Anti-code:
If a number is positive, its anticode is the same as the original code;
If a number is negative, the symbol bit is 1, and the rest of the code is reversed;

2. Complement: with overflow, we can convert subtraction into addition.
For the decimal number, get 5 subtraction from 9:
9-4 = 5 because 4 + 6 = 10, we can use 6 as the complement of 4.
Rewrite to addition:
9 + 6 = 15 (remove the high 1, that is, minus 10) to get 5.

For hexadecimal numbers, subtraction from c to 5 is available:
C-7 = 5 because 7 + 9 = 16 will 9 as the complement of 7
Rewrite to addition:
C + 9 = 15 (remove high 1, that is, minus 16) to get 5.

In a computer, if we use one byte to represent a number, and one byte has eight bits, more than eight bits will enter 1. In the memory, this is (100000000), and carry 1 is discarded.

(1) If a number is positive, the original code, reverse code, and complement code are the same.
(2) A number is negative, and the just-Signed bit is 1. The rest of you will reverse the original code, and then add 1 to the entire number.

-The original Code of 1 is 10000001.
-The Anti-Code of 1 is 11111110.
+ 1
-The complement of 1 is 11111111.
 
The original code of 0 is 00000000
The bitcode of 0 is 11111111 (the bitcode of positive and negative Zeros is the same)
+ 1
The completion code of 0 is 100000000 (1 of the headers are removed, and the completion codes of positive and negative zeros are the same)

 

The parameter of Integer. toHexString is int. If & 0xff is not performed, when a byte is converted to int, the byte is supplemented because the int Is 32 bits and the byte is only 8 bits,
For example, if the decimal number of the complement code 11111111 is-1 and the value is converted to an int, it will become more than 11111111111111111111111111111111! That is, 0xffffffff, but this number is incorrect. This complement will cause errors.
And 0xff, the 24-bit high will be cleared, and the result is correct.

 


How can c ++ convert a float byte into a byte [4] array and then convert byte [4] into an int in c #?

Float fexp = 0.1f;
Byte bAry [4];
Byte * lpAry = bAry;
* (Foat *) lpAry = fexp;

C #4-byte array to float

It is hard to understand the conversion from float to byte.
It would be reasonable to convert it to int and then convert it to byte.
This should be agreed upon by both parties.
For example, a = 12.5
12.5x10 = 125
125 to byte. It's easy for you to receive the conversion again.
Dividing the converted value by 10 is the original value.

The sender disconnects bytes for sending (high bytes -------> low bytes)
A = 125;
Byte [] bytes = new byte [4];
Bytes [0] = (byte) (a> 24 & 0xFF );
Bytes [1] = (byte) (a> 16 & 0xFF );
Bytes [2] = (byte) (a> 8 & 0xFF );
Bytes [3] = (byte) (a> 0 & 0xFF );
Receiver restructured Bytes:
Int B = 0x00;
B = B <24 | bytes [0];
B = B <16 | bytes [1];
B = B <8 | bytes [2];
B = B <0 | bytes [3];
(Here B = 125, and then divide it by 10 to 12.5)
You cannot use the shift operation for float conversion, and it is difficult for you to determine what the converted value is.
What you are giving is the C ++ operation pointer address. I personally think that other languages are not easy to parse. I will not say it right now, at least it is not universal.

Yes, that is, your communication, but you have to send it according to the protocol. If the sender understands that the receiver does not understand it, it is not called the protocol. No communication between you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.