• <tfoot id='hGlo4'></tfoot>

  • <small id='hGlo4'></small><noframes id='hGlo4'>

    <i id='hGlo4'><tr id='hGlo4'><dt id='hGlo4'><q id='hGlo4'><span id='hGlo4'><b id='hGlo4'><form id='hGlo4'><ins id='hGlo4'></ins><ul id='hGlo4'></ul><sub id='hGlo4'></sub></form><legend id='hGlo4'></legend><bdo id='hGlo4'><pre id='hGlo4'><center id='hGlo4'></center></pre></bdo></b><th id='hGlo4'></th></span></q></dt></tr></i><div id='hGlo4'><tfoot id='hGlo4'></tfoot><dl id='hGlo4'><fieldset id='hGlo4'></fieldset></dl></div>

    <legend id='hGlo4'><style id='hGlo4'><dir id='hGlo4'><q id='hGlo4'></q></dir></style></legend>

      • <bdo id='hGlo4'></bdo><ul id='hGlo4'></ul>
      1. javascript中的字符到十六进制

        时间:2023-08-07

        <small id='vEo3q'></small><noframes id='vEo3q'>

          1. <tfoot id='vEo3q'></tfoot>
            1. <i id='vEo3q'><tr id='vEo3q'><dt id='vEo3q'><q id='vEo3q'><span id='vEo3q'><b id='vEo3q'><form id='vEo3q'><ins id='vEo3q'></ins><ul id='vEo3q'></ul><sub id='vEo3q'></sub></form><legend id='vEo3q'></legend><bdo id='vEo3q'><pre id='vEo3q'><center id='vEo3q'></center></pre></bdo></b><th id='vEo3q'></th></span></q></dt></tr></i><div id='vEo3q'><tfoot id='vEo3q'></tfoot><dl id='vEo3q'><fieldset id='vEo3q'></fieldset></dl></div>

                  <tbody id='vEo3q'></tbody>
                  <bdo id='vEo3q'></bdo><ul id='vEo3q'></ul>
                  <legend id='vEo3q'><style id='vEo3q'><dir id='vEo3q'><q id='vEo3q'></q></dir></style></legend>
                  本文介绍了javascript中的字符到十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  谁能指导我如何在javascript中将char转换为十六进制?

                  例如:

                  Could anyone guide me on how to convert char to hex in javascript?

                  For example:

                  "入力されたデータは范囲外です."

                  "u5165u529Bu3055u308Cu305Fu30C7u30FCu30BFu306Fu7BC4u56F2u5916u3067u3059u3002"

                  "入力されたデータは範囲外です。"
                  to
                  "u5165u529Bu3055u308Cu305Fu30C7u30FCu30BFu306Fu7BC4u56F2u5916u3067u3059u3002"

                  这个网站做到了

                  但是我想不通.

                  任何建议.

                  谢谢,萨博坦

                  推荐答案

                  您可以遍历字符并使用 charCodeAt 函数获取它们的 UTF-16 值,然后用它们构造一个字符串.

                  You can loop through the characters and use the charCodeAt function to get their UTF-16 values, then constructing a string with them.

                  这是我构建的一些代码,它比您链接的网站上的代码要好得多,并且应该更容易理解:

                  Here's some code I constructed that is much better than the code on the site you've linked, and should be easier to understand:

                  function string_as_unicode_escape(input) {
                      function pad_four(input) {
                          var l = input.length;
                          if (l == 0) return '0000';
                          if (l == 1) return '000' + input;
                          if (l == 2) return '00' + input;
                          if (l == 3) return '0' + input;
                          return input;
                      }
                      var output = '';
                      for (var i = 0, l = input.length; i < l; i++)
                          output += '\u' + pad_four(input.charCodeAt(i).toString(16));
                      return output;
                  }
                  

                  让我们分解一下.

                  1. string_as_unicode_escape 采用一个参数,input,它是一个字符串.
                  2. pad_four 是一个做一件事的内部函数;它用前导 '0' 字符填充字符串,直到长度至少为四个字符.
                  3. 首先将 output 定义为空字符串.
                  4. 对于字符串中的每个字符,将 u 附加到 output 字符串.用 input.charCodeAt(i) 取字符的 UTF-16 值,然后用 .toString(16) 将其转换为十六进制字符串,然后用前导填充零,然后将结果附加到 output 字符串.
                  5. 返回输出字符串.
                  1. string_as_unicode_escape takes one argument, input, which is a string.
                  2. pad_four is an internal function that does one thing; it pads strings with leading '0' characters until the length is at least four characters long.
                  3. Start off by defining output as an empty string.
                  4. For each character in the string, append u to the output string. Take the UTF-16 value of the character with input.charCodeAt(i), then convert it to a hexadecimal string with .toString(16), then pad it with leading zeros, then append the result to the output string.
                  5. Return the output string.

                  正如 Tim Down 所说,我们还可以将 0x10000 添加到 charCodeAt 值,然后添加 .slice(1) 调用产生的字符串.toString(16),实现填充效果.

                  As Tim Down commented, we can also add 0x10000 to the charCodeAt value and then .slice(1) the string resulting from calling .toString(16), to achieve the padding effect.

                  这篇关于javascript中的字符到十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:英文字母中哪个字母占用的像素最多? 下一篇:检查字符串中的第一个字符

                  相关文章

                  最新文章

                  1. <i id='weQhn'><tr id='weQhn'><dt id='weQhn'><q id='weQhn'><span id='weQhn'><b id='weQhn'><form id='weQhn'><ins id='weQhn'></ins><ul id='weQhn'></ul><sub id='weQhn'></sub></form><legend id='weQhn'></legend><bdo id='weQhn'><pre id='weQhn'><center id='weQhn'></center></pre></bdo></b><th id='weQhn'></th></span></q></dt></tr></i><div id='weQhn'><tfoot id='weQhn'></tfoot><dl id='weQhn'><fieldset id='weQhn'></fieldset></dl></div>

                    1. <legend id='weQhn'><style id='weQhn'><dir id='weQhn'><q id='weQhn'></q></dir></style></legend>

                    2. <tfoot id='weQhn'></tfoot>

                      <small id='weQhn'></small><noframes id='weQhn'>

                      • <bdo id='weQhn'></bdo><ul id='weQhn'></ul>