Application Development 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: 

How to convert string to array of bytes

override21
Member
0 Kudos
1,522

Hello I'm looking for method to convert string to array of bytes.

I need something what returns result as:

C#:

byte[] arr;

string str;

str = "that is a string";

arr = System.Text.ASCIIEncoding.UTF8.GetBytes(str);

or

Java:

String str = "string";

byte arr[] = str.getBytes();

I have tried to use FM 'SCMS_STRING_TO_XSTRING' or 'HR_KR_STRING_TO_XSTRING' but it doesn't work properly.

I'm obligated in specification to convert string to array of bytes and then make RSA encryption using public key and array of bytes.SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; }.L0S33 { color: #4DA619; }

4 REPLIES 4

FredericGirod
Active Contributor
1,183

You have a lot of method to convert in the class CL_BCS_CONVERT

SURYA_ABAP
Participant
0 Kudos
1,183

Hi,

Try this, it will work.

1 - SCMS_STRING_TO_XSTRING

2 - SCMS_XSTRING_TO_BINARY

Sandra_Rossi
Active Contributor
1,183

Converting characters into bytes means 2 things:

  • you need to execute Character Encoding, so you need to know which Code Page/Character Set, Transformation algorithm, BOM, etc. you want to use. There are potentially many possibilities, and also many different ABAP classes or function modules.
  • you need to know which data type you want to use to store the bytes. As you say "array of bytes", probably you mean a "table of lines containing bytes" (which is quite an obsolete solution to me, better use the type XSTRING as far as possible), but there are potentially many possibilities for these tables of bytes.

and the combination of the two above can lead to even more possibilities.

Please indicate your exact issue/requirement.

ThorstenHoefer
Active Contributor
0 Kudos
1,183

To convert binary to string, you can use this example:

  DATA: lv_encode TYPE abap_encod.
lv_encode = cl_abap_codepage=>sap_codepage( 'UTF-8' ).
lr_converter = cl_abap_conv_in_ce=>create( input = <doc>
encoding = lv_encode
replacement = '' ).
lr_converter->read( EXPORTING n = xstrlen( <doc> )
IMPORTING data = <wa_doc>-doc ).

To convert string back to binary, you can use class CL_ABAP_CONV_OUT_CE