将字符串化的数字解析回数字。十六进制

时间:2018-05-24 23:39:39

标签: javascript math hex discord

        var color = Math.floor(Math.random() * 16777215).toString(16);
        var hex = Number.parseInt(color, 16);
        console.log(hex);

        message.channel.send({embed: {
            color: hex, //This is the place I attempt to implement it. It doesn't accept strings.
            author: {
          name: message.author.username + "'s Profile",
          icon_url: message.author.avatarURL
        },
        description: ":white_small_square: **About:** " + message.author.bio,
        fields: [{
            name: "Client Info",
            value: ":white_small_square: **User ID** - " + "`" + message.member.id + "`" + "\n:white_small_square: **Status** - " + "`" + message.author.presence.status.toUpperCase() + "`" + " | ***Is*** " + occupation
          },
          {
            name: "Server Info",
            value: ":white_small_square: **Joined at** - "  /*+ "`" + new Date(guild.detailsOfUser(message.author.user).joinedAt).toUTCString() + "`" */+ "\n:white_small_square: **Avowed Roles** - " + ranks
          }
        ],
        timestamp: new Date(),
        footer: {
          text: message.author.username,
        }
      }
    });
    }

我一直在四处寻找并找到一个论坛帖子,其中一组人在一行中成功创建了十六进制生成器。这完全没问题,但是,我不希望它是一个字符串。我试图在行之后解析Int,但它只是返回“未定义”,并且当放入数字的某个位置时它表示它不是整数。如果有人对如何将其解析为Int有任何想法,请告诉我。

编辑:在我解释为什么要关闭此功能之前,感谢所有帮助我尝试修复此问题的人。我只是在试验并意识到.toString(16)从未真正需要,并且结果数字是有效的颜色。再次感谢大家帮助我

1 个答案:

答案 0 :(得分:1)

您可以使用noted here

来反转该过程
parseInt(hex, 16);

或者:

Number.parseInt(hex, 16);

这会将其更改回您可以存储的int