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: 

Convert binary data in string into text in coding utf-8

alex_klim
Explorer
0 Kudos
3,935

Hello everybody, i have got ls_data type string with data(bit string) inside this structure - 11010000101000001101000010110101110100011000000111010000101111111101000110000011110100001011000111010000101110111101000010111000110100001011101011010000101100001101000010111101110100011000000111010000101110101101000010111000110100001011100100100000110100011000001111010000101101001101000010111110110100011000000111010001100000101101000010111110110100001011001011010000101101011101000110000000110100011000111111010001100011101101000110001001110100001011100011010000101110010010000011010001100001101101000010110101110100001011110111010001100000101101000110000000001000001101000010010011110100001011111011010001100000011101000010100001110100001010001111010000100111101101000010011010

And i need to convert this data in this structure in readable text in coding utf-8?

Is there in abap any way to do this?

I tried cl_abap_conv_in_ce=>create and cl_http_utility but no success

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos
3,732

I wonder what is the business case of that...

The first part of your question is about how to convert from base 2 (0 and 1) into bytes.

In your ABAP program, use SET BIT. The bit number must correspond to an existing byte, so you must initialize the xstring variable with enough bytes. Then it's simple (not tested):

REPORT.
DATA(bit_string_8_bytes) = '1101000010100000110100001011010111010001100000011101000010111110'.
DATA(xstring) = VALUE xstring( ).

PERFORM bit_string_to_x USING bit_string_8_bytes CHANGING xstring.

ASSERT xstring = 'D0A0D0B5D181D0BE'.


FORM bit_string_to_x USING bit_string_8_bytes TYPE csequence CHANGING xstring TYPE xstring.

  DATA(number_of_bytes) = ( strlen( bit_string_8_bytes ) + 7 ) DIV 8.
  xstring = VALUE #( ).
  SHIFT xstring RIGHT BY number_of_bytes PLACES IN BYTE MODE.
  DATA(bit_number) = 0.
  DO strlen( bit_string_8_bytes ) TIMES.
    bit_number = bit_number + 1.
    DATA(bit_value) = CONV i( substring( val = bit_string_8_bytes off = bit_number - 1 len = 1 ) ).
    SET BIT bit_number OF xstring TO bit_value.
  ENDDO.

ENDFORM.
13 REPLIES 13

Ryan-Crosby
Active Contributor
0 Kudos
3,732

Your string is in bits so you would need to convert it to xstring first.

Sandra_Rossi
Active Contributor
0 Kudos
3,733

I wonder what is the business case of that...

The first part of your question is about how to convert from base 2 (0 and 1) into bytes.

In your ABAP program, use SET BIT. The bit number must correspond to an existing byte, so you must initialize the xstring variable with enough bytes. Then it's simple (not tested):

REPORT.
DATA(bit_string_8_bytes) = '1101000010100000110100001011010111010001100000011101000010111110'.
DATA(xstring) = VALUE xstring( ).

PERFORM bit_string_to_x USING bit_string_8_bytes CHANGING xstring.

ASSERT xstring = 'D0A0D0B5D181D0BE'.


FORM bit_string_to_x USING bit_string_8_bytes TYPE csequence CHANGING xstring TYPE xstring.

  DATA(number_of_bytes) = ( strlen( bit_string_8_bytes ) + 7 ) DIV 8.
  xstring = VALUE #( ).
  SHIFT xstring RIGHT BY number_of_bytes PLACES IN BYTE MODE.
  DATA(bit_number) = 0.
  DO strlen( bit_string_8_bytes ) TIMES.
    bit_number = bit_number + 1.
    DATA(bit_value) = CONV i( substring( val = bit_string_8_bytes off = bit_number - 1 len = 1 ) ).
    SET BIT bit_number OF xstring TO bit_value.
  ENDDO.

ENDFORM.

0 Kudos
3,732

Thank you, i try to test it, but result is not good i think:

3,732

LOL If the code does SET BIT ... TO 1 for each bit, of course all bits are set to 1. Did you even try to understand what SET BIT is? And to understand the code I have posted?

Fixed:

replace

SET BIT ... TO 1

with

SET BIT ... TO bit_value.

0 Kudos
3,732

sandra.rossi sorry)

0 Kudos
3,732

Great sandra.rossi, big thanks to you everything is ok now)

3,732

Thanks for the feedback. I just tested, it works well. I have improved the answer to make it more understandable and reusable.

0 Kudos
3,732

sandra.rossi Thank you very much one more time, you help me a lot)

0 Kudos
3,732

EDIT: optimization done to initialize XSTRING with a given number of bytes (SHIFT instead of weird REPLACE '00').

matt
Active Contributor
3,732

sandra.rossi FORM?

3,732

I feel it's much more suitable for forum, more easy to understand. I never use subroutines apart forum.

In forum, I feel that doing "good" code is too much noise.

I know that if people continue posting obsolete code, that won't help people adopting good habits. I don't know.

Other people could even ask for class pools instead of executable programs.

"Noisy" version:

CLASS zcl_bit_string_to_x DEFINITION PUBLIC FINAL CREATE PRIVATE.
  PUBLIC SECTION.

    CLASS-METHODS convert
      IMPORTING
        bit_string_8_bytes TYPE csequence
      RETURNING
        VALUE(result)      TYPE xstring.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_bit_string_to_x IMPLEMENTATION.

  METHOD convert.
    DATA(number_of_bytes) = ( strlen( bit_string_8_bytes ) + 7 ) DIV 8.
    result = VALUE #( ).
    SHIFT result RIGHT BY number_of_bytes PLACES IN BYTE MODE.
    DATA(bit_number) = 0.
    DO strlen( bit_string_8_bytes ) TIMES.
      bit_number = bit_number + 1.
      DATA(bit_value) = CONV i( substring( val = bit_string_8_bytes off = bit_number - 1 len = 1 ) ).
      SET BIT bit_number OF result TO bit_value.
    ENDDO.
  ENDMETHOD.

ENDCLASS.

test class

CLASS ltc_main DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    METHODS test FOR TESTING.
ENDCLASS.
CLASS ltc_main IMPLEMENTATION.
  METHOD test.
    cl_abap_unit_assert=>assert_equals(
           act = zcl_bit_string_to_x=>convert( '1101000010100000110100001011010111010001100000011101000010111110' )
           exp = CONV xstring( 'D0A0D0B5D181D0BE' ) ).
  ENDMETHOD.
ENDCLASS.

matt
Active Contributor
3,732

sandra.rossi Fair enough, and you've helped me so often on really difficult stuff that I bow to your wisdom. I just have a visceral response to FORM. 🙂

alex_klim
Explorer
0 Kudos
3,732

Ok i try to convert my string using for example HR_KR_STRING_TO_XSTRING

Is is correct that in output xstring i have data like 313130313030303031303130303030303131303130303030313031313031303131313031303030313130303030303031313130313030303031303131313131313131303130303031313030303030313131313031303030303130313130303031313130313030303031303131313031313131303130303030313031313130303031313031303030303130313131303130313130313030303031303131303030303131303130303030313031313131303131313031303030313130303030303031313130313030303031303131313031303131303130303030313031313130303031313031303030303130313131303031303031303030303031313031303030?