MySQL: Convert decimal to binary

Source: Internet
Author: User
Tags decimal to binary mysql code

Conversion from MySQL code Note: decimal. c

Bytes ------------------------------------------------------------------------------------------------------------

*
Convert decimal to its binary fixed-length Representation
Two representations of the same length can be compared with memcmp
With the correct-1/0/+ 1 Result

Synopsis
Decimal2bin ()
From-value to convert
To-points to buffer Where string representation shoshould be stored
Precision/scale-see decimal_bin_size () below

Note
The buffer is assumed to be of the size decimal_bin_size (precision, scale)

Return Value
E_dec_ OK/e_dec_truncated/e_dec_overflow

Description
For storage decimal numbers are converted to the "binary" format.

This format has the following properties:
1. Length of the binary representation depends on the {precision, scale}
As provided by the caller and not on the intg/frac of the decimal
Convert.
2. Binary representations of the same {precision, scale} can be compared
With memcmp-with the same result as decimal_cmp () of the original
Decimals (not taking into account possible precision loss
Conversion ).

This binary format is as follows:
1. First the number is converted to have a requested precision and scale.
2. Every full dig_per_dec1 digits of intg part are stored in 4 bytes
As is
3. The first intg % dig_per_dec1 digits are stored in the specified CED
Number of bytes (enough bytes to store this number of digits-
See dig2bytes)
4. Same for frac-full decimal_digit_t's are stored as is,
The last frac % dig_per_dec1 digits-in the specified ced number of bytes.
5. If the number is negative-every byte is inversed.
5. The very first bit of the resulting byte array is inverted (because
Memcmp compares unsigned bytes, see property 2 above)

Example:

1234567890.1234

Internally is represented as 3 decimal_digit_t's

1 234567890 123400000

(Assuming we want a binary representation with precision = 14, scale = 4)
In hex it's

00-00-00-01 0d-fb-38-d2 07-5a-ef-40

Now, middle decimal_digit_t is full-It stores 9 decimal digits. It goes
Into binary representation as is:

...... 0d-fb-38-d2 ............

First decimal_digit_t has only one decimal digit. We can store one digit in
One byte, no need to waste four:

01 0d-fb-38-d2 ............

Now, last digit. It's 123400000. We can store 1234 in two bytes:

01 0d-fb-38-d2 04-d2

So, we 've packed 12 bytes number in 7 bytes.
And now we invert the highest bit to get the final result:

81 0d FB 38 D2 04 D2

And for-1234567890.1234 it wocould be

7E F2 04 37 2D FB 2D

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.