How to convert 2 guides to a string of no more than 50 characters (conversion in 2 ways)

there is an interesting problem - I need to convert 2 (randomly) created Guides to a string. Here are the limitations:

  • string max 50 has a length.
  • only numbers and small letters can be used (0123456789abcdefghijklmnopqrstuvwxyz)
  • the algorithm should be in the 2nd way - you need to be able to decode the encoded string into the same 2 separate loops.

I have been looking through the toBase36 conversion a lot, so far no luck with Guid.

Any ideas? (FROM#)

+6
source share
1 answer

First of all, you're in luck, 36 ^ 50 is about 2 ^ 258.5, so you can save the information in a line with a 50 byte byte. It is interesting, however, why for this someone will have to use base-36.

You need to treat each GUID as a 128-bit number, and then combine them into a 256-bit number, which is then converted to base number 36. Converting back does the same thing in reverse.

Guid.ToByteArray converts a GUID to a 16-byte array. Do this for both GUIDs, and you have an array of 32 bytes (which is 256 bits). Build a BigInt from this array (there is a constructor there), and then just convert that number to base-36.

To convert a number to base 36, do something like this (I assume everything is positive)

 const string digits = "0123456789abcdefghijklmnopqrstuvwxyz"; string ConvertToBase36(BigInt number) { string result = ""; while(number > 0) { char digit = string[number % 36]; result += digit; number /= 36; } } 
+9
source

Source: https://habr.com/ru/post/910960/


All Articles