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

Convert HTTP_CLIENT_RECEIVE from UTF-16 to UTF-8

dimath72
Explorer
0 Likes
4,150

Dear Friends ,

I have made a program that call an API and the answer is in UTF-16 and I cannot handle it. I have searched many hours on the forum but I did;t managed to fix it.

Can you propose something?

CALL METHOD P_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
OTHERS = 4.

P_P_CONTENT = P_HTTP_CLIENT->RESPONSE->GET_CDATA( ).

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = P_P_CONTENT
IMPORTING
BUFFER = GV_XML_STRING
EXCEPTIONS
FAILED = 1
OTHERS = 2.


CALL FUNCTION 'SMUM_XML_PARSE'
EXPORTING
XML_INPUT = GV_XML_STRING
TABLES
XML_TABLE = GWA_XML_DATA

Look the result that I take from server

And look the headers that I sent ....

Thanks a lot .....

1 ACCEPTED SOLUTION
Read only

Ryan-Crosby
Active Contributor
4,002

Hi Dimitras,

You could try something like this for UTF-16LE (4102 for BE) - All parameters but one are mandatory so this is just a sample but you can tailor it however you want and see if it will work. Also, if this does not work there are some system -> external and external -> system format classes you can try: CL_ABAP_CONV_IN_CE, CL_ABAP_CONV_OUT_CE.

DATA: input TYPE xstring,
      output TYPE xstring,
      inused TYPE i,
      outused TYPE i,
      substed TYPE i,
      input_ends TYPE c,
      too_short TYPE c.
      

DATA(converter) = NEW cl_abap_conv_obj( incode = '4103' 
                                        outcode = '4110' ).
converter->convert( EXPORTING
                      inbuff             = input
                      outbufflg          = 10485760 "10MB buffer limit
                    IMPORTING
                      outbuff            = output
                      inused             = inused
                      outused            = outused
                      substed            = substed
                      input_ends_in_char = input_ends
                      outbuff_too_short  = too_short  ).

Regards,

Ryan Crosby

8 REPLIES 8
Read only

ThorstenHoefer
Active Contributor
4,002

Hi,

please check class cl_abap_conv_codepage, like

 cl_abap_conv_codepage=>create_in( CODEPAGE = 'UTF-16' )->convert( l_xstr ) 
Read only

0 Likes
4,002

Can you please provide an example ?

Read only

0 Likes
4,002

I am in version 740. I dont have it ...

Read only

Ryan-Crosby
Active Contributor
0 Likes
4,002

Hi Dimitras,

Is the UTF-16 big endian or little endian?

Regards,

Ryan Crosby

Read only

dimath72
Explorer
0 Likes
4,002

Actually, I don't know if it is Big or Little ....

Read only

Ryan-Crosby
Active Contributor
4,003

Hi Dimitras,

You could try something like this for UTF-16LE (4102 for BE) - All parameters but one are mandatory so this is just a sample but you can tailor it however you want and see if it will work. Also, if this does not work there are some system -> external and external -> system format classes you can try: CL_ABAP_CONV_IN_CE, CL_ABAP_CONV_OUT_CE.

DATA: input TYPE xstring,
      output TYPE xstring,
      inused TYPE i,
      outused TYPE i,
      substed TYPE i,
      input_ends TYPE c,
      too_short TYPE c.
      

DATA(converter) = NEW cl_abap_conv_obj( incode = '4103' 
                                        outcode = '4110' ).
converter->convert( EXPORTING
                      inbuff             = input
                      outbufflg          = 10485760 "10MB buffer limit
                    IMPORTING
                      outbuff            = output
                      inused             = inused
                      outused            = outused
                      substed            = substed
                      input_ends_in_char = input_ends
                      outbuff_too_short  = too_short  ).

Regards,

Ryan Crosby

Read only

4,002

Thanks a lot!

Correct answer.

You help me very much.

P.S)Thanks all for the help/

Read only

Sandra_Rossi
Active Contributor
0 Likes
4,002

It's UTF-16LE (little endian) because of order <# (where # is in fact the null byte in your case). If order was #< that would be big endian.

Note that if you define an XML transformation to process the XML, you would use GET_DATA instead of GET_CDATA, and you wouldn't need to care about the encoding/codepage.