2012-12-12 3 views
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 | 
+------+------+ 

덜 자세한 내용이 있습니까? 함수가 그러한 변환을 어떻게하는 것일까? 바이너리 문자열로 정수를 연결하는

답변

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 | 
+------+------+ 
관련 문제