0

I have a request to generate unique codes through an algorithm.

  1. take a unique number from a sequence (maximum value 2 ^ 30)
  2. ZBase32 encoding is used to encrypt the number. The encoding alphabet is as follows: YBNDRFG8EJKMCPQXOT1UWISZA345H769

can you help me in case it can be done? thanks!

2
  • 1
    Welcome to SFSE. Right now, your question doesn't fit our format here: we look to see what you've tried so far and where you're stuck, that is, a specific problem in your work. Additionally, it's not clear how your question relates to the Salesforce platform.
    – David Reed
    Commented Apr 28, 2021 at 14:32
  • 1
    Could you take a look at How to Ask and help center, and then edit your post to add more information?
    – David Reed
    Commented Apr 28, 2021 at 14:32

1 Answer 1

1

A naive implementation for this would be:

static String[] baseMap = 'YBNDRFG8EJKMCPQXOT1UWISZA345H769'.split('');
public String encodeZbase32(Integer source) {
    return 
        baseMap[(source >> 25)&31] +
        baseMap[(source >> 20)&31] +
        baseMap[(source >> 15)&31] +
        baseMap[(source >> 10)&31] +
        baseMap[(source >>  5)&31] +
        baseMap[(source      )&31];
    
}
public Integer decodeZBase32(String source) {
    String[] bits = source.split('');
    return
        (baseMap.indexOf(bits))<<25 |
        (baseMap.indexOf(bits))<<20 |
        (baseMap.indexOf(bits))<<15 |
        (baseMap.indexOf(bits))<<10 |
        (baseMap.indexOf(bits))<< 5 |
        (baseMap.indexOf(bits));
}

I leave it up to you to decide if you need the trailing 0 bits at the end, or any other features.

Not the answer you're looking for? Browse other questions tagged .