首页 > 数据库 > Oracle > 正文

ORACLE中实现ASCII字符串和16进制串互相转换

2024-08-29 13:49:39
字体:
来源:转载
供稿:网友
  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:=sTmptrim(to_char(ascii(substr(sIn,i,1)),'XXXX'));
end loop;
return sTmp;
END;  16进制串==>ASCII字符串  要区分字符集,以中文字符为例,对于数据库字符集是中文,要保证能够正确转回来,要有所不同。数据库字符集是中文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 i>length(sIn);
   x:=to_number(substr(sIn,i,2),'XXXX');
   if x>128 then
     sTmp:=sTmpchr(to_number(substr(sIn,i,4),'XXXX'));
     i:=i+4;
   else
     sTmp:=sTmpchr(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 i>length(sIn);
   sTmp:=sTmpchr(to_number(substr(sIn,i,2),'XXXX'));
   i:=i+2;
end loop;
return sTmp;
END;  测试,如下:SQL>select hextoasc(asctohex('你好ABC么')) from dual;
HEXTOASC(ASCTOHEX('你好ABC么'))
-----------------------------------------------------------
你好ABC么


上一篇:ORACLE UPDATE 语句语法与性能分析的一点看法

下一篇:ORACLE9i 表分析脚本

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
学习交流
热门图片

新闻热点

疑难解答

图片精选

网友关注