oracle 将十六进制转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19495308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:01:31  来源:igfitidea点击:

Convert hex to string

oracleplsql

提问by frgtv10

I want to use HEXTORAW()to get the char value from the ASCII HEX code '30'. ASCII HEX 30 should return varchar '0'.

我想用来HEXTORAW()从 ASCII 十六进制代码“30”中获取字符值。ASCII HEX 30 应返回 varchar '0'。

How to to that? Is HEXTORAW()the right function?

那怎么办?是HEXTORAW()正确的功能吗?

回答by Nick Krasnov

You could use utl_rawpackage and cast_to_varchar2()function specifically:

您可以专门使用utl_raw包和cast_to_varchar2()函数:

select utl_raw.cast_to_varchar2(hextoraw('30')) as res
  from dual

result:

结果:

RES
-----
0

回答by Vincent Malgrat

You can also use CHR(for single characters):

您还可以使用CHR(对于单个字符):

SQL> select chr(to_number('30', 'XX')) from dual;

CHR(TO_NUMBER('30','XX'))
-------------------------
0

回答by Maarten Malaise

I had some difficulties using characters from the extended ascii range (ISO Latin?1 ; ISO-8859?1). Solved it like this:

我在使用扩展的 ascii 范围(ISO Latin?1 ; ISO-8859?1)中的字符时遇到了一些困难。是这样解决的:

select chr(to_number('D6','xx') using NCHAR_CS) from dual

where hex value 'D6' matches (extended) ascii value 214 or character '?'

其中十六进制值 'D6' 匹配(扩展)ascii 值 214 或字符 '?'

or in a function

或在函数中

create or replace function hex_to_ascii(TEXT_IN varchar2) 
RETURN varchar2
as
  TEXT_OUT varchar2(200);
  TEXT_MAN varchar2(200):=TEXT_IN;
BEGIN
  while length(TEXT_MAN)>0
  LOOP
    TEXT_OUT:=TEXT_OUT||chr(to_number(substr(TEXT_MAN,0,2),'xx') using NCHAR_CS);
    TEXT_MAN:=substr(TEXT_MAN,3);
  END LOOP;
  return TEXT_OUT; 
END;

The function can be used like this:

该函数可以这样使用:

select hex_to_ascii('30D6D0D1C7DC') from dual

The input hex string '30D6D0D1C7DC' will be converted to '0?D??ü'

输入的十六进制字符串 '30D6D0D1C7DC' 将被转换为 '0?D??ü'

I hope that this can be of use to others

我希望这对其他人有用