简体   繁体   English

编码info_hash以用于请求torrent跟踪器

[英]Encoding info_hash for request torrent tracker

I would like to interact with a torrent tracker in Node.js. 我想与Node.js中的torrent跟踪器进行交互。 So I calculate the info_hash and I get the good one : 7cd350e5a70f0a61593e636543f9fc670ffa8a4d 因此,我计算出了info_hash并得到了一个好的结果: 7cd350e5a70f0a61593e636543f9fc670ffa8a4d

But to be send to the tracker it have to be urlencoded. 但是要发送到跟踪器,必须对其进行urlencoded。 The problem is I don't know how to do it to get the right encoded value which is %7c%d3P%e5%a7%0f%0aaY%3eceC%f9%fcg%0f%fa%8aM 问题是我不知道该怎么做才能获得正确的编码值,即%7c%d3P%e5%a7%0f%0aaY%3eceC%f9%fcg%0f%fa%8aM

I doesn't know how to get this value, because it doesn't follow the %nn format 我不知道如何获取此值,因为它不遵循%nn格式

I tried with encodeURIComponent and querystring, but I didn't get the right string. 我尝试使用encodeURIComponent和querystring,但是没有得到正确的字符串。

Here's one solution that matches the expected output (including capitalization): 这是一个与预期输出(包括大写字母)匹配的解决方案:

var h = '7cd350e5a70f0a61593e636543f9fc670ffa8a4d';
h = h.replace(/.{2}/g, function(m) {
  var v = parseInt(m, 16);
  if (v <= 127) {
    m = encodeURIComponent(String.fromCharCode(v));
    if (m[0] === '%')
      m = m.toLowerCase();
  } else
    m = '%' + m;
  return m;
});
console.log(h);

// outputs: %7c%d3P%e5%a7%0f%0aaY%3eceC%f9%fcg%0f%fa%8aM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM