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 structure to char field

BjrnKorioth
Product and Topic Expert
Product and Topic Expert
0 Likes
6,742

Hi guys,

I am trying to convert a structure (ls_bt_i) into a charfield (ls_idoc_dd-sdata):


  FIELD-SYMBOLS:
    <ls_bt_i> TYPE c,
    <ls_bt_c> TYPE c.


* Generate Idoc Data-Record(s) - Item Segment
  lt_items[] = items.

  LOOP AT lt_items INTO ls_items.
    PERFORM data_segment_create CHANGING lv_segnum.
    PERFORM idoc_data_record_create USING gc_seg_i lv_psgnum CHANGING lv_segnum ls_idoc_dd.

    MOVE-CORRESPONDING ls_items TO ls_bt_i.
    ASSIGN ls_bt_i TO <ls_bt_i> CASTING.
    ls_idoc_dd-sdata = <ls_bt_i>.

It works fine with the char components of the structure, but there is a quantitiy and an amount field in the structure too and their values are in char something like #### (prob. the hex value).

So my question is howw can i convert the structure into the char without messing my quantity and amount fields up?

1 ACCEPTED SOLUTION
Read only

rmnachi_kx
Explorer
3,287

This one should be easy if you follow the traditional way. I assume ls_items and ls_bt_i shares similar data type declarations. Since you have used move corresponding, your source QTY of ls_items will be moved to ls_bt_i QTY field. My suggestion would be, if you are already aware of ls_items structure, then change your ls_bt_i structure to hold only Characters (Flat type I mean).

For Ex: Lets assume ls_items is having FKIMG as data type for QTY, then corresponding to this field, declare your ls_bt_i structure QTY field as FKIMG(17) type C. Since your ls_bt_i structure is of flat type, you don't need the declared field symbols (<ls_bt_i>). After move corresponding, just condense (optional) the qty field and assign it to sdata. This should be simple and solve your issue.

MOVE-CORRESPONDING ls_items TO ls_bt_i.
CONDENSE ls_bt_i-fkimg.
ls_idoc_dd-sdata = ls_bt_i.
TYPES: BEGIN OF typ_item,
         vbeln  TYPE vbeln,
         fkimg  TYPE fkimg,
       END OF typ_item,

       BEGIN OF typ_data,
         vbeln(10) TYPE c,
         fkimg(17) TYPE c,
       END OF typ_data.

DATA: ls_items TYPE typ_item,
      ls_bt_i  TYPE typ_data,
      ls_sdata TYPE char1024.

ls_items-vbeln = '0000100000'.
ls_items-fkimg = '1000.00'.
MOVE-CORRESPONDING ls_items TO ls_bt_i.
CONDENSE ls_bt_i-fkimg.
ls_sdata = ls_bt_i.
WRITE:/ ls_sdata.

Output is: 00001000001000.000
Without Condense : 0000100000        1000.000

Hi guys,

I am trying to convert a structure (ls_bt_i) into a charfield (ls_idoc_dd-sdata):


  FIELD-SYMBOLS:
    <ls_bt_i> TYPE c,
    <ls_bt_c> TYPE c.


* Generate Idoc Data-Record(s) - Item Segment
  lt_items[] = items.

  LOOP AT lt_items INTO ls_items.
    PERFORM data_segment_create CHANGING lv_segnum.
    PERFORM idoc_data_record_create USING gc_seg_i lv_psgnum CHANGING lv_segnum ls_idoc_dd.

    MOVE-CORRESPONDING ls_items TO ls_bt_i.
    ASSIGN ls_bt_i TO <ls_bt_i> CASTING.
    ls_idoc_dd-sdata = <ls_bt_i>.

It works fine with the char components of the structure, but there is a quantitiy and an amount field in the structure too and their values are in char something like #### (prob. the hex value).

So my question is howw can i convert the structure into the char without messing my quantity and amount fields up?

4 REPLIES 4
Read only

former_member400468
Active Participant
3,287

Hi !

You can try class CL_ABAP_STRUCTDESCR for getting structure fields and then check for fields type P whether they CURR or QUAN via class CL_ABAP_TYPEDESCR.

Also if you know exactly the fields and their currency\unit keys, you can just set them via WRITE statement

Hope it's helpful.

Evgeny

Read only

fabianlupa
Contributor
3,287

Depending on what you want to do (storage or transfer and not user interaction) CL_ABAP_CONTAINER_UTILITIES might also help.

Read only

rmnachi_kx
Explorer
3,288

This one should be easy if you follow the traditional way. I assume ls_items and ls_bt_i shares similar data type declarations. Since you have used move corresponding, your source QTY of ls_items will be moved to ls_bt_i QTY field. My suggestion would be, if you are already aware of ls_items structure, then change your ls_bt_i structure to hold only Characters (Flat type I mean).

For Ex: Lets assume ls_items is having FKIMG as data type for QTY, then corresponding to this field, declare your ls_bt_i structure QTY field as FKIMG(17) type C. Since your ls_bt_i structure is of flat type, you don't need the declared field symbols (<ls_bt_i>). After move corresponding, just condense (optional) the qty field and assign it to sdata. This should be simple and solve your issue.

MOVE-CORRESPONDING ls_items TO ls_bt_i.
CONDENSE ls_bt_i-fkimg.
ls_idoc_dd-sdata = ls_bt_i.
TYPES: BEGIN OF typ_item,
         vbeln  TYPE vbeln,
         fkimg  TYPE fkimg,
       END OF typ_item,

       BEGIN OF typ_data,
         vbeln(10) TYPE c,
         fkimg(17) TYPE c,
       END OF typ_data.

DATA: ls_items TYPE typ_item,
      ls_bt_i  TYPE typ_data,
      ls_sdata TYPE char1024.

ls_items-vbeln = '0000100000'.
ls_items-fkimg = '1000.00'.
MOVE-CORRESPONDING ls_items TO ls_bt_i.
CONDENSE ls_bt_i-fkimg.
ls_sdata = ls_bt_i.
WRITE:/ ls_sdata.

Output is: 00001000001000.000
Without Condense : 0000100000        1000.000
Read only

Former Member
3,287

And Why is this happening ?

Because numeric information is stored in a variety of formats that are not human readable such ad BCD or Mantissa/Exponent for floating points. When you move a structure to a string, then the structure is treated as a single long string which does not respect the individual field types so you then get the binary representations of the number as a string which are generally unprintable characters displayed as '#'.

As an aside you could (before Unicode) emulate the CHR() and ASC() functions of other languages using a structure:

Data: Begin Of Chr,
            Asc type x,
      End Of Chr.
*
Chr-Asc = 65.
Write :/ Chr.          " = 'A'
*
Chr = 'B'.
Write :/ Chr-Asc.      " = 66.

This occurs in a lot of languages because the only way to treat a structure as a single piece of data is a string.

Rich