Question:
Hi
I need to create a 5 Byte Integer as part of a unique key. The requirement comes from our architektur team. Therefore i can't change it...
SAP supports just 1, 2 & 4 Byte Integer. It is possible calculate with a X(5) field but "just" up to 4'294'967'295. If the 4Byte Integer limit is exceeded the system dumps. Hence I can't use SAP Standard conversion.
I'm looking for a function that converts a field of type P into HEX format as used in a integer.
E.g. 44571 -> AE1B (but for more than 4 Byte).
Any idea?
Thanks
Regards
Joe
Answer:
It's easy enough to code it yourself. Use a string to hold the results.
Calculate the divisor by raising a packed field containing your base in incremental steps until it's larger that the dividend.
Then divide the dividend by the divisor, multiply the result by the divisor and subtract that from the dividend.
Divide the divisor by the base.
Use the result as a pointer into the string '0123456789ABCDEF' and concatenate that single character into your result string.
Keep going until the divisor is 0.
(Hope I got that right..... )
_________________
Regards
R
Abap KC
SFMDR
Answer:
[quote="R"]It's easy enough to code it yourself. Use a string to hold the results.
[quote]
Yup, standard maths for base conversion. Unless it's signed. Or there's some kind of endian issue involved...
I was once on a project where they did something similar, but much bigger. The number was elarge enough to count the atoms in the sun. I suppose telling the architecture team to get s&*^%$$ isn't an option, sadly.
Answer:
Thank you R!
How about that:
REPORT zkej_int5.
CONSTANTS: v_hex(16) TYPE c VALUE '0123456789ABCDEF'.
CONSTANTS: v_maxdigits TYPE i VALUE 10.
DATA: destx(5) TYPE x.
DATA: destc(10) TYPE c.
DATA: tmp1(12) TYPE p.
DATA: tmp2(12) TYPE p.
DATA: offset TYPE i.
DATA: exponent TYPE i.
DATA: factor TYPE i.
PARAMETERS: p12(12) TYPE p.
START-OF-SELECTION.
WRITE: / p12.
MOVE p12 TO tmp1.
DO v_maxdigits TIMES.
offset = sy-index - 1.
exponent = v_maxdigits - sy-index.
CLEAR: tmp2, factor.
WHILE tmp2 LE tmp1.
factor = factor + 1.
tmp2 = factor * ( 16 ** exponent ).
ENDWHILE.
factor = factor - 1.
MOVE v_hex+factor(1) TO destc+offset(1).
tmp2 = factor * ( 16 ** exponent ).
tmp1 = tmp1 - tmp2.
ENDDO.
WRITE: / destc COLOR COL_POSITIVE.
MOVE destc TO destx.
(this exmaple is specific for converteng a packed 13 to a int 5)
Regards[/code]
Answer:
Hey Joe! Thanks for getting back to us.
One last favour - could you add [RESOLVED] to the front of your thread title so people know there is an answer here.
..... On second thoughts make that [Resolved] in case Snowy's fingers get a bit itchy....
_________________
Regards
R
Abap KC
SFMDR
Answer:
I optimzed the code. Runtime is about 4 to 5 times better...
REPORT zkej_int5_2.
CONSTANTS: v_hex(16) TYPE c VALUE '0123456789ABCDEF'.
CONSTANTS: v_maxdigits TYPE i VALUE 10.
DATA: destx(5) TYPE x.
DATA: destc(10) TYPE c.
DATA: tmp1(13) TYPE p.
DATA: tmp2(13) TYPE p.
DATA: offset TYPE i.
DATA: exponent TYPE i.
DATA: factor TYPE i.
PARAMETERS: p13(13) TYPE p.
START-OF-SELECTION.
WRITE: / p13.
MOVE p13 TO tmp1.
DO v_maxdigits TIMES.
offset = sy-index - 1.
exponent = v_maxdigits - sy-index.
tmp2 = 16 ** exponent.
factor = tmp1 DIV tmp2.
tmp1 = tmp1 MOD tmp2.
MOVE v_hex+factor(1) TO destc+offset(1).
ENDDO.
WRITE: / destc COLOR COL_POSITIVE.
MOVE destc TO destx.