这个Python函数可以转换成Javascript吗?
我从事密码工作;加密和解密,我目前正在研究现代拉丁字母表。例如,字母“a”是两位数代码,“11”和“abc”是“11 12 13” 我在Python中找到了一个解决方案:这个Python函数可以转换成Javascript吗?,javascript,python,encryption,Javascript,Python,Encryption,我从事密码工作;加密和解密,我目前正在研究现代拉丁字母表。例如,字母“a”是两位数代码,“11”和“abc”是“11 12 13” 我在Python中找到了一个解决方案: # Python Program to implement polybius cipher # function to display polybius cipher text def polybiusCipher(s): # convert each character to its enc
# Python Program to implement polybius cipher
# function to display polybius cipher text
def polybiusCipher(s):
# convert each character to its encrypted code
for char in s:
# finding row of the table
row = int((ord(char) - ord('a')) / 5) + 1
# finding column of the table
col = ((ord(char) - ord('a')) % 5) + 1
# if character is 'k'
if char == 'k':
row = row - 1
col = 5 - col + 1
# if character is greater than 'j'
elif ord(char) >= ord('j'):
if col == 1 :
col = 6
row = row - 1
col = col - 1
print(row, col, end ='', sep ='')
我正在尝试转换它,并不断得到NaN
,因为我误用了一些东西,或者多个东西。以下是我到目前为止的情况:
function polybius(input, encode = true) {
for (let i = 0; i < input.length; i++) {
let letter = input[i];
let row = letter.charCodeAt(0 - letter.charCodeAt(97)) / 5 + 1;
let col = letter.charCodeAt(0 - (letter.charCodeAt(97) % 5)) + 1;
if (letter.charCodeAt(0) === letter.charCodeAt(107)) {
row = row - 1;
col = 5 - col + 1;
} else if (letter.charCodeAt(0) >= letter.charCodeAt(106)) {
if (col === 1) {
col = 6;
row = row - 1;
}
col = col - 1;
}
return `${row}${col} `;
}
}
函数polybius(输入,encode=true){
for(设i=0;i让row=letter.charCodeAt(i-letter.charCodeAt('a'))/5+1
不正确,因为字母本身的字符代码是字母。charCodeAt(0)
,而'a'
的字符代码是'a'。charCodeAt(0)
。此外,您需要使用Math.floor
进行四舍五入
因此,它应该是let row=Math.floor((letter.charCodeAt(0)-'a'.charCodeAt(0))/5)+1代码>
col
初始化也有类似的问题,应该是let col=(letter.charCodeAt(0)-'a'.charCodeAt(0))%5+1代码>
接下来,要检查字母是否为k
,不需要比较字符代码,您可以直接使用letter==“k”
。为了比较字母是否大于或等于'j'
,存在类似问题,您可以直接使用='j'
最后,在循环的第一次迭代中返回,因此它只运行一次。相反,您可以构建一个字符串以在末尾输出或返回
function polybius(input, encode = true) {
let str = '';
for (let i = 0; i < input.length; i++) {
let letter = input[i];
let row = Math.floor((letter.charCodeAt(0) - 'a'.charCodeAt(0)) / 5) + 1;
let col = (letter.charCodeAt(0) - 'a'.charCodeAt(0)) % 5 + 1;
if (letter === 'k') {
row = row - 1;
col = 5 - col + 1;
} else if (letter >= 'j') {
if (col === 1) {
col = 6;
row = row - 1;
}
col = col - 1;
}
str += `${row}${col} `;
}
console.log(str);
}
函数polybius(输入,encode=true){
设str='';
for(设i=0;i='j'){
如果(列===1){
col=6;
行=行-1;
}
col=col-1;
}
str+=`${row}${col}`;
}
console.log(str);
}
让row=letter.charCodeAt(i-letter.charCodeAt('a'))/5+1
不正确,因为字母本身的字符代码是字母。charCodeAt(0)
,而'a'
的字符代码是'a'。charCodeAt(0)
。此外,您需要使用Math.floor
进行四舍五入
因此,它应该是let row=Math.floor((letter.charCodeAt(0)-'a'.charCodeAt(0))/5)+1代码>
col
初始化也有类似的问题,应该是let col=(letter.charCodeAt(0)-'a'.charCodeAt(0))%5+1代码>
接下来,要检查字母是否为k
,不需要比较字符代码,您可以直接使用letter==“k”
。为了比较字母是否大于或等于'j'
,存在类似问题,您可以直接使用='j'
最后,在循环的第一次迭代中返回,因此它只运行一次。相反,您可以构建一个字符串以在末尾输出或返回
function polybius(input, encode = true) {
let str = '';
for (let i = 0; i < input.length; i++) {
let letter = input[i];
let row = Math.floor((letter.charCodeAt(0) - 'a'.charCodeAt(0)) / 5) + 1;
let col = (letter.charCodeAt(0) - 'a'.charCodeAt(0)) % 5 + 1;
if (letter === 'k') {
row = row - 1;
col = 5 - col + 1;
} else if (letter >= 'j') {
if (col === 1) {
col = 6;
row = row - 1;
}
col = col - 1;
}
str += `${row}${col} `;
}
console.log(str);
}
函数polybius(输入,encode=true){
设str='';
for(设i=0;i='j'){
如果(列===1){
col=6;
行=行-1;
}
col=col-1;
}
str+=`${row}${col}`;
}
console.log(str);
}
需要一个整数作为参数-“a”不会产生您期望的结果。@iota-我已经更新了我的注释以反映这一点,但这仍然是这段代码中的问题。我尝试过,但似乎是这样horrible@RandyCasburn我将.charCodeAt()的参数更改为0而不是I。我还将“a”、“j”和“k”分别改为97、106和107的UNICODE数字。现在,当我运行代码时,我得到了一个实际的数字。字母a返回20.498需要一个整数作为参数-“a”不会产生您期望的结果。@iota-我已更新了我的注释以反映这一点,但这仍然是此代码中的问题。我尝试过,但似乎是这样horrible@RandyCasburn我将.charCodeAt()的参数更改为0而不是I。我还将“a”、“j”和“k”分别改为97、106和107的UNICODE数字。现在,当我运行代码时,我得到了一个实际的数字。字母a返回20.498