Last Update:2018-12-05 Source: Internet 
Author: User
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more >

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

This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or

[转帖]MySQL: Convert decimal to binary的更多相关文章

  1. convert decimal to binary

    public class Solution { public static void main(String[] args) { ; String str = ""; ) { ; ...

  2. convert decimal to binary using inbuilt function

    package testpacknm; import java.util.Scanner; public class testcnm { public static void main(String[ ...

  3. Write a program to convert decimal to 32-bit unsigned binary.

    Write a program to convert decimal to 32-bit unsigned binary. Write a program to convert a 32-bit un ...

  4. MySQL中 DECIMAL FLOAT DOUBLE的区别

    第一篇文章: MySQL中Decimal类型和Float Double等区别 MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,d ...

  5. mysql 由decimal 引起的 Warning: Data truncated for column

    今天在使用python 库mysqldb的rawsql的时候遇到一个问题(其实并不是mysqlbean引起的) cls.raw_sql('update {table} set available_am ...

  6. [Algorithm] Convert a number from decimal to binary

    125, how to conver to binary number? function DecimalToDinary (n) { let temp = n; let list = []; if ...

  7. MySQL中Decimal类型和Float Double的区别 & BigDecimal与Double使用场景

    MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,double等非标准类型,在DB中保存的是近似值,而Decimal则以字符串的形 ...

  8. MySQL数据类型-decimal详解

    from:http://www.linuxidc.com/Linux/2013-07/88032.htm 1.首先,对于精度比较高的东西,比如money,我会用decimal类型,不会考虑float, ...

  9. Convert a given Binary Tree to Doubly Linked List

    The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...

  10. Convert string to binary and binary to string in C#

    String to binary method: public static string StringToBinary(string data) { StringBuilder sb = new S ...

随机推荐

  1. CSS3学习笔记-盒模型

    CSS盒模型是指包含内容(content).填充(padding).边框(border)和外边距(margin)几个方面的一个矩形框模型. 内容区(content):指元素中显示内容的区域,它的大小由 ...

  2. 将一个服务器通过HTTP请求另一个服务器

    1.一个服务器访问另一个服务器(不传参) @RequestMapping ("/test") public ResponseEntity<String> serverT ...

  3. 获取yml自定义内容的方式

    yml内容 yml: login: name: zhangsan age: 18 pass: 123456 方式一: 创建实体类 @Configuration @ConfigurationProper ...

  4. nacos 系列:.net core 6.0 使用 nacos

    目录 安装 Nuget 包 配置 appsettings.json 添加服务和配置 测试 官方示例:https://kgithub.com/nacos-group/nacos-sdk-csharp 安 ...

  5. 小熊派开发实践丨漫谈LiteOS之传感器移植

    摘要:本文基于小熊派开发板简单介绍了如何在LiteOS中移植传感器,从而实现对于传感器的相关控制. 1 hello world 相信大家无论在学习编程语言开始的第一个函数应该是HelloWorld,本 ...

  6. 如何给MindSpore添加一个新的硬件后端?快速构建测试环境!

    摘要:介绍如何给MindSpore添加一个新的硬件后端. 本文分享自华为云社区<如何给MindSpore添加一个新的硬件后端?快速构建测试环境!>,原文作者:HWCloudAI. Mind ...

  7. HTTP请求转发那些事:你可能不知道的Hop-by-hop Headers和End-to-end Headers

    摘要:不是所有的web容器都能帮助开发者屏蔽hop-by-hop headers,有些容器反而允许开发者自定义hop-by-hop headers来实现更大程度的灵活性. 本文分享自华为云社区< ...

  8. Linux 创建新用户

    添加用户组[root@VipSoft ~]#groupadd admin 添加用户[root@VipSoft ~]#useradd jimmy -m -d /home/jimmy -g admin 修 ...

  9. UltraEdit 去除文本中的空行,按指定字符换行

    在将JSON格式的数据,整理到 Excel中查看时,可以通过文本替换的方式将JSON存到csv 后,使用 UltraEdit 编辑工具按需进行替换处理 去除多个空行 ^p^p  替换成 ^p 按逗号换 ...

  10. Java 网络编程 —— 基于 UDP 的数据报和套接字

    UDP 简介 UDP(User Datagram Protocol,用户数据报协议)是传输层的另一种协议,比 TCP 具有更快的传输速度,但是不可靠.UDP 发送的数据单元被称为 UDP 数据报,当网 ...