2019 Nov 01 9:18 AM
Dear Experts,
I have a code from #C from hex data type as follow
0x01 that will read in serial port as 01
How do you convert that into abap code?
DATA: x type i.
x = cl_abap_codepage=>convert_to( '0x02' )
The expected result should be 2
but I get 813183026
Thanks,
Sam
2019 Nov 01 12:58 PM
To convert one byte containing FF hexadecimal value (decimal 255) into an integer variable:
DATA: byte TYPE x LENGTH 1,
integer TYPE i.
byte = 'FF'.
integer = byte.
ASSERT integer = 255.PS: other remarks about your question:
The writing "0x..." is interpreted by ABAP as two characters with no special meaning.
If you want to express a byte value:
DATA byte TYPE x LENGTH 1.
byte = 'FF'. " the two characters FF are interpreted as hexadecimal because byte has type X
DATA(byte_string) = CONV xstring( 'FF00FF00' ). " 4 bytes / interpreted as hexa because type XSTRINGFor more information: ABAP documentation - Conversion Rules for Elementary Data Objects
The method CONVERT_TO of cl_abap_codepage converts a text ('0x02' are four characters) into bytes, by using the encoding parameter whose default is UTF-8 (in your case all four characters are part of US-ASCII, so it's four bytes, '30783032' in hexadecimal notation).
But you transfer it to an integer variable, so the standard conversion rule from X to I applies (see explanation in the link above) which gives 813183026 (as you can see in the Windows Calculator in Programmer mode: press "HEX", type "30783032", and you see "813 183 026" at the right of "DEC"). Note that SAP could also have given "842 037 296" if your system was of type Little Endian.
Dear Experts,
I have a code from #C from hex data type as follow
0x01 that will read in serial port as 01
How do you convert that into abap code?
DATA: x type i.
x = cl_abap_codepage=>convert_to( '0x02' )
The expected result should be 2
but I get 813183026
Thanks,
Sam
2019 Nov 01 10:09 AM
As sandra.rossi said several times, Hex or decimal is only the representation of the same data.
data hexa type xstring value '0x02'.
data(dec) = conv string( hexa ).
write dec.
dec = 2.
hexa = dec.
write hexa.
2019 Nov 01 12:33 PM
Please use the CODE button to format your code so that it's shown in a more user-friendly format (colorized).
2019 Nov 01 12:54 PM
2019 Nov 01 12:58 PM
To convert one byte containing FF hexadecimal value (decimal 255) into an integer variable:
DATA: byte TYPE x LENGTH 1,
integer TYPE i.
byte = 'FF'.
integer = byte.
ASSERT integer = 255.PS: other remarks about your question:
The writing "0x..." is interpreted by ABAP as two characters with no special meaning.
If you want to express a byte value:
DATA byte TYPE x LENGTH 1.
byte = 'FF'. " the two characters FF are interpreted as hexadecimal because byte has type X
DATA(byte_string) = CONV xstring( 'FF00FF00' ). " 4 bytes / interpreted as hexa because type XSTRINGFor more information: ABAP documentation - Conversion Rules for Elementary Data Objects
The method CONVERT_TO of cl_abap_codepage converts a text ('0x02' are four characters) into bytes, by using the encoding parameter whose default is UTF-8 (in your case all four characters are part of US-ASCII, so it's four bytes, '30783032' in hexadecimal notation).
But you transfer it to an integer variable, so the standard conversion rule from X to I applies (see explanation in the link above) which gives 813183026 (as you can see in the Windows Calculator in Programmer mode: press "HEX", type "30783032", and you see "813 183 026" at the right of "DEC"). Note that SAP could also have given "842 037 296" if your system was of type Little Endian.
2019 Nov 01 1:02 PM
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |