Base 64 decoding in Python and Oracle

江枫思渺然 提交于 2020-03-25 12:34:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!