Para codificar o decodificar a BASE64 utilizaremos las funciones ‘SCMS_BASE64_ENCODE_STR‘ o ‘SCMS_BASE64_DECODE_STR‘ respectivamente.
Función SCMS_BASE64_ENCODE_STR
Para codificar a BASE64 deberemos pasarle el contenido en una variable de tipo XSTRING y nos devolverá el contenido codificado en una variable de tipo STRING.
DATA: lv_base64_encode TYPE string,
lv_xstring TYPE xstring.
CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
input = lv_xstring
IMPORTING
output = lv_base64_encode.
Función SCMS_BASE64_DECODE_STR
Para decodificar BASE64 le pasaremos el contenido BASE64 en una variable de tipo STRING y nos devolverá el resultado decodificado en una variable de tipo XSTRING (que luego podemos convertir a lo que necesitemos).
DATA: lv_base64_encode TYPE string,
lv_base64_decode TYPE xstring.
CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
EXPORTING
input = lv_base64_encode
IMPORTING
output = lv_base64_decode
EXCEPTIONS
failed = 1
OTHERS = 2.
Flujo Completo Codificar y Decodificar
Ejemplo de flujo completo donde codificaremos y decodificaremos a BASE64 una variable de tipo STRING.
DATA: lv_origen TYPE string VALUE 'Hola Mundo',
lv_xstring TYPE xstring,
lv_base64_encode TYPE string,
lv_base64_decode TYPE xstring,
lv_resultado TYPE string.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_origen
IMPORTING
buffer = lv_xstring
EXCEPTIONS
failed = 1
OTHERS = 2.
CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
input = lv_xstring
IMPORTING
output = lv_base64_encode.
CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
EXPORTING
input = lv_base64_encode
IMPORTING
output = lv_base64_decode
EXCEPTIONS
failed = 1
OTHERS = 2.
CALL FUNCTION '/IRM/SO_CONV_XSTRING_TO_STRING'
EXPORTING
i_xstring = lv_base64_decode
IMPORTING
e_string = lv_resultado.
“El valor de lv_resultado será ‘Hola Mundo’
Para ver mas funciones de SAP pulse en el siguiente enlace: Listado de funciones