如何将Unicode用十六进制转换为飞镖 /扑朔迷离

发布于 2025-01-26 13:16:21 字数 120 浏览 7 评论 0原文

%U0BB5%U0BA3%U0B95%U0BCD%U0B95%U0BAE%U0BAE%U0BCD

上方是unicode,带有十六进制字符字符串 需要将其转换为可读文本 解码时,以上文本将返回வணக்கம்表示欢迎

%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD

Above is unicode with hex characters string
Need to convert that to readable text
When decoded, the above text will return வணக்கம் meaning welcome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2025-02-02 13:16:21

如果您想要一个硬编码的字符串,如 flutter中的特殊字符
“ nofollow noreferrer”> the dart语言之旅 ,您可以使用\ u指定Unicode代码点:

var welcome = '\u0BB5\u0BA3\u0B95\u0BCD\u0B95\u0BAE\u0BCD';

如果给出了字符串'%U0BB5%U0BA3%U0B95%U0B95%U0BCD%U0B95%U0B95%U0BAE%U0BCD'要在运行时动态转换,您将需要:

  1. 将字符串分为%uxxxx组件。
  2. 解析XXXX部分作为十六进制整数以获取代码点。
  3. 从代码点构造字符串
void main() {
  var s = '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD';
  var re = RegExp(r'%u(?<codePoint>[0-9A-Fa-f]{4})');
  var matches = re.allMatches(s);
  var codePoints = [
    for (var match in matches)
      int.parse(match.namedGroup('codePoint')!, radix: 16),
  ];
  var decoded = String.fromCharCodes(codePoints);
  print(decoded); // Prints: வணக்கம்
}

编辑1

调整后的版本,可以用编码的代码点和未编码的字符来处理字符串:

void main() {
  var s = '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD'
      ' hello world! '
      '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD';
  var re = RegExp(r'(%u(?<codePoint>[0-9A-Fa-f]{4}))|.');
  var matches = re.allMatches(s);
  var codePoints = <int>[];
  for (var match in matches) {
    var codePoint = match.namedGroup('codePoint');
    if (codePoint != null) {
      codePoints.add(int.parse(codePoint, radix: 16));
    } else {
      codePoints += match.group(0)!.runes.toList();
    }
  }
  var decoded = String.fromCharCodes(codePoints);
  print(decoded); // Prints: வணக்கம் hello world! வணக்கம்
}

编辑2

上述版本假定您的输入仅由Unicode代码点组成,该点数编码为>%uhhhh(其中H,其中H是十六进制的数字)和原始的ASCII字符。但是,您的这个问题的新版本表示您实际上需要处理:Unicode

  • 代码点:unicode代码点编码为> %uhhhh
  • RAW(未编码)ASCII字符。
  • ASCII字符编码为%HH

要处理第三种情况:

