Skip to main content
Notice removed Reward existing answer by lyxal
Bounty Ended with xigoi's answer chosen by lyxal
Tweeted twitter.com/StackCodeGolf/status/1394170831618650119
Notice added Reward existing answer by lyxal
Bounty Started worth 50 reputation by lyxal
formatting and add test case to cover digit output
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

In Salesforce CRM, every object has 15-character alphanumeric ID, which is case-sensitive. If anyone's curious, actually it's base-62 number. However, tools used for data migration and integration may or may not support case sensitivity. To overcome that, IDs can be safely converted to 18-character case-insensitive alphanumeric IDs. In that process 3-character alphanumeric checksum is appended to the ID.
The The conversion algorithm is:

exampleExample: a0RE000000IJmcN

a0RE000000IJmcN
  1. Split ID into three 5-character chunks.

    Split ID into three 5-character chunks.

     a0RE0  00000  IJmcN
    
  2. Reverse each chunk.

     0ER0a  00000  NcmJI
    
  3. Replace each character in every chunk by 1 if it's uppercase or by 0 if otherwise.

     01100  00000  10011
    
  4. For each 5-digit binary number i, get character at position i in concatenation of uppercase alphabet and digits 0-5 (ABCDEFGHIJKLMNOPQRSTUVWXYZ012345).

     00000 -> A,
     00001 -> B,
     00010 -> C, ..., 
     11010 -> Z, 
     11011 -> 0, ...,
     11111 -> 5`
    

    Yielding:

     M  A  T
    
  5. Append these characters, the checksum, to the original ID.

a0RE0 00000 IJmcN

  1. Reverse each chunk.

0ER0a 00000 NcmJI

  1. Replace each character in every chunk by 1 if it's uppercase or by 0 if otherwise.

01100 00000 10011

  1. For each 5-digit binary number i, get character at position i in concatenation of uppercase alphabet and digits 0-5 (ABCDEFGHIJKLMNOPQRSTUVWXYZ012345). 00000 -> A, 00001 -> B, 00010 -> C, ..., 11010 -> Z, 11011 -> 0, ..., 11111 -> 5

M A T

  1. Append these characters, the checksum, to the original ID.

outputOutput: a0RE000000IJmcNMAT

a0RE000000IJmcNMAT

Write program or function which takes 15-character alphanumeric (ASCII) string as input and returns 18-character ID.
Input

Input validation is out of scope of this question. Programs may return any value or crash on invalid input.

Test data: a01M00000062mPg -> a01M00000062mPgIAI 001M000000qfPyS -> 001M000000qfPySIAU a0FE000000D6r3F -> a0FE000000D6r3FMAR 0F9E000000092w2 -> 0F9E000000092w2KAA aaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaAAA AbCdEfGhIjKlMnO -> AbCdEfGhIjKlMnOVKV

Test Cases

a01M00000062mPg    -> a01M00000062mPgIAI
001M000000qfPyS    -> 001M000000qfPySIAU
a0FE000000D6r3F    -> a0FE000000D6r3FMAR
0F9E000000092w2    -> 0F9E000000092w2KAA
aaaaaaaaaaaaaaa    -> aaaaaaaaaaaaaaaAAA
AbCdEfGhIjKlMnO    -> AbCdEfGhIjKlMnOVKV
aBcDEfgHIJKLMNO    -> aBcDEfgHIJKLMNO025

In Salesforce CRM, every object has 15-character alphanumeric ID, which is case-sensitive. If anyone's curious, actually it's base-62 number. However, tools used for data migration and integration may or may not support case sensitivity. To overcome that, IDs can be safely converted to 18-character case-insensitive alphanumeric IDs. In that process 3-character alphanumeric checksum is appended to the ID.
The conversion algorithm is:

example: a0RE000000IJmcN

  1. Split ID into three 5-character chunks.

a0RE0 00000 IJmcN

  1. Reverse each chunk.

0ER0a 00000 NcmJI

  1. Replace each character in every chunk by 1 if it's uppercase or by 0 if otherwise.

01100 00000 10011

  1. For each 5-digit binary number i, get character at position i in concatenation of uppercase alphabet and digits 0-5 (ABCDEFGHIJKLMNOPQRSTUVWXYZ012345). 00000 -> A, 00001 -> B, 00010 -> C, ..., 11010 -> Z, 11011 -> 0, ..., 11111 -> 5

M A T

  1. Append these characters, the checksum, to the original ID.

output: a0RE000000IJmcNMAT

Write program or function which takes 15-character alphanumeric (ASCII) string as input and returns 18-character ID.
Input validation is out of scope of this question. Programs may return any value or crash on invalid input.

Test data: a01M00000062mPg -> a01M00000062mPgIAI 001M000000qfPyS -> 001M000000qfPySIAU a0FE000000D6r3F -> a0FE000000D6r3FMAR 0F9E000000092w2 -> 0F9E000000092w2KAA aaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaAAA AbCdEfGhIjKlMnO -> AbCdEfGhIjKlMnOVKV

In Salesforce CRM, every object has 15-character alphanumeric ID, which is case-sensitive. If anyone's curious, actually it's base-62 number. However, tools used for data migration and integration may or may not support case sensitivity. To overcome that, IDs can be safely converted to 18-character case-insensitive alphanumeric IDs. In that process 3-character alphanumeric checksum is appended to the ID. The conversion algorithm is:

Example:

a0RE000000IJmcN
  1. Split ID into three 5-character chunks.

     a0RE0  00000  IJmcN
    
  2. Reverse each chunk.

     0ER0a  00000  NcmJI
    
  3. Replace each character in every chunk by 1 if it's uppercase or by 0 if otherwise.

     01100  00000  10011
    
  4. For each 5-digit binary number i, get character at position i in concatenation of uppercase alphabet and digits 0-5 (ABCDEFGHIJKLMNOPQRSTUVWXYZ012345).

     00000 -> A,
     00001 -> B,
     00010 -> C, ..., 
     11010 -> Z, 
     11011 -> 0, ...,
     11111 -> 5`
    

    Yielding:

     M  A  T
    
  5. Append these characters, the checksum, to the original ID.

Output:

a0RE000000IJmcNMAT

Write program or function which takes 15-character alphanumeric (ASCII) string as input and returns 18-character ID.

Input validation is out of scope of this question. Programs may return any value or crash on invalid input.

Test Cases

a01M00000062mPg    -> a01M00000062mPgIAI
001M000000qfPyS    -> 001M000000qfPySIAU
a0FE000000D6r3F    -> a0FE000000D6r3FMAR
0F9E000000092w2    -> 0F9E000000092w2KAA
aaaaaaaaaaaaaaa    -> aaaaaaaaaaaaaaaAAA
AbCdEfGhIjKlMnO    -> AbCdEfGhIjKlMnOVKV
aBcDEfgHIJKLMNO    -> aBcDEfgHIJKLMNO025
Source Link
Trang Oul
  • 750
  • 4
  • 17

Convert Salesforce 15-character ID to 18-character

In Salesforce CRM, every object has 15-character alphanumeric ID, which is case-sensitive. If anyone's curious, actually it's base-62 number. However, tools used for data migration and integration may or may not support case sensitivity. To overcome that, IDs can be safely converted to 18-character case-insensitive alphanumeric IDs. In that process 3-character alphanumeric checksum is appended to the ID.
The conversion algorithm is:

example: a0RE000000IJmcN

  1. Split ID into three 5-character chunks.

a0RE0 00000 IJmcN

  1. Reverse each chunk.

0ER0a 00000 NcmJI

  1. Replace each character in every chunk by 1 if it's uppercase or by 0 if otherwise.

01100 00000 10011

  1. For each 5-digit binary number i, get character at position i in concatenation of uppercase alphabet and digits 0-5 (ABCDEFGHIJKLMNOPQRSTUVWXYZ012345). 00000 -> A, 00001 -> B, 00010 -> C, ..., 11010 -> Z, 11011 -> 0, ..., 11111 -> 5

M A T

  1. Append these characters, the checksum, to the original ID.

output: a0RE000000IJmcNMAT

Write program or function which takes 15-character alphanumeric (ASCII) string as input and returns 18-character ID.
Input validation is out of scope of this question. Programs may return any value or crash on invalid input.

Please, don't use Salesforce propretiary languages' features that make this challenge trivial (such as formula CASESAFEID(), converting Id to String in APEX &c).

Test data: a01M00000062mPg -> a01M00000062mPgIAI 001M000000qfPyS -> 001M000000qfPySIAU a0FE000000D6r3F -> a0FE000000D6r3FMAR 0F9E000000092w2 -> 0F9E000000092w2KAA aaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaAAA AbCdEfGhIjKlMnO -> AbCdEfGhIjKlMnOVKV