Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7184817
  • 博文数量: 655
  • 博客积分: 10264
  • 博客等级: 上将
  • 技术积分: 8278
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-04 17:47
个人简介

ABAP顾问

文章分类

全部博文(655)

文章存档

2017年(2)

2014年(8)

2013年(3)

2012年(2)

2011年(18)

2010年(102)

2009年(137)

2008年(274)

2007年(134)

分类:

2008-05-14 11:23:36

CL CL_ABAP_CONV_OUT_CE

____________________________________________________

短文本

Code Page and Endian Conversion (System Format -> External)

Functionality

Instances of the class CL_ABAP_CONV_OUT_CE allow you to convert ABAP data objects to binary data. (That is, data in the system format is converted to an external format.)
You can convert character sets (for text data) and the byte order (for numeric data).
Additionally, there are static methods, which allow you to ascertain the hexadecimal or decimal value in the Unicode codepage for any character in the current codepage. These methods are: and .

Relationships

Converts binary data into ABAP data objects
Converts ABAP data objects between two external binary formats. Bin鋜formaten.
Various attributes and methods for character sets and byte order
Corrects alignment of structures in containers of type C (or STRING). You need to make this correction if East-Asian characters ("full-width" characters in Chinese, Japanese, and Korean) are to be copied from a non-Unicode to a Unicode system or vice versa. You do not need to make the correction if you use the method CONVERT_STRUC from this class.

Example

In the following example, text from the system codepage is converted to UTF-8 and numbers from the system codepage are converted to little-endian format:
DATA:
    text(4) TYPE c VALUE 'ABC',
    int TYPE i VALUE 258.
  DATA:
    buffer1 TYPE xstring,
    buffer2 TYPE xstring,
    conv TYPE REF TO cl_abap_conv_out_ce.
  conv = cl_abap_conv_out_ce=>create(
           encoding = 'UTF-8'
           endian = 'L' ).
  conv->convert( EXPORTING data = text
                 IMPORTING buffer = buffer1 ).
  conv->convert( EXPORTING data = int
                 IMPORTING buffer = buffer2 ).

Calling the method creates a conversion instance. For example, the target codepage or the byte order used in the input buffer can be specified as the parameter.
After calling the method, the buffer1 variable contains the 4 bytes 41424320 (hexadecimal) that represent the string "ABC" in UTF-8. The buffer2 variable contains the 4 bytes 02010000 that represent the value 258 in little-endian format.
For details refer to the documentation for the individual methods.
You can convert structures, as shown in the following example:
DATA:
    BEGIN OF struc,
      text(5) TYPE c,
      int TYPE i,
    END OF struc.
  DATA:
    buffer TYPE xstring,
    conv TYPE REF TO cl_abap_conv_out_ce,
    view TYPE REF TO cl_abap_view_offlen.
  view = cl_abap_view_offlen=>create_legacy_view( struc ).
  conv = cl_abap_conv_out_ce=>create(
                              encoding = '0120'
                              endian = 'B' ).
  struc-text = 'Abc12'.
  struc-int  = 65538.
  conv->convert_struc( EXPORTING data = struc
                                 view = view
                       IMPORTING buffer = buffer ).

In this example, the CREATE_LEGACY_VIEW method is used to determine the layout generally used in non-Unicode systems. (You can use the CREATE_UNICODE16_VIEW method to determine the layout in Unicode systems.) The target codepage is specified as '0120' (SAP character set number for EBCDIC-Latin-1). At the end, the buffer(5) contains the bytes C18283F1F2 (for "Abc12") andbuffer+8(4) contains the bytes 00010002 (for 65538).
The CL_ABAP_CONV_OUT_CE class contains additional methods that implement a stream-oriented model:The ABAP data objects are written sequentially to a binary output buffer. The methods are used as follows:
See above.
The system converts the data. Calling this method several times consecutively allows you to add the data to the output buffer sequentially.
Gets the output buffer (binary strings) for further processing (writing data to a file, sending data by RFC, and so on).
Individual attributes of the conversion instance can be reset. (This method is particularly used for deleting the output buffer while retaining the remaining attributes.)
DATA:
  text(100) TYPE C VALUE 'ABC',
  int TYPE I VALUE 258.
DATA:
  buffer TYPE XSTRING,
  conv TYPE REF TO cl_abap_conv_out_ce.
conv = cl_abap_conv_out_ce=>create(
         encoding = 'UTF-8'
         endian = 'L'
       ).
CALL METHOD conv->write( data = text  n = 4 ).
CALL METHOD conv->write( data = int ).
buffer = conv->get_buffer( ).

The buffer variable now contains the hexadecimal string "4142432002010000", which is made up of:
  • 4 bytes (hexadecimal "41424320") that represent the string "ABC" in UTF-8.
  • 4 bytes (hexadecimal "02010000") that represent the value 258 in the little-endian format.
If you have the desired byte order in the old format as expected by (as an N(4) value), you can proceed as follows:
TYPE-POOLS: ABAP.
CLASS cl_abap_char_utilities DEFINITION LOAD.
DATA:
  number_format(4) TYPE N VALUE '0101'.
DATA:
  endian TYPE ABAP_ENDIAN,
  conv TYPE REF TO cl_abap_conv_out_ce.
endian = cl_abap_char_utilities=>number_format_to_endian(
           number_format
         ).
conv = cl_abap_conv_out_ce=>create(
         encoding = 'UTF-8'
         endian = endian
       ).

Notes

For details refer to the documentation for the individual methods.
阅读(10967) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~