csv_to_structure方法为什么会抛出溢出错误?

huangapple go评论70阅读模式
英文:

Why csv_to_structure method throws overflow error?

问题

我正在读取一个具有以下结构的CSV文件:

Nr;Name;Name;Nr;Name
14;Doe;John;0838;test
15;Doe;John;0835;test2
16;Doe;John;1008;test3

我将数据读入一个结构类型中:

types:
begin of ty_dbstr,
Nr TYPE p_pernr, "char 10
Nachname TYPE nachname, "char 25
Vorname TYPE vorname, "char 25
ProdZeit TYPE ZXXX_hours, "dec length 4 decimals 2
Datum TYPE datum, "char 8
end OF ty_dbstr.

使用函数'GUI_UPLOAD',我获得一个结果表lt_raw_dataTYPE truxs_t_text_data

现在我正在处理数据,并希望将它们写入先前声明的结构的表中:

LOOP AT lt_raw_data INTO DATA(ls_csv_line).
CALL METHOD lo_csv_converter->csv_to_structure
EXPORTING
i_data = ls_csv_line
IMPORTING
e_s_data = <fs_wa>.
.
.
.

ENDLOOP.

这对前两个条目运行正常,但第三个会导致崩溃,因为它说转换1008时溢出

似乎1008不适合具有4位长度和2位小数的十进制数,这是怎么可能的?

英文:

I'm reading a csv file with following structure:

Nr;Name;Name;Nr;Name
14;Doe;John;0838;test
15;Doe;John;0835;test2
16;Doe;John;1008;test3

I'm reading the data into a struct type:

types:
  begin of ty_dbstr,
    Nr   TYPE p_pernr,        &quot;char 10
    Nachname TYPE nachname,   &quot;char 25
    Vorname  TYPE vorname,    &quot;char 25
    ProdZeit TYPE ZXXX_hours, &quot;dec length 4 decimals 2
    Datum    TYPE datum,      &quot;char 8
  end OF ty_dbstr.

With function &#39;GUI_UPLOAD&#39; I get a result table lt_raw_data (TYPE truxs_t_text_data)

Now I'm processing the data and want to write them in a table of the former declared struct using:

LOOP AT lt_raw_data INTO DATA(ls_csv_line).
      CALL METHOD lo_csv_converter-&gt;csv_to_structure
        EXPORTING
          i_data   = ls_csv_line
        IMPORTING
          e_s_data = &lt;fs_wa&gt;.
.
.
.

 ENDLOOP.

This works fine for the first 2 entries, but the third produces a dump because it says Overflow when converting 1008

It seems like the 1008 does not fit in a dec of length 4 with 2 decimals, how is this possible?

答案1

得分: 3

数据字典数据类型:

DEC 长度 4 小数位 2

意味着总长度为4,即有2位用于整数部分,2位用于小数部分。

这意味着可以存储在此类型中的最大值为:99.99

在文档中,这并不那么明显:

DEC 用于紧凑型数字
此类型描述BCD格式的一般紧凑型数字。在使用时,必须添加类型的长度和小数位数。

当ABAP数据字典类型(即DEC 4)用于ABAP类型定义时,环境会执行从DEC到p的映射,此时p的长度将根据DEC的长度计算:

DEC(长度 1-31) -> 到 p(length_of_DEC 除以 2 + 1)

在上述情况中,DEC长度为4,小数位数为2,映射为[P(3) DEC 2],但3位的整数部分不足以存储1008,这导致溢出。因此,在这种情况下,数据字典类型应至少为DEC长度6(6 除以 2 + 1 = P(4))才能存储这个数字。

还值得一提的是,基于预定义类型DEC的数据类型定义应使用奇数位数。由于值以BCD格式存储,因此在数据类型p中,仅允许使用奇数位数,因为半字节用于符号。如果数据类型DEC具有偶数字符,与之关联的ABAP类型p的长度将四舍五入并包含下一个最高的奇数位数。这可能导致溢出和异常。

英文:

Dictionary data type:
>DEC Length 4 Decimal places 2

means that the total length is 4, i.e. 2 places are used for the whole part of the number and 2 are used for the decimal part.

That means that the maximum value to store in this type is: 99.99

In the documentation it is not so obvious:
> DEC for packed numbers
> This type describes general packed numbers in BCD format. When used, a length and the number of decimal places must be added to the type.

When an ABAP Dictionary type (i.e DEC 4) is used for the type definition in abap, environment performs the mapping to the abap type, in this case from DEC to p, and the length of p will be calcuated based of the length of DEC:

DEC (length 1-31) -> to p (length_of_DEC DIV 2 + 1)

In the above case DEC length 4 decimals 2 is mapped to the [P(3) DEC 2], and the whole part of 3 digits is not enough to store 1008, which leads to overflow. So in this case Dictionary type should be at least DEC length 6 ( 6 DIV 2 + 1 = P(4) ) to store the number.

It also worth mentioning, that an odd number of places should be used in the definition of data types based on the predefined type DEC. Since the values are stored in BCD format, for which in data type p, only an odd number of places is possible, since a half byte is used for the sign. If a data type DEC has an even number of characters, the length of the associated ABAP type p is rounded up and hence contains the next highest odd number of places. This can cause overflows and exceptions.

huangapple
  • 本文由 发表于 2023年3月9日 22:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定