void main() {
  var s = '%3Cp%3E%3Cb%3E%u0B87%u0BA8%u0BCD%u0BA4%u0BBF%u0BAF%u0BBE%u0BB5%u0BBF%u0BA9%u0BCD%20%u0BAA%u0BC6%u0BB0%u0BC1%u0BAE%u0BCD%u0BAA%u0BBE%u0BA9%u0BCD%u0BAE%u0BC8%u0BAF%u0BBE%u0BA9%20%u0BAE%u0B95%u0BCD%u0B95%u0BB3%u0BCD%20%u0BAA%u0BB4%u0B99%u0BCD%u0B95%u0BBE%u0BB2%u0BA4%u0BCD%u0BA4%u0BBF%u0BB2%u0BBF%u0BB0%u0BC1%u0BA8%u0BCD%u0BA4%u0BC7%20.........%20%u0BAA%u0BCB%u0BA9%u0BCD%u0BB1%u0BC1%20%u0BA4%u0BBE%u0BA9%u0BBF%u0BAF%u0B99%u0BCD%u0B95%u0BB3%u0BC8%20%u0BAE%u0BC1%u0B95%u0BCD%u0B95%u0BBF%u0BAF%20%u0B89%u0BA3%u0BB5%u0BBE%u0B95%u0BAA%u0BCD%20%u0BAA%u0BAF%u0BA9%u0BCD%u0BAA%u0B9F%u0BC1%u0BA4%u0BCD%u0BA4%u0BBF%u0BA9%u0BB0%u0BCD.%3C/b%3E%0A%3Col%20type%3D%22I%22%20style%3D%22font-weight%3Abold%3B%22%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0B85%u0BB0%u0BBF%u0B9A%u0BBF%3C/span%3E%3C/li%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0B95%u0BC7%u0BB4%u0BCD%u0BB5%u0BB0%u0B95%u0BC1%20%3C/span%3E%3C/li%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0B93%u0B9F%u0BCD%u0BB8%u0BCD%3C/span%3E%3C/li%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0BAA%u0BB0%u0BC1%u0BAA%u0BCD%u0BAA%u0BC1%3C/span%3E%3C/li%3E%3C/ol%3E%3C/p%3E';
  var re = RegExp(
    r'(%(?<asciiValue>[0-9A-Fa-f]{2}))'
    r'|(%u(?<codePoint>[0-9A-Fa-f]{4}))'
    r'|.',
  );
  var matches = re.allMatches(s);
  var codePoints = <int>[];
  for (var match in matches) {
    var codePoint = match.namedGroup('asciiValue') ?? match.namedGroup('codePoint');
    if (codePoint != null) {
      codePoints.add(int.parse(codePoint, radix: 16));
    } else {
      codePoints += match.group(0)!.runes.toList();
    }
  }
  var decoded = String.fromCharCodes(codePoints);
  print(decoded);
}

打印:

<p><b>இந்தியாவின் பெரும்பான்மையான மக்கள் பழங்காலத்திலிருந்தே ......... போன்று தானியங்களை முக்கிய உணவாகப் பயன்படுத்தினர்.</b>
<ol type="I" style="font-weight:bold;">
<li><span style="font-weight:normal;"> அரிசி</span></li>
<li><span style="font-weight:normal;"> கேழ்வரகு </span></li>
<li><span style="font-weight:normal;"> ஓட்ஸ்</span></li>
<li><span style="font-weight:normal;"> பருப்பு</span></li></ol></p>

有些软件包可以渲染html(例如代码> 以及其他各种)。否则,我将考虑与HTML打交道以超出此答案的范围,无论如何,这应该应得的问题。

If you want a hard-coded string, as noted in Special characters in Flutter
and in the Dart Language Tour, you can use \u to specify Unicode code points:

var welcome = '\u0BB5\u0BA3\u0B95\u0BCD\u0B95\u0BAE\u0BCD';

If you are given a string '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD' and need to convert it dynamically at runtime, then you will need to:

  1. Split the string into %uXXXX components.
  2. Parse the XXXX portion as a hexadecimal integer to get the code point.
  3. Construct a String from the code points.
void main() {
  var s = '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD';
  var re = RegExp(r'%u(?<codePoint>[0-9A-Fa-f]{4})');
  var matches = re.allMatches(s);
  var codePoints = [
    for (var match in matches)
      int.parse(match.namedGroup('codePoint')!, radix: 16),
  ];
  var decoded = String.fromCharCodes(codePoints);
  print(decoded); // Prints: வணக்கம்
}

Edit 1

An adjusted version that can handle strings with a mixture of encoded code points and unencoded characters:

void main() {
  var s = '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD'
      ' hello world! '
      '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD';
  var re = RegExp(r'(%u(?<codePoint>[0-9A-Fa-f]{4}))|.');
  var matches = re.allMatches(s);
  var codePoints = <int>[];
  for (var match in matches) {
    var codePoint = match.namedGroup('codePoint');
    if (codePoint != null) {
      codePoints.add(int.parse(codePoint, radix: 16));
    } else {
      codePoints += match.group(0)!.runes.toList();
    }
  }
  var decoded = String.fromCharCodes(codePoints);
  print(decoded); // Prints: வணக்கம் hello world! வணக்கம்
}

Edit 2

The versions above assumed that your input would consist only of Unicode code points encoded as %uHHHH (where H is a hexadecimal digit) and of raw ASCII characters. However, your new version of this question indicates that you actually need to handle a mixture of:

  • Unicode code points encoded as %uHHHH.
  • Raw (unencoded) ASCII characters.
  • ASCII characters encoded as a %HH.

To handle that third case:

void main() {
  var s = '%3Cp%3E%3Cb%3E%u0B87%u0BA8%u0BCD%u0BA4%u0BBF%u0BAF%u0BBE%u0BB5%u0BBF%u0BA9%u0BCD%20%u0BAA%u0BC6%u0BB0%u0BC1%u0BAE%u0BCD%u0BAA%u0BBE%u0BA9%u0BCD%u0BAE%u0BC8%u0BAF%u0BBE%u0BA9%20%u0BAE%u0B95%u0BCD%u0B95%u0BB3%u0BCD%20%u0BAA%u0BB4%u0B99%u0BCD%u0B95%u0BBE%u0BB2%u0BA4%u0BCD%u0BA4%u0BBF%u0BB2%u0BBF%u0BB0%u0BC1%u0BA8%u0BCD%u0BA4%u0BC7%20.........%20%u0BAA%u0BCB%u0BA9%u0BCD%u0BB1%u0BC1%20%u0BA4%u0BBE%u0BA9%u0BBF%u0BAF%u0B99%u0BCD%u0B95%u0BB3%u0BC8%20%u0BAE%u0BC1%u0B95%u0BCD%u0B95%u0BBF%u0BAF%20%u0B89%u0BA3%u0BB5%u0BBE%u0B95%u0BAA%u0BCD%20%u0BAA%u0BAF%u0BA9%u0BCD%u0BAA%u0B9F%u0BC1%u0BA4%u0BCD%u0BA4%u0BBF%u0BA9%u0BB0%u0BCD.%3C/b%3E%0A%3Col%20type%3D%22I%22%20style%3D%22font-weight%3Abold%3B%22%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0B85%u0BB0%u0BBF%u0B9A%u0BBF%3C/span%3E%3C/li%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0B95%u0BC7%u0BB4%u0BCD%u0BB5%u0BB0%u0B95%u0BC1%20%3C/span%3E%3C/li%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0B93%u0B9F%u0BCD%u0BB8%u0BCD%3C/span%3E%3C/li%3E%0A%3Cli%3E%3Cspan%20style%3D%22font-weight%3Anormal%3B%22%3E%20%u0BAA%u0BB0%u0BC1%u0BAA%u0BCD%u0BAA%u0BC1%3C/span%3E%3C/li%3E%3C/ol%3E%3C/p%3E';
  var re = RegExp(
    r'(%(?<asciiValue>[0-9A-Fa-f]{2}))'
    r'|(%u(?<codePoint>[0-9A-Fa-f]{4}))'
    r'|.',
  );
  var matches = re.allMatches(s);
  var codePoints = <int>[];
  for (var match in matches) {
    var codePoint = match.namedGroup('asciiValue') ?? match.namedGroup('codePoint');
    if (codePoint != null) {
      codePoints.add(int.parse(codePoint, radix: 16));
    } else {
      codePoints += match.group(0)!.runes.toList();
    }
  }
  var decoded = String.fromCharCodes(codePoints);
  print(decoded);
}

which prints:

<p><b>இந்தியாவின் பெரும்பான்மையான மக்கள் பழங்காலத்திலிருந்தே ......... போன்று தானியங்களை முக்கிய உணவாகப் பயன்படுத்தினர்.</b>
<ol type="I" style="font-weight:bold;">
<li><span style="font-weight:normal;"> அரிசி</span></li>
<li><span style="font-weight:normal;"> கேழ்வரகு </span></li>
<li><span style="font-weight:normal;"> ஓட்ஸ்</span></li>
<li><span style="font-weight:normal;"> பருப்பு</span></li></ol></p>

There are packages that can render HTML (e.g. package:flutter_html and probably various others). Otherwise I'm going to consider dealing with the HTML to be outside the scope of this answer, and that would deserve its own question anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文