简体   繁体   English

尝试在不加密的情况下将字符串转换为十六进制日志字符串

[英]Attempts to convert string to hex log string without encryption

I'm trying to make a text box which will give an output of a string in hex.我正在尝试制作一个文本框,它将给出一个十六进制字符串的 output 。 There is a similar question here which I got my current code from which I modified a bit, but when I logged it, instead of the string "486578", I got "Hex". 这里有一个类似的问题,我得到了我当前的代码,我从中进行了一些修改,但是当我记录它时,我得到的不是字符串“486578”,而是“Hex”。 Here is my code:这是我的代码:

 function Hex() { var Hex = "Hex" Hex = Hex.toString('16'); console.log(Hex) } Hex();

And no, I don't want something like encrypt.js.不,我不想要 encrypt.js 之类的东西。 How would I solve this?我将如何解决这个问题? Thanks!谢谢!

You are not calling Number.prototype.toString() but String.prototype.toString() .您不是在调用Number.prototype.toString()而是调用String.prototype.toString()

Your input is the string "Hex".您的输入是字符串“Hex”。 This gets converted to a string "Hex".这将转换为字符串“Hex”。 You should try to use a number, if you want to convert a number.如果要转换数字,则应尝试使用数字。

In order to get numbers, you have to encode your string.为了获得数字,您必须对字符串进行编码。

 var encoded = new TextEncoder().encode("Hex"); var hex = Object.values(encoded).map(n => n.toString(16)); console.log (hex.join (' '));

Compare it with the ASCII table .将其与ASCII 表进行比较。

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

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