Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Encode and Decode Base64

Former Member
0 Likes
30,481

Hi All,

I need to Encode and Decode Base64 File.

Please let me know if there is any Function Modules or Class Methods to achieve the requirement.

Thanks in advance.

Regards

Joseph

Message was edited by: Joseph Brown

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
16,110

Hi Joseph,

You can use this to decode a base64 string:

DATA: utility TYPE REF TO cl_http_utility.
      CREATE OBJECT utility.

      CALL METHOD utility->decode_base64
        EXPORTING
          encoded = mysapsso2
        RECEIVING
          decoded = cookie.

Regards,

Ravikiran.

PS: Pls assign points for helpful replies.

Encoding: To encode a string to Base64 format, you can first convert the string to Xstring using the function module SCMS_STRING_TO_XSTRING, and then use the function module SCMS_BASE64_ENCODE_STR to convert the Xstring to Base64.

Decoding: To decode a Base64 string, you can use a three-step procedure. First, convert the Base64 string to Xstring using the function module SCMS_BASE64_DECODE_STR. Next, transform the Xstring to Binary using the function module SCMS_XSTRING_TO_BINARY. Finally, turn the Binary format to String via the function module SCMS_BINARY_TO_STRING.

        12 REPLIES 12
        Read only

        Former Member
        0 Likes
        16,110

        I found two comment for you :

        1- Take a look this FM

        SCMS_BASE64_ENCODE

        HTTP_BASE64_ENCODE

        SSFC_BASE64_CODE

        2-Have look at this for Base64 encoding decoding sample check http://makcoder.sourceforge.net/demo/base64.php

        P.S. This knowledges quotationed.

        Read only

        Former Member
        0 Likes
        16,111

        Hi Joseph,

        You can use this to decode a base64 string:

        DATA: utility TYPE REF TO cl_http_utility.
              CREATE OBJECT utility.
        
              CALL METHOD utility->decode_base64
                EXPORTING
                  encoded = mysapsso2
                RECEIVING
                  decoded = cookie.

        Regards,

        Ravikiran.

        PS: Pls assign points for helpful replies.

        Read only

        0 Likes
        16,110

        Hi,

        This is the encoded base64 file in XML format:

        <?xml version="1.0" encoding="UTF-8"?>

        <Z_CREFO_DOC>

        <E_PDF><![CDATA[JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k

        ZT4+CnN0cmVhbQp4nE2MPQ/CMAxEd/8Kj/aQEKehIWvFh8QElTfEVNFOGUr7/0UagcTd8vSkuxmd

        FY9u6wGDLs4rRA1dhfvvCeYIaDbbZU8c9Dxk7LMGG50RGkWsHobExpj7F1NjhBzfCgG5tAR/Z0

        5pYMN6Qs9OJACxuhlY0n5Kde4aRwL/0A6e4hnWVuZHN0cmVhbQplbmRvYmoKNiAwIG9iagoxMzEK

        ZW5kb2JqCjQgMCBvYmoKPDwvVHlwZS9QYWdlL01lZGlhQm94IFswIDAgNTk1LjIyIDg0Ml0KL1Jv

        dGF0ZSAwL1BhcmVudCAzIDAgUgovUmVzb3VyY2VzPDwvUHJvY1NldFsvUERGIC9UZXh0XQovRXh0

        R1N0YXRlIDEwIDAgUgovRm9udCAxMSAwIFIKPj4KL0NvbnRlbnRzIDUgMCBSCj4+CmVuZG9iagoz

        IDAgb2JqCjw8IC9UeXBlIC9QYWdlcyAvS2lkcyBbCjQgMCBSCl0gL0NvdW50IDEKL1JvdGF0ZSAw

        Pj4KZW5kb2JqCjEgMCBvYmoKPDwvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMyAwIFIKPj4KZW5kb2Jq

        CjcgMCBvYmoKPDwvVHlwZS9FeHRHU3RhdGUKL09QTSAxPj5lbmRvYmoKMTAgMCBvYmoKPDwvUjcK

        NyAwIFIPgplbmRvYmoKMTEgMCBvYmoKPDwvUjkKOSAwIFIPgplbmRvYmoKMTIgMCBvYmoKPDwv

        U3VidHlwZS9UeXBlMUMvRmlsdGVyL0ZsYXRlRGVjb2RlL0xlbmd0aCAxMyAwIFI+PnN0cmVhbQp4

        nGWSbUxTVxiAz23h3jtX60bTGAKWm2w4FUjAwLRiMAZkP0bQsYIY2UaBK97Zr/UD2lGghVYoR0BB

        ELJSaEEbDfAj7llOmUiFnWbxs0Moxkm25FfRBN3ksuydaybPuxPyfvOe953yfneQ+BYkSIIAiJ

        itOyprQivVati5TASCTxTxm8TYubJ3ZVvsJlQwIF6PJWIsiRlKjNHGwaOX4dYGuPISEhPE9J0n

        uXqDzchVHzUzW4qLDm5NSUn97yRDqVQyFbZ/Mkwea+KqdczmSFDDavQGLaszZzO5kdsaDVfJVGts

        hqMmRl1VxVZFy0rUGvYYk89pOINBX8Nsyd3KbE9Pz0iLLNuzmUKLljXqUxlOd4TTcWYbo9ZVMfu1

        bLWa0aqr2GiDPC1nNtqYzHRO9291IaetsJiYtRczhXolU8AUsdUWjdr4/wxC6AU270C+yaxi0hDK

        R8koA72GVKgYZSECSdG6iEQUgyrRE0JF3BCViSZFq6I/xRugQ8ofcN4DXRCoSYJPhZ3yw3vKdW/i

        Ilw+rpu1TbtCnjANYbL5rnPcGNROvjN0EB/CZTaWK6sw5GElLSgeZAH1y8M5QJcUwgKkynsXPp4K

        4zAe1vdl0tKVI9hnneIXp6y+uGfLPAnERpn3GRBynNvYsOc4baVkGpgigcbPQ/M37s5N/YSX8M/a

        BdVXb9wTiElhE6ZlXiFgJR5TjfhvfRySC7TnOvtvvj0bh33hWKhGIWFhAW1lXs2FVWqnurMRPT

        lY2ng0lS/jr2gf8CXI2if1Guih6Cd6Fy/KzHm/bWbyAZ3oHb4VnAzjr/Gl+mnDeOXlzLG0CPCx

        4LWS3zf3OHEuvbqPwjuanXtctBVCPmp3h70ff0fzNTGypdGurlFImC97dduBQwJVW3i3az4Gxu

        HAQsqYj4NWN8qe8yaok4PdTDVm2zZmBaSwUiOnTw1qoBEEmKx37NoEdI2VIrvBK7OhZhu/vq

        8b419k7HmqYoO6ezvh8/oEEHydRj7LWf2UVLXinv26Izxhs8Md9OQ/KxG98C18I5eltkBirIP0

        eJpcbW0tON6Nne2OkxGb3RZL5wcJu4tKc98OVi4eTrr5/ie1XhPWxL93TF/Kafr8VkXNSFOgYZau

        I2UFQnof1dXT2X4Gn8b9noA70sA9NtoykPDDnZv3r5pC+yeTBHRH/5F9BI/GXzg39vnsBLc9oJDC

        Oec1XhIk7v8ohm6RL4ZaLKnrcOtcDW5m1xOVlVuK7F4XK78HHcesLT4aGvCzPkjonyuSvTw9dC

        iobeGuOHTguOP2IfvZ0E5/+gpDxgH//rJQK6lsXQtYLkq60Uft3h2Bn185mPUnbW90Vn0z1DAYn9

        toAgoodJNTEroZJt5ATO0wOAxHwQQympZGvD0h5DZxHnrkBdB2m5LWDvIlg6AaJCfWLb440S2R

        LA5I1iP0F03p+8AKZW5kc3RyZWFtCmVuZG9iagoxMyAwIG9iagoxMDk1CmVuZG9iago5IDAgb2Jq

        Cjw8L0Jhc2VGb250L1FaSEJGVCtUaW1lcy1Sb21hbi9Gb250RGVzY3JpcHRvciA4IDAgUi9UeXBl

        L0ZvbnQKL0ZpcnN0Q2hhciAzMi9MYXN0Q2hhciAxMTYvV2lkdGhzWwoyNTAgMCAwIDAgMCAwIDAg

        MCAwIDAgMCAwIDAgMzMzIDAgMAowIDAgMCAwIDAgMCAwIDAgMCAwIDAgMCAwIDAgMCAwCjAgMCAw

        IDAgNzIyIDAgNTU2IDAgMCAwIDAgMCAwIDAgMCAwCjU1NiAwIDAgMCA2MTEgMCAwIDAgMCAwIDAg

        MCAwIDAgMCAwCjAgMCAwIDAgMCA0NDQgMCAwIDAgMCAwIDAgMCAwIDAgMAowIDAgMCAzODkgMjc4

        XQovRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nL1N1YnR5cGUvVHlwZTE+PgplbmRvYmoKOCAwIG9i

        ago8PC9UeXBlL0ZvbnREZXNjcmlwdG9yL0ZvbnROYW1lL1FaSEJGVCtUaW1lcy1Sb21hbi9Gb250

        QkJveFswIC0xMCA2ODUgNjYyXS9GbGFncyA0Ci9Bc2NlbnQgNjYyCi9DYXBIZWlnaHQgNjYyCi9E

        ZXNjZW50IC0xMAovSXRhbGljQW5nbGUgMAovU3RlbVYgMTAyCi9NaXNzaW5nV2lkdGggMjUwCi9D

        aGFyU2V0KC9lL0QvUC9GL3MvdC9UL3NwYWNlL2h5cGhlbikvRm9udEZpbGUzIDEyIDAgUj4+CmVu

        ZG9iagoyIDAgb2JqCjw8L1Byb2R1Y2VyKEdQTCBHaG9zdHNjcmlwdCA4LjE1KQovQ3JlYXRpb25E

        YXRlKEQ6MjAwNjAzMjExMjExMTYpCi9Nb2REYXRlKEQ6MjAwNjAzMjExMjExMTYpCi9UaXRsZShN

        aWNyb3NvZnQgV29yZCAtIERva3VtZW50MSkKL0NyZWF0b3IoUFNjcmlwdDUuZGxsIFZlcnNpb24g

        NS4yKQovQXV0aG9yKG11ZWxsZXJoKT4+ZW5kb2JqCnhyZWYKMCAxNAowMDAwMDAwMDAwIDY1NTM1

        IGYgCjAwMDAwMDA0NjYgMDAwMDAgbiAKMDAwMDAwMjM5OSAwMDAwMCBuIAowMDAwMDAwMzk4IDAw

        MDAwIG4gCjAwMDAwMDAyMzUgMDAwMDAgbiAKMDAwMDAwMDAxNSAwMDAwMCBuIAowMDAwMDAwMjE2

        IDAwMDAwIG4gCjAwMDAwMDA1MTQgMDAwMDAgbiAKMDAwMDAwMjE2MCAwMDAwMCBuIAowMDAwMDAx

        ODE3IDAwMDAwIG4gCjAwMDAwMDA1NTUgMDAwMDAgbiAKMDAwMDAwMDU4NSAwMDAwMCBuIAowMDAw

        MDAwNjE1IDAwMDAwIG4gCjAwMDAwMDE3OTYgMDAwMDAgbiAKdHJhaWxlcgo8PCAvU2l6ZSAxNCAv

        Um9vdCAxIDAgUiAvSW5mbyAyIDAgUgovSUQgWyityXEclVXSeoKdpGR5FFwppSkorclxHJVV0nqC

        naRkeRRcKaUpXQo+PgpzdGFydHhyZWYKMjU5NgolJUVPRgo=]]>

        </E_PDF>

        <C_ERROR></C_ERROR>

        </Z_CREFO_DOC>

        Can i pass all these characters or i need to transform it first, extract the value of the element and decode? Finally i need to write the decoded outcome to a file (a pdf file) to the hard drive (may be with: OPEN DATASET P_DATASET FOR INPUT IN TEXT MODE ENCODING DEFAULT)

        **************************************************

        I saw this hint but I think there should be more to it (eg. transformation of the initial decoded xml file and the writing it to hard drive using the Open Dataset for Input...):

        ****************being of report *************************

        DATA: z TYPE string,

        encode TYPE string,

        decode TYPE string .

        DATA: obj TYPE REF TO cl_http_utility,

        cref TYPE REF TO if_http_utility.

        CREATE OBJECT : obj.

        cref = obj.

        z = 'what u have to encode'.

        encode = obj->if_http_utility~encode_base64( z ) .

        decode = obj->if_http_utility~decode_base64( encode ).

        ****************end of report ***************************

        What do i need more to the above report

        Read only

        0 Likes
        16,110

        you need to parse the xml and take the contents betwee <E_PDF> and </E_PDF> and pass the same to the method.

        by the way the decode method is a static method so you dont need the create object call.

        CALL METHOD cl_http_utility=>IF_HTTP_UTILITY~DECODE_BASE64

        EXPORTING

        encoded = encoded

        RECEIVING

        decoded = decoded.

        Regards

        Raja

        Read only

        0 Likes
        16,110

        Hi,

        I passed the xml through DOM and got the value for the pdf which corresponds to the characters from 'J' to '='.

        I then applied SM69 to wrap up my Java Application and passed the value and the pdf filename as parameters; this architecture worked for me.

        Seems the IF_HTTP_UTILITY class works when the xml file is encoded using:

        encode = obj->if_http_utility~encode_base64( z ) .

        and decoded using:

        decode = obj->if_http_utility~decode_base64( encode ).

        Regards,

        Jospeh

        Read only

        0 Likes
        16,110

        Hi i have the inverse situation.

        i have a file in pdf format and i need to convert in Base64.

        can anyone help me?

        SSFC_BASE64_CODE how this FM works?

        i tried on SE37 but i did not understand.

        Read only

        0 Likes
        16,110

        hello Durai,

        After I decode as said by you, how do I archive it in SAP against an invoice?. I dont want a file to be archived by using the file path, instead I would want to directly archive the decoded message.

        Best regards

        Read only

        0 Likes
        16,110

        HI Durai,

                       Am able to encode my value using your code, when am trying to decode the encoded value am not getting correct value,  please check my code and  help me to solve this .

        i_part = ''2000002.

            CALL METHOD cl_http_utility=>if_http_utility~decode_base64

              EXPORTING

                encoded = i_part

             RECEIVING

               decoded = i_part1.

        after encoding my i_part value is

        i_part1 = ÓM4ÓM4.

        decoding my encoded value

        CALL METHOD cl_http_utility=>if_http_utility~encode_base64

               EXPORTING

                 unencoded = i_part1

               RECEIVING

                 encoded   = i_part2.


        i_part2 = 000000.




        Read only

        Former Member
        0 Likes
        16,110

        This message was moderated.

        Read only

        0 Likes
        16,110

        Prepare new thread.

        Regards,

        Abbas.

        Read only

        alagon
        Explorer
        0 Likes
        16,110

        Encoding: To encode a string to Base64 format, you can first convert the string to Xstring using the function module SCMS_STRING_TO_XSTRING, and then use the function module SCMS_BASE64_ENCODE_STR to convert the Xstring to Base64.

        Decoding: To decode a Base64 string, you can use a three-step procedure. First, convert the Base64 string to Xstring using the function module SCMS_BASE64_DECODE_STR. Next, transform the Xstring to Binary using the function module SCMS_XSTRING_TO_BINARY. Finally, turn the Binary format to String via the function module SCMS_BINARY_TO_STRING.

              Read only

              Sandra_Rossi
              Active Contributor
              0 Likes
              16,110

              Quite complex solution. You can do it in one call only, for decode and encode. See other places in forum.