[转帖]MySQL: Convert decimal to binary

mysql,convert,decimal,to,binary · 浏览次数 : 0

小编点评

**Conversion from Decimal to Binary** This article explains how to convert decimal numbers to binary format using various methods. **Key Concepts:** * **Precision** and **Scale:** These values determine the number of digits to retain in the binary representation. * **Memory Representation:** The binary representation is stored in a specific format, depending on the precision and scale. **Method 1: `decimal2bin()` Function** The `decimal2bin()` function takes a decimal number and converts it to a binary string. * It assumes that the input is a string in the format "value.scale". * The function uses string manipulation techniques to extract the precision and scale values. * It then uses bitwise operations to construct the binary representation. **Method 2: Conversion from Decimal to Binary Representation** * The article explains three methods for converting decimal numbers to binary: * Splitting the number into integer and fractional parts. * Storing the integer part in 4 bytes and the fractional part in the remaining space. * Inverting the highest bit for negative numbers. **Method 3: Memory Representation of Binary Numbers** * The binary representation is stored in a variable-length format. * The length of the representation depends on the precision and scale. * The binary representation is reversed for negative numbers. **Example:** | **Decimal Number** | **Binary Representation** | |---|---| | 1234567890.1234 | 0d-fb-38-d2 07-5a-ef-40 | **Note:** * The precision and scale values are typically passed as arguments to the function. * This method is suitable for converting decimal numbers to binary for both storage and transmission.

正文

 

Last Update:2018-12-05 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

与[转帖]MySQL: Convert decimal to binary相似的内容:

[转帖]MySQL: Convert decimal to binary

Last Update:2018-12-05 Source: Internet Author: User Tags decimal to binary mysql code Developer on Alibaba Coud: Build your first app with APIs, SDKs

[转帖]MySQL pid 和 socket 文件说明

2021-10-13 11:595110转载MySQL 1 pid-file文件 MySQL 中的 pid 文件记录的是当前 mysqld 进程的 pid ,pid 亦即 Process ID 。可以通过 pid-file 参数来配置 pid 文件路径及文件名,如果未指定此变量,则 pid 文件默认

[转帖]MySQL 慢查询日志深入理解

https://www.jb51.net/article/210312.htm + 目录 什么是慢查询日志 MySQL的慢查询日志是 MySQL提供的一种日志记录,它用来记录在 MySQL 中响应时间超过阀值的语句,具体指运行时间超过long_query_time 值的 SQL,则会被记录到慢查询日

[转帖]MySQL with Docker - Performance characteristics

https://dev.mysql.com/blog-archive/mysql-with-docker-performance-characteristics/ Docker presents new levels of portability and ease of use when it co

[转帖]MySQL Performance : Impact of InnoDB Transaction Isolation Modes in MySQL 5.7

http://dimitrik.free.fr/blog/archives/2015/02/mysql-performance-impact-of-innodb-transaction-isolation-modes-in-mysql-57.html There were so many valua

[转帖]MySQL Performance : IP port -vs- UNIX socket impact in 8.0 GA

http://dimitrik.free.fr/blog/posts/mysql-performance-80-ga-ip-port-vs-unix-socket-impact.html 2018-06-15 16:05 | MySQL, Performance, InnoDB, Benchmark

[转帖]MySQL Performance : XFS -vs- EXT4 Story

http://dimitrik.free.fr/blog/posts/mysql-80-perf-xfs-vs-ext4.html 2020-05-13 22:15 | MySQL, Performance, InnoDB, Benchmarks, DoubleWrite, XFS, EXT4 by

[转帖]MySQL Performance : 8.0 and UTF8 impact

http://dimitrik.free.fr/blog/posts/mysql-performance-80-and-utf8-impact.html 2018-04-26 00:58 | MySQL, Performance, UTF8 by Dimitri The world is movin

[转帖]MySQL十六:36张图理解Buffer Pool

https://www.cnblogs.com/yunlongn/p/16630257.html 转载~ 在应用系统中,我们为加速数据访问,会把高频的数据放在「缓存」(Redis、MongoDB)里,减轻数据库的压力。 在操作系统中,为了减少磁盘IO,引入了「缓冲池」(buffer pool)机制。

[转帖]MySQL提升笔记(4)InnoDB存储结构

https://cdn.modb.pro/u/310923 这一节本来计划开始索引的学习,但是在InnoDB存储引擎的索引里,存在一些数据存储结构的概念,这一节先了解一下InnodDB的逻辑存储结构,为索引的学习打好基础。 从InnoDB存储引擎的存储结构看,所有数据都被逻辑地放在一个空间中,称之为