Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9236

Re: Convert MEID (HEX) to MEID (DEC)

$
0
0

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         TYPEREFTO lcl_bignum.
  6.   DATA lr_16          TYPEREFTO lcl_bignum.    "fix value for base 16
  7.   DATA lr_power       TYPEREFTO lcl_bignum.
  8.   DATA lr_multiplier  TYPEREFTO lcl_bignum.
  9.   DATA lr_digit       TYPEREFTO lcl_bignum.
  10.   DATA lr_tempdec     TYPEREFTO lcl_bignum.
  11.   DATA offset TYPEi.
  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.       DO14TIMES.
  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.

/.


Viewing all articles
Browse latest Browse all 9236

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>