encoding-functions.md 5.8 KB
Newer Older
1 2
---
toc_priority: 52
3
toc_title: "Функции кодирования"
4 5
---

6
# Функции кодирования {#funktsii-kodirovaniia}
7

S
Fixes  
Sergei Bocharov 已提交
8 9
## char {#char}

A
alexey-milovidov 已提交
10
Возвращает строку, длина которой равна числу переданных аргументов, и каждый байт имеет значение соответствующего аргумента. Принимает несколько числовых аргументов. Если значение аргумента выходит за диапазон UInt8 (0..255), то оно преобразуется в UInt8 с возможным округлением и переполнением.
S
Fixes  
Sergei Bocharov 已提交
11 12 13

**Синтаксис**

14
``` sql
S
Fixes  
Sergei Bocharov 已提交
15 16 17 18 19
char(number_1, [number_2, ..., number_n]);
```

**Параметры**

20
-   `number_1, number_2, ..., number_n` — Числовые аргументы, которые интерпретируются как целые числа. Типы: [Int](../../sql-reference/functions/encoding-functions.md), [Float](../../sql-reference/functions/encoding-functions.md).
S
Fixes  
Sergei Bocharov 已提交
21 22 23

**Возвращаемое значение**

24
-   строка из соответствующих байт.
S
Fixes  
Sergei Bocharov 已提交
25 26 27 28 29 30 31

Тип: `String`.

**Пример**

Запрос:

32
``` sql
S
Fixes  
Sergei Bocharov 已提交
33 34 35 36 37
SELECT char(104.1, 101, 108.9, 108.9, 111) AS hello
```

Ответ:

38
``` text
S
Fixes  
Sergei Bocharov 已提交
39 40 41 42 43
┌─hello─┐
│ hello │
└───────┘
```

A
alexey-milovidov 已提交
44 45
Вы можете создать строку в произвольной кодировке, передав соответствующие байты. Пример для UTF-8:

S
Fixes  
Sergei Bocharov 已提交
46
Запрос:
47 48

``` sql
S
Fixes  
Sergei Bocharov 已提交
49 50 51 52 53
SELECT char(0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82) AS hello;
```

Ответ:

54
``` text
S
Fixes  
Sergei Bocharov 已提交
55 56 57 58 59 60 61
┌─hello──┐
│ привет │
└────────┘
```

Запрос:

62
``` sql
S
Fixes  
Sergei Bocharov 已提交
63 64 65 66 67
SELECT char(0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD) AS hello;
```

Ответ:

68
``` text
S
Fixes  
Sergei Bocharov 已提交
69 70 71 72 73
┌─hello─┐
│ 你好  │
└───────┘
```

S
Fixes  
Sergei Bocharov 已提交
74
## hex {#hex}
75

76
Returns a string containing the argument’s hexadecimal representation.
S
Fixes  
Sergei Bocharov 已提交
77

G
George 已提交
78 79
Синоним: `HEX`.

S
Fixes  
Sergei Bocharov 已提交
80 81
**Syntax**

82
``` sql
S
Fixes  
Sergei Bocharov 已提交
83 84 85
hex(arg)
```

A
alexey-milovidov 已提交
86
The function is using uppercase letters `A-F` and not using any prefixes (like `0x`) or suffixes (like `h`).
S
Fixes  
Sergei Bocharov 已提交
87

88
For integer arguments, it prints hex digits («nibbles») from the most significant to least significant (big endian or «human readable» order). It starts with the most significant non-zero byte (leading zero bytes are omitted) but always prints both digits of every byte even if leading digit is zero.
S
Fixes  
Sergei Bocharov 已提交
89

A
alexey-milovidov 已提交
90
Example:
S
Fixes  
Sergei Bocharov 已提交
91

A
alexey-milovidov 已提交
92
**Example**
S
Fixes  
Sergei Bocharov 已提交
93

A
alexey-milovidov 已提交
94
Query:
S
Fixes  
Sergei Bocharov 已提交
95

96
``` sql
A
alexey-milovidov 已提交
97 98
SELECT hex(1);
```
S
Fixes  
Sergei Bocharov 已提交
99

A
alexey-milovidov 已提交
100
Result:
S
Fixes  
Sergei Bocharov 已提交
101

102
``` text
A
alexey-milovidov 已提交
103 104
01
```
S
Fixes  
Sergei Bocharov 已提交
105

A
alexey-milovidov 已提交
106
Values of type `Date` and `DateTime` are formatted as corresponding integers (the number of days since Epoch for Date and the value of Unix Timestamp for DateTime).
S
Fixes  
Sergei Bocharov 已提交
107

A
alexey-milovidov 已提交
108
For `String` and `FixedString`, all bytes are simply encoded as two hexadecimal numbers. Zero bytes are not omitted.
S
Fixes  
Sergei Bocharov 已提交
109

A
alexey-milovidov 已提交
110
Values of floating point and Decimal types are encoded as their representation in memory. As we support little endian architecture, they are encoded in little endian. Zero leading/trailing bytes are not omitted.
S
Fixes  
Sergei Bocharov 已提交
111

A
alexey-milovidov 已提交
112 113
**Parameters**

114
-   `arg` — A value to convert to hexadecimal. Types: [String](../../sql-reference/functions/encoding-functions.md), [UInt](../../sql-reference/functions/encoding-functions.md), [Float](../../sql-reference/functions/encoding-functions.md), [Decimal](../../sql-reference/functions/encoding-functions.md), [Date](../../sql-reference/functions/encoding-functions.md) or [DateTime](../../sql-reference/functions/encoding-functions.md).
A
alexey-milovidov 已提交
115 116 117

**Returned value**

118
-   A string with the hexadecimal representation of the argument.
A
alexey-milovidov 已提交
119 120 121 122 123 124

Type: `String`.

**Example**

Query:
S
Fixes  
Sergei Bocharov 已提交
125

126
``` sql
S
Fixes  
Sergei Bocharov 已提交
127 128 129
SELECT hex(toFloat32(number)) as hex_presentation FROM numbers(15, 2);
```

A
alexey-milovidov 已提交
130
Result:
S
Fixes  
Sergei Bocharov 已提交
131

132
``` text
S
Fixes  
Sergei Bocharov 已提交
133 134 135 136 137 138
┌─hex_presentation─┐
│ 00007041         │
│ 00008041         │
└──────────────────┘
```

A
alexey-milovidov 已提交
139
Query:
S
Fixes  
Sergei Bocharov 已提交
140

141
``` sql
S
Fixes  
Sergei Bocharov 已提交
142 143 144
SELECT hex(toFloat64(number)) as hex_presentation FROM numbers(15, 2);
```

A
alexey-milovidov 已提交
145
Result:
S
Fixes  
Sergei Bocharov 已提交
146

147
``` text
S
Fixes  
Sergei Bocharov 已提交
148 149 150 151 152
┌─hex_presentation─┐
│ 0000000000002E40 │
│ 0000000000003040 │
└──────────────────┘
```
153

154 155 156 157
## unhex(str) {#unhexstr}

Accepts a string containing any number of hexadecimal digits, and returns a string containing the corresponding bytes. Supports both uppercase and lowercase letters A-F. The number of hexadecimal digits does not have to be even. If it is odd, the last digit is interpreted as the least significant half of the 00-0F byte. If the argument string contains anything other than hexadecimal digits, some implementation-defined result is returned (an exception isn’t thrown).
If you want to convert the result to a number, you can use the ‘reverse’ and ‘reinterpretAsType’ functions.
A
alexey-milovidov 已提交
158

159
## UUIDStringToNum(str) {#uuidstringtonumstr}
160

161
Принимает строку, содержащую 36 символов в формате `123e4567-e89b-12d3-a456-426655440000`, и возвращает в виде набора байт в FixedString(16).
162

163 164
## UUIDNumToString(str) {#uuidnumtostringstr}

165 166
Принимает значение типа FixedString(16). Возвращает строку из 36 символов в текстовом виде.

167 168
## bitmaskToList(num) {#bitmasktolistnum}

169 170
Принимает целое число. Возвращает строку, содержащую список степеней двойки, в сумме дающих исходное число; по возрастанию, в текстовом виде, через запятую, без пробелов.

171 172
## bitmaskToArray(num) {#bitmasktoarraynum}

173
Принимает целое число. Возвращает массив чисел типа UInt64, содержащий степени двойки, в сумме дающих исходное число; числа в массиве идут по возрастанию.
I
Ivan Blinkov 已提交
174