I'm currently working on a code generator and porting it over to javascript, however I can't seem to get the same results?
The code which I've got in Python is the following:
def sign(self, cs, api):
temp_string = ""
cs_length = len(cs)
api_length = len(api)
if api_length > 0:
for index in range(0, cs_length):
temp_char = cs[index]
temp_code = ord(temp_char)
temp_code_2 = ord(api[index % api_length])
new_code = self.get_code(temp_code, 47, 57, temp_code_2)
new_char = ""
if new_code != cs[index]:
new_char = chr(new_code)
try:
temp_string += new_char
except:
xbmc.log("Unprintable character => %s" % new_char)
xbmc.log("Index: %i\tCharacter %i:%s\t\tModulus %i\t\tModulus Code %i:%s\t\tNew Code %i:%s" %
(index, temp_code, temp_char, (index % api_length), temp_code_2, chr(temp_code_2), new_code,
new_char))
else:
xbmc.log("Signature cannot be blank")
# xbmc.log("\r\nTarget Signature: 7a74G7m23Vrp0o5c")
xbmc.log("Result signature: %s" % temp_string[0:16])
return temp_string[0:16]
def get_code(self, char_code, const_1, const_2, char_result):
if const_1 < char_code <= const_2:
char_code += char_result % (const_2 - const_1)
if char_code > const_2:
char_code = char_code - const_2 + const_1
return char_code
And here's what I've wrote in Javascript:
get_code(char_code, const_1, const_2, char_result) {
if(const_1 < char_code <= const_2) {
char_code += char_result % (const_2 - const_1);
if(char_code > const_2) {
char_code = char_code - const_2 + const_1;
}
}
return char_code;
}
sign(cs, api) {
let temp_string = '';
let cs_length = cs.length;
let api_length = api.length;
if(api_length > 0) {
for(let index in this.range(0, cs_length)) {
let temp_char = cs[index];
let temp_code = temp_char.charCodeAt(0);
let temp_code_2 = api[index % api_length].charCodeAt(0);
let new_code = this.get_code(temp_code, 47, 57, temp_code_2);
let new_char = "";
if(new_code != cs[index]) {
try {
new_char = new_code.charCodeAt(0);
temp_string += new_char;
} catch(error) {
console.log('"Unprintable character => ' + new_char);
}
}
}
}
return temp_string.substring(0, 16);
}
When running it in Javascript, I'm just getting a ton of "Unprintable character => results being thrown?