---
title: "Oracle基本数据类型存储格式研究（二）—数字类型"
author: "libin123"
date: "2010-12-14T00:35:16Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2010-12-14T20:31:12Z"
id: 841337
url: "https://dbua.iteye.com/blog/841337"
---

数字类型包含number,integer,float...oracle内部标识为2

数字类型在oracle内部是以单字节的数字为存储的变长数组

数字类型在oracle文件中的存储格式为：

类型 &lt;\[长度]&gt;，符号/指数位 \[数字1，数字2，数字3，......，数字20]

各位的含义如下:

1.类型: Number型,Type=2 (类型代码可以从Oracle的文档上查到)

2.长度:指存储的字节数

3.符号/指数位

在存储上,Oracle对正数和负数分别进行存储转换:

正数：加1存储(为了避免Null)  
负数：被101减,如果总长度小于21个字节，最后加一个102(是为了排序的需要)

指数位换算:

If the first byte is greater than or equal to 128, then the number is positive and the  
exponent is:  
exponent = first byte - 128 - 65 = first byte - 193  
If the first byte is less than 128, then the number is negative and the exponent is:  
exponent = (255 - first byte) - 128 - 65 = 62 - first byte

4.从&lt;数字1&gt;开始是有效的数据位

从&lt;数字1&gt;开始是最高有效位,所存储的数值计算方法为：

将下面计算的结果加起来：

每个&lt;数字位&gt;乘以100^(指数-N) (N是有效位数的顺序位，第一个有效位的N=0)

5. 正数计算举例：

 

```
SYS@huiche>select dump(123456.783,16) from dual;

DUMP(123456.783,16)
----------------------------------------------------------
Typ=2 Len=6: c3,d,23,39,4f,1f

SYS@huiche>
<指数>:   195 - 193 = 2 
• 数字
0xd = 13(dec) -1 = 12 > 12 * 100^2 = 120000
0x23 = 35(dec) -1 = 34 > 34 * 100^1 = 3400
0x39 = 57(dec) -1 = 56 > 56 * 100^0 = 56
0x4f = 79(dec) -1 = 78 > 78 * 100^-1 = .78
0x5b = 31(dec) -1 = 30 > 90 * 100^-2 = .003
                                                      sum = 123456.783
 因为第一个字节(0xc3)大于128, 所以为正数
```

 6.负数计算举例：

 

```
SYS@huiche>select dump(-123456.783,16) from dual;

DUMP(-123456.783,16)
------------------------------------------------------------------
Typ=2 Len=7: 3c,59,43,2d,17,47,66

SYS@huiche>
指数 => 0x3c= 62(dec) - 60 = 2
• 数字
0x59 = 89(dec): 101 - 89 = 12 > 12 * 100^2 = 120000
0x43 = 67(dec): 101 - 67 = 34 > 34 * 100^1 = 3400
0x2d = 45(dec): 101 - 45 = 56 > 56 * 100^0 = 56
0x17 = 23(dec): 101 - 23 = 78 > 78 * 100^-1 = .78
0xb = 11(dec): 101 - 71 = 30 > 30 * 100^-2 = .003
                                                         sum = 123456.783 (-)
• 最后一个字节不计算在内 0x66 = 102(dec) ,第一个字节(0x3c)小于128, 所以为负数
```

 7.0的存储数字是80

 

```
SYS@huiche>select dump(0,16) from dual;

DUMP(0,16)
------------------------------
Typ=2 Len=1: 80

SYS@huiche>
```