问题
I am trying to replicate some python 2.7
code in Oracle
database.
When trying to replicate base64 decoding
, I am getting different output.
Python Code:
# -*- coding: utf-8 -*-
import base64
print(base64.b64decode('rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAgGLCRnyhEZhzJJRd7EoVPp2XWTRgDNYHq4cAghvXvTaw='))
Python Code Output:
�� ur [B���T� xp ���(Df�%{�O�e�M5���� ���M�
Oracle Query:
select utl_raw.cast_to_varchar2(UTL_ENCODE.BASE64_DECODE( UTL_RAW.CAST_TO_RAW ('rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAgGLCRnyhEZhzJJRd7EoVPp2XWTRgDNYHq4cAghvXvTaw='))) from dual;
Oracle Query Output:
�ur[B��T�xp �Df�{��M5����
The output seems to be partially same but not completely.
May be due to lack of knowledge, I am missing basics. Please suggest.
回答1:
Apparently your BASE64 string encodes some binary data (i.e. not text). In this case cast_to_varchar2
would be useless.
Try TO_BLOB(UTL_ENCODE.BASE64_DECODE(UTL_RAW.CAST_TO_RAW('rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAgGLCRnyhEZhzJJRd7EoVPp2XWTRgDNYHq4cAghvXvTaw=')))
回答2:
I have achieved the same using UTL_ENCODE.TEXT_DECODE
method and AL32UTF8
as encode_charset .
declare
v2 varchar2(4000 char);
begin
v2 := UTL_ENCODE.TEXT_DECODE(
'rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAgGLCRnyhEZhzJJRd7EoVPp2XWTRgDNYHq4cAghvXvTaw=',
'AL32UTF8',
UTL_ENCODE.BASE64);
DBMS_OUTPUT.PUT_LINE('v2='||v2);
end;
Output:
v2=��ur[B���T�xp ���(Df�%{�O�e�M5���� ���M�
来源:https://stackoverflow.com/questions/59750781/base-64-decoding-in-python-and-oracle