updated encode/decode to use string index instead of substring and added a pad parameter