简体   繁体   中英

How to remove '\r\n' from Base64 string in oracle?

I have created the following function in Oracle to convert my string to BASE64 and I am getting a new line after 64 characters . The function is as below

function to_base64(t in varchar2) return varchar2 is
begin
  return utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(t)));
end to_base64; 

My input is tobase64('ACae23b41949dbcfa8cd2c8b114bb81af4:909f2edb3e9b8c7e9c545d2942aa8576')

and output I am getting is as follows where new line is added after 64 characters. QUNhZTIzYjQxOTQ5ZGJjZmE4Y2QyYzhiMTE0YmI4MWFmNDo5MDlmMmVkYjNlOWI4 YzdlOWM1NDVkMjk0MmFhODU3Ng==

Can someone suggest how can I remove the new line or any change required in the function?

You can use replace() to remove the two characters individually:

function to_base64(t in varchar2) return varchar2 is
begin
  return replace(
    replace(
      utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(t))),
      chr(10)),
    chr(13)
  );
end to_base64; 

or as a pair:

function to_base64(t in varchar2) return varchar2 is
begin
  return replace(
    utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(t))),
    chr(13)||chr(10)
  );
end to_base64; 

and then either way

select to_base64('ACae23b41949dbcfa8cd2c8b114bb81af4:909f2edb3e9b8c7e9c545d2942aa8576')
from dual;

gives the result on a single line:

QUNhZTIzYjQxOTQ5ZGJjZmE4Y2QyYzhiMTE0YmI4MWFmNDo5MDlmMmVkYjNlOWI4YzdlOWM1NDVkMjk0MmFhODU3Ng==

But it's common for base64 values to include them, and decoding should ignore them anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM