javascript - 尝试使用 Discord 机器人将用户消息从十进制转换为十六进制

标签 javascript converters discord

好的,我有这个

Commands.hex = {
  name: 'hex',
  help: "I'll convert your steamid for you",
  timeout: 10,
  level: 0,
  fn: function (msg, suffix) {
    msg.reply('Steam64').then((m) => {
      m.edit('<@' + msg.author.id + '>, Steam ID Converted: ' + suffix.toString(16)).slice(-2).toUpperCase()
    })
  }
}

我正在尝试让它做这个网站所做的事情 http://www.binaryhexconverter.com/decimal-to-hex-converter

picture of what it's doing

我只是想将其转换为我的不和谐机器人,谢谢!

最佳答案

事实证明,您的值:76561198262006743 远大于 Number.MAX_SAFE_VALUE,即 (2^53 -1)。这将导致在尝试进行算术运算时出现未定义的行为。

例如,在 Google Chrome 上,当仅在控制台中输入数字并按 Enter 键时,该值变为 76561198262006740(注意末尾的 3 变为 0),这意味着十六进制转换也不正确。

一个简单的解决方案是,由于您已经拥有字符串形式的值,因此可以执行您自己的十进制 -> 十六进制转换。

他可以在这里找到这样的算法: https://stackoverflow.com/a/21668344/8237835

Number#toString(16)dec2hex(String) 结果的小比较:

function dec2hex(str){ // .toString(16) only works up to 2^53
    var dec = str.toString().split(''), sum = [], hex = [], i, s
    while(dec.length){
        s = 1 * dec.shift()
        for(i = 0; s || i < sum.length; i++){
            s += (sum[i] || 0) * 10
            sum[i] = s % 16
            s = (s - sum[i]) / 16
        }
    }
    while(sum.length){
        hex.push(sum.pop().toString(16))
    }
    return hex.join('')
}

num = "76561198262006743"

console.log("dec2Hex: " + dec2hex(num))

console.log("toString: " + parseInt(num).toString(16))

关于javascript - 尝试使用 Discord 机器人将用户消息从十进制转换为十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47144223/

相关文章:

javascript - 在折线图中动态显示数据 - ChartJS

javascript - 为什么我的 ajax.beginform 操作调用的部分 View 没有出现在页面上?

jsf - selectOneMenu 在提交后始终显示列表中的最后一项作为所选项目

WPF 绑定(bind)错误 Datagrid 转换器 SelectedItems

python - Discord 机器人无法使用 Discord.py 向用户添加角色

javascript - Raphael 图形在 IE8 中不显示

javascript - 如何使用 google maps api v3 旋转 map 方向

javascript - 在 View 面板 View 列中使用链接时数字转换器不工作

javascript - Discord.JS - 使用角色将 Discord 中的用户移动到您的 VC

python-3.x - 如何同时使用discord bot命令和事件?