[HOWTO] How to implement the ASC string and 16-string conversion in Oracle

Source: Internet
Author: User
Tags exit character set chr implement integer sin
oracle| Conversion | string
How to implement the ASC string and 16-string conversion in Oracle

Welcome everyone to communicate with me: Enhydraboy enhydra_boy@tom.com

Welcome to reprint, please keep this statement, thank you!

The following is the author's previous answer to the CSDN Forum on the summary of questions, I hope to be useful.

ASC string ==>16 into string

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 Binary string ==>ASC strings
To differentiate between character sets, take Chinese characters as an example, to make sure that the database character set is in Mandarin and to be able to turn back correctly.

The database character set is Chinese

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:=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;
/


Database character set is in English

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:=stmp| | Chr (To_number (substr (sin,i,2), ' XXXX '));
i:=i+2;
End Loop;
return STMP;
End;
/

Test, as follows:

Sql>select Hextoasc (asctohex (' Hello ABC ') from dual;
HEXTOASC (Asctohex (' take ABC) ")
--------------------------------------------------------------------------------
Hello ABC?






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.