如何获取Java中字符的Unicode代码点? [英] How to get the Unicode code point for a character in Javascript?

查看:161
本文介绍了如何获取Java中字符的Unicode代码点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用条形码扫描仪来读取我的网站上的条形码(该网站是在OpenUI5中创建的。)

I'm using a barcode scanner to read a barcode on my website (the website is made in OpenUI5).

扫描仪的工作原理就像是一个键盘,可以输入它读取的字符。在键入的结尾和开头,它使用特殊字符。对于每种类型的扫描仪,这些字符都不同。

The scanner works like a keyboard that types the characters it reads. At the end and the beginning of the typing it uses a special character. These characters are different for every type of scanner.

某些可能的字符是:






在我的代码中,我使用 if(oModelScanner.oData.scanning& oEvent.key == \u2584)检查扫描仪的输入是否为。

In my code I use if (oModelScanner.oData.scanning && oEvent.key == "\u2584") to check if the input from the scanner is ▄.

有没有办法从\uHHHH样式的那个字符中获取代码? (HHHH是字符的十六进制代码)

Is there any way to get the code from that character in the \uHHHH style? (with the HHHH being the hexadecimal code for the character)

我尝试了 charCodeAt ,但这会返回十进制代码。

I tried the charCodeAt but this returns the decimal code.

使用 codePointAt示例,它们将我需要的代码转换为十进制代码,所以我需要与此相反。 / p>

With the codePointAt examples they make the code I need into a decimal code so I need a reverse of this.

推荐答案

JavaScript字符串具有方法 codePointAt 会为您提供以10为底的给定符号的整数值,然后,如果您希望将整数格式化为四个十六进制数字,则需要将其转换为以16为底的整数(十六进制)序列(如Nikolay Spasov的回应)。

Javascript strings have a method codePointAt which gives you the integer value of a given symbol in base 10, you need then to convert your integer in base 16 (hexadecimal) if you wish to format the integer into a four hexadecimal digits sequence (as in the response of Nikolay Spasov).

var hex = "▄".codePointAt(0).toString(16);
var result = "\\u" + "0000".substring(0, 4 - hex.length) + hex;

但是,直接检查关键码点整数是否与预期代码匹配可能更容易点

However it would probably be easier for you to check directly if you key code point integer match the expected code point

oEvent.key.codePointAt(0) === '▄'.codePointAt(0);

请注意,符号相等实际上可能比较棘手:某些符号由代理对定义(您可以

Note that "symbol equality" can actually be trickier: some symbols are defined by surrogate pairs (you can see it as the combination of two halves defined as four hexadecimal digits sequence).

出于这个原因,我建议使用专门的库。将其视为定义为四个十六进制数字序列的两半的组合。

For this reason I would recommend to use a specialized library.

您将在非常相关的内容中找到更多详细信息Mathias Bynens的文章

这篇关于如何获取Java中字符的Unicode代码点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