Skip to Content
author's profile photo Former Member
Former Member

Convert MEID (HEX) to MEID (DEC)

Hello Experts,

I have been trying to convert this value:

99000124997700

to this value:

256691434010057472

As it is done in at this website:

http://www.meidconverter.net/index.php#Results

My problem is my code does not convert it correctly.

Input:

99000124997700

Output:

172805295610057472

Can anyone advise to what is the issue or in theory how these conversion work?



 DATA:

lv_input TYPE /sapdmc/ls_value,

lv_dec_1 TYPE string,

lv_dec_2 TYPE string,

ev_export TYPE to_dec,

lv_hex(4) TYPE x.



lv_input = iv_hex_value(8).



*-- input first part of HEX value

CALL FUNCTION 'Z_HEX_TO_DEC_CONVERT'

EXPORTING

pin = lv_input

IMPORTING

ra = ev_export.



*-- store in tmp variable

lv_dec_1 = ev_export.

CONDENSE lv_dec_1.

lv_input = iv_hex_value+8(8).



CLEAR ev_export.



*-- input the rest of the HEX value

CALL FUNCTION 'Z_HEX_TO_DEC_CONVERT'

EXPORTING

pin = lv_input

IMPORTING

ra = ev_export.



lv_dec_2 = ev_export.

CONDENSE lv_dec_2.



CONCATENATE lv_dec_1 lv_dec_2 INTO lv_input SEPARATED BY ''.

REPLACE '-' in lv_input WITH ''.

CONDENSE lv_input NO-GAPS.

ev_decimal_value = lv_input.

Code of Z_HEX_TO_DEC_CONVERT



data: i_ra(15) type x,

i_ra2(4) type x,

i_pin like pin,

n_pin(30) type n,

i_len type i,

i_ra3 type xstring.



if pin co '1234567890 '.

move pin to i_pin.

move i_pin to n_pin.

i_pin = n_pin.

i_ra = i_pin.

ra = i_ra.

else.

move pin to i_pin.

i_len = strlen( i_pin ).

if i_len = 8.

i_ra2 = i_pin.

i_ra = i_pin.

i_ra3 = i_pin.

ra = i_ra2.

ra = i_ra.

ra = i_ra3.

else.

while i_len lt 30.

concatenate '0' i_pin into i_pin.

i_len = strlen( i_pin ).

endwhile.

i_ra = i_pin.

ra = i_ra.

* ra = i_num.

endif.



endif.

:Thanks

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Feb 08, 2014 at 09:16 AM

    I had a look at wikipedia article on MEID format and it gave required information.

    You want to convert 99000124997700 to 256691434010057472.

    99000124base16 = 2566914340base10

    997700base16 = 10057472base10

    Concatenating base10 numbers gives 256691434010057472.

    Theory:

    Split the hex number into 2 parts, length 8 and length 6.

    Convert those parts to decimal, and then concatenate the parts.

    Code:

    The code is not straightforward. Reason being the integer variable is of 2 bytes, and it can't store big enough numbers like 2566914340.

    I had written a document on how to handle Integer Arithmetic Operations on Large Numbers

    Below code snippet is using same class given in my document, and it is able to give correct answer.

    After looking at how it works, you can clean and optimize it.

    Note: Padding zeros on left side of parts may be required.

    1. START-OF-SELECTION.
    2. DATA meid_hex TYPE string VALUE '99000124997700'.
    3. DATA meid_dec TYPE string.
    4. DATA temp TYPE string.
    5. DATA lr_dec TYPE REF TO lcl_bignum.
    6. DATA lr_16 TYPE REF TO lcl_bignum. "fix value for base 16
    7. DATA lr_power TYPE REF TO lcl_bignum.
    8. DATA lr_multiplier TYPE REF TO lcl_bignum.
    9. DATA lr_digit TYPE REF TO lcl_bignum.
    10. DATA lr_tempdec TYPE REF TO lcl_bignum.
    11. DATA offset TYPE i.
    12. offset = 13. "13th character is last for hex meid
    13. TRY .
    14. CREATE OBJECT lr_dec.
    15. CREATE OBJECT lr_16.
    16. CREATE OBJECT lr_power.
    17. CREATE OBJECT lr_multiplier.
    18. CREATE OBJECT lr_digit.
    19. CREATE OBJECT lr_tempdec.
    20. * initialize the base16 value
    21. lr_16->set_data( '16' ).
    22. DO 14 TIMES.
    23. *calculate decimal multiplier based on hex position
    24. * decimal number 123 = 1 * 10^2 + 2 * 10^1 + 3 * 10^0
    25. * decimal equalant of hex number 543 = 5 * 16^2 + 4 * 16^1 + 3 * 16^0
    26. temp = sy-index - 1.
    27. lr_power->set_data( temp ).
    28. lr_multiplier->power( i_obj1 = lr_16
    29. i_obj2 = lr_power ).
    30. * we loop from last hex digit to first hex digit
    31. temp = meid_hex+offset(1).
    32. lr_digit->set_data( temp ).
    33. lr_tempdec->multiply( i_obj1 = lr_multiplier
    34. i_obj2 = lr_digit ).
    35. * add digit including its weight to final decimal result
    36. lr_dec->add( i_obj1 = lr_dec
    37. i_obj2 = lr_tempdec ).
    38. * temp = lr_dec->get_string( ).
    39. * WRITE:/ offset, temp.
    40. * number = number + meid_hex+offset(1) * ( 16 ** ( sy-index - 1 ) ).
    41. offset = offset - 1.
    42. ENDDO.
    43. lr_power->set_data( '6' ).
    44. lr_multiplier->power( i_obj1 = lr_16
    45. i_obj2 = lr_power ).
    46. lr_tempdec->divide( i_obj1 = lr_dec
    47. i_obj2 = lr_multiplier ).
    48. * get part1
    49. meid_dec = lr_tempdec->get_string( ).
    50. * get part2
    51. lr_tempdec->mod( i_obj1 = lr_dec
    52. i_obj2 = lr_multiplier ).
    53. temp = lr_tempdec->get_string( ).
    54. CONCATENATE meid_dec temp INTO meid_dec.
    55. WRITE:/ meid_dec.
    56. CATCH cx_root.
    57. ENDTRY.

    /.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Now I know what the problem was. Yesterday i wrote:

      In fact, splitting that number to size 8 and 6 and then convert to decimal using calculator does not give same value.

      The issue is that for alpha-numeric MEID, length is 15 (not 14). And it is IMEI represented in hex.

      As a result, the splitting of parts needs to be done so that first 8 digits go to first part, and remaining (6 or 7) go to second part.

      Keeping this in mind, these are the changes you need to do in code snippet.

      Replace:

      part2 = meid_hex+8(6).

      With:

      part2 = meid_hex+8.

      For 0-9 values, default value is good enough.

      Replace:

      temp = iv_hex+offset(1).

      With:

      temp = iv_hex+offset(1).

      CASE temp.

      WHEN 'A'.

      temp = '10'.

      WHEN 'B'.

      temp = '11'.

      WHEN 'C'.

      temp = '12'.

      WHEN 'D'.

      temp = '13'.

      WHEN 'E'.

      temp = '14'.

      WHEN 'F'.

      temp = '15'.

      WHEN OTHERS.

      ENDCASE.

      EDIT: The decimal part1 needs to be 10digits, and decimal part2 needs to be 8 digits. If it isn't, you need to pad zeros to the left to increase the size. This can be done easily by moving part1 to numc length 10 variable, and part2 to numc length 8 variable. Then concatenate to get answer.

      /.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.