【问题标题】:Implement ROT13 in JavaScript [duplicate]在 JavaScript 中实现 ROT13 [重复]
【发布时间】:2015-01-20 15:57:40
【问题描述】:

我有一个 JS 函数可以按原样读取变量 i。我想传递这个用 ROT13 编码的变量,所以我的函数首先必须解码变量然后继续。

问题是如何让JS解码并使用它。

我为 ROT13 找到了一个 JS implementation,但我不知道将其包含在我的函数中的哪个位置:

{{
_inst.gmlimgp=parameter_string( 1 );
_inst.gmlsp=string_pos( "i=", _inst.gmlimgp );
if ((_inst.gmlsp!=0)) {{
_inst.gmlsp+=2;
_inst.gmlimgp=string_copy( _inst.gmlimgp, _inst.gmlsp, string_length( _inst.gmlimgp ) );
g_pBuiltIn.background_index[1]=3;
background_replace( g_pBuiltIn.background_index[1], _inst.gmlimgp, 0, 0 );
_inst.set_sprite_index( (-4) );
}
;}
 else {{
show_message( "invalid parameter" );
}
;};
}
;}

【问题讨论】:

  • 这看起来不像 ROT13! :(
  • 我无法理解您的问题文本如何应用于您提供的代码。似乎没有任何函数读取名为 i 的变量(除非 "i=" 在某些时候传递给 eval?如果是这样,那完全不清楚你在这里给出了什么)。您应该清楚地确定给您带来困难的功能/线路,并展示您为使其工作所做的任何尝试(以及您遇到的错误,如果有的话)。
  • 我编辑了问题以使其更清晰(希望如此)。

标签: javascript rot13


【解决方案1】:

您可以在 JavaScript 中将其用作 ROT13:

错误1

<script>
String.prototype.rot13 = rot13 = function(s)
 {
    return (s = (s) ? s : this).split('').map(function(_)
     {
        if (!_.match(/[A-Za-z]/)) return _;
        c = Math.floor(_.charCodeAt(0) / 97);
        k = (_.toLowerCase().charCodeAt(0) - 96) % 26 + 13;
        return String.fromCharCode(k + ((c == 0) ? 64 : 96));
     }).join('');
 };
</script>

或更短的版本:

s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});

参考:Where is my one-line implementation of rot13 in JavaScript going wrong?

要传递这个变量i,你可以这样做:

getRot13Input (i.rot13());

【讨论】:

  • 我粘贴的代码按原样读取变量i - 即直接。它不传递它,它不编码它。但是,我会将变量 i 传递给使用 ROT13 编码的 JS,并正在寻找一种方法让我的 JS 首先对其进行解码,然后继续原样。
  • 我预计这会被否决,因为 OP 在构建有效的 ROT13 实现方面存在任何困难并不明显。看来问题在于对问题中提供的代码应用 ROT13 函数。我不明白 如何 OP 希望将 ROT13 应用于问题中的代码,所以我无法回答,但似乎这个答案解决了一个独立于 OP 问题的问题.
  • @apsillers 我仍然很困惑。但是好吧,可能是,我会尝试接受我错了,直到反对者回答。 :)
  • 代码失败 'Bacon'.rot13() 在应该是 'Onpba' 时出现在 'Onp|{' 中。
  • @georgeawg 好吧。但它是 4 年前发布的。
【解决方案2】:

这是一个简单的解决方案:

//This function take rot13 encoded string and decoded it as simple string.
function rot13(str) { 
  var arr = str.split('');
  var newArray = [];

 var first = {
  'A' : 'N',
  'B' : 'O',
  'C' : 'P',
  'D' : 'Q',
  'E' : 'R',
  'F' : 'S',
  'G' : 'T',
  'H' : 'U',
  'I' : 'V',
  'J' : 'W',
  'K' : 'X',
  'L' : 'Y',
  'M' : 'Z'
 };

var rest = {
'N' : 'A',
'O' : 'B',
'P' : 'C',
'Q' : 'D',
'R' : 'E',
'S' : 'F',
'T' : 'G',
'U' : 'H',
'V' : 'I',
'W' : 'J',
'X' : 'K',
'Y' : 'L',
'Z' : 'M'
};

// Iteration though the string array(arr)
for(var i = 0; i <= arr.length; i++){

  if (first.hasOwnProperty(arr[i])){       //checking first obj has the element or not

    newArray.push(first[arr[i]]);           //Pushing the element to the nerarray

  } else if(rest.hasOwnProperty(arr[i])){   //checking rest obj has the element or not

    newArray.push(rest[arr[i]]);
  } else {
    newArray.push(arr[i]);
  }
}
return newArray.join('');
}


rot13("SERR PBQR PNZC");  //call the function with rot13 encoded string

【讨论】:

    【解决方案3】:

    添加后

    _inst.gmlimgp=string_copy( _inst.gmlimgp, _inst.gmlsp, string_length( _inst.gmlimgp ) );
    

    但之前

    g_pBuiltIn.background_index[1]=3;
    

    像这样调用 str_rot13 的行

    _inst.gmlimgp= global.str_rot13(_inst.glomgp); 
    

    PS 我假设您将 str_rot13 函数包含为全局对象的属性。

    【讨论】:

    • PS i assume that you include the str_rot13 functin as a property of global object. - 你能详细说明一下吗?
    猜你喜欢
    • 2021-11-19
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多