在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享ORACLE中如何实现ASCII字符串和16进制串互相转换,希望可以对大家能有小小的帮助。
【 tulaoshi.com - 编程语言 】
ASCII字符串==16进制串
create or replace function AscToHex(sIn IN varchar2)
RETURN varchar2
IS
sTmp varchar2(4000);
i integer;
BEGIN
i:=1;
stmp:='';
for i in 1..length(sIn) loop
sTmp:=sTmp||trim(to_char(ascii(substr(sIn,i,1)),'XXXX'));
end loop;
return sTmp;
END;
/
16进制串==ASCII字符串
要区分字符集,以中文字符为例,对于数据库字符集是中文,要保证能够正确转回来,要有所不同。
数据库字符集是中文
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)create or replace function HexToAsc(sIn IN varchar2)
RETURN varchar2
IS
sTmp varchar2(4000);
i integer;
x integer;
BEGIN
i:=1;
stmp:='';
loop
exit when ilength(sIn);
x:=to_number(substr(sIn,i,2),'XXXX');
if x128 then
sTmp:=sTmp||chr(to_number(substr(sIn,i,4),'XXXX'));
i:=i+4;
else
sTmp:=sTmp||chr(to_number(substr(sIn,i,2),'XXXX'));
i:=i+2;
end if;
end loop;
return sTmp;
END;
/
数据库字符集是英文
create or replace function HexToAsc(sIn IN varchar2)
RETURN varchar2
IS
sTmp varchar2(4000);
i integer;
BEGIN
i:=1;
stmp:='';
loop
exit when ilength(sIn);
sTmp:=sTmp||chr(to_number(substr(sIn,i,2),'XXXX'));
i:=i+2;
end loop;
return sTmp;
END;
/
测试,如下:
SQLselect hextoasc(asctohex('你好ABC么')) from dual;
HEXTOASC(ASCTOHEX('你好ABC么'))
------------------------------------------
你好ABC么
来源:http://www.tulaoshi.com/n/20160219/1621821.html
看过《ORACLE中如何实现ASCII字符串和16进制串互相转换》的人还看了以下文章 更多>>