0

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

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

我找到了 ROT13 的 JS实现,但我不知道在我的函数中包含在哪里:

{{
_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" );
}
;};
}
;}
4

3 回答 3

2

您可以在 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);});

参考: 我在 JavaScript 中的 rot13 的单行实现哪里出错了?

要传递此变量i,请执行以下操作:

getRot13Input (i.rot13());
于 2015-01-20T16:00:16.110 回答
1

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

//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
于 2017-11-30T06:27:34.797 回答
0

之后添加

_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 函数包含为全局对象的属性。

于 2015-01-21T15:51:49.793 回答