Integer to character and vice-versa
Implement two method that used in tiny url generation and parsing, first method will convert string to number and second one is convert same number to string.
Number to String Algorithm:
- Set char_base = 26
- Set char_map = “abc…zABC..Z01..9”
- Create output_string
- Iterate till number is greater than zero
- add
output_string += char_map.charAg(number % char_map)
- add
- reverse output_string
- return output_string
String to Number Algorithm:
- Set char_base = 26
- Set char_map = “abc…zABC..Z01..9”
- Set number = 0
- Iterate from 0 to string.length
number += string.charAt(i) * char_base ^ (string.length - (index + 1))
- Return number
Source Code:
Github: TinyUrl.java
Output:
Tiny url: irm9P Id: 122312215