4

アプリケーションから整数として提供される20バイトをバイナリ文字列に格納したいと思います。これが私にできる最善のことです。単純にするために2バイトのみ。

create table t (bs binary(2));

insert into t (bs) values
(concat(unhex(hex(240)), unhex(hex(40))))
;

select
    conv(hex(left(bs, 1)), 16, 10) n1,
    conv(hex(mid(bs, 2, 1)), 16, 10) n2
from t;
+------+------+
| n1   | n2   |
+------+------+
| 240  | 40   |
+------+------+

冗長性の低いものはありますか?そうでない場合、関数はそれらの変換をどのように行うのでしょうか?

4

1 に答える 1

0

整数をバイナリ文字列に連結する関数:

create function concat_integers_into_binary(
    i1 integer, i2 integer
) returns binary(2) deterministic
return concat(unhex(hex(i1)), unhex(hex(i2)))

insert into t (bs) values
(concat_integers_into_binary(240, 40))

バイナリ文字列から整数に変換する関数:

create function binary2integer(
    bs varbinary(20), position integer
) returns integer deterministic
return conv(hex(substring(bs, position, 1)), 16, 10)

select
    binary2integer(bs, 1) n1,
    binary2integer(bs, 2) n2
from t;
+------+------+
| n1   | n2   |
+------+------+
|  240 |   40 |
+------+------+
于 2012-12-13T14:16:34.053 に答える