gpt4 book ai didi

powershell - 在Powershell中分割表情符号序列

转载 作者:行者123 更新时间:2023-12-03 01:26:34 25 4
gpt4 key购买 nike

我有一个仅填充表情符号的文本框。请勿使用任何空格或字符。我需要拆分这些表情符号以便识别它们。这是我尝试过的:

function emoji_to_unicode(){
foreach ($emoji in $textbox.Text) {
$unicode = [System.Text.Encoding]::Unicode.GetBytes($emoji)
Write-Host $unicode
}
}

循环只运行一次,而不是一一打印字节,而是打印所有连接在一起的表情符号的代码。就像所有表情符号都是一个项目一样。我用6个表情符号进行了测试,而不是得到这个:

61216 7222

61216 67222

6121610222

6121628222

6121686220

60216174223

我得到这个:

61 216 7 222 61 216 67 222 61 216 10 222 61 216 28 222 61 216 86 220 60 216 174 223

我想念什么?

最佳答案

字符串只是一个元素。您想要将其更改为字符数组。

foreach ($i in 'hithere') { $i }
hithere

foreach ($i in [char[]]'hithere') { $i }
h
i
t
h
e
r
e
嗯,这行不通。这些代码点相当高,U + 1F600(32位)等
foreach ($i in [char[]]'😀😁😂😃😄😅😆') { $i }       
� # 16 bit surrogate pairs?













好的,添加每对。这是使用 https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates(或仅使用ConvertToUTF32($ emoji,0))完成此操作的另一种方法
$emojis = '😀😁😂😃😄😅😆'
for ($i = 0; $i -lt $emojis.length; $i += 2) {
[System.Char]::IsHighSurrogate($emojis[$i])
0x10000 + ($emojis[$i] - 0xD800) * 0x400 + $emojis[$i+1] - 0xDC00 | % tostring x
# [system.char]::ConvertToUtf32($emojis,$i) | % tostring x # or
$emojis[$i] + $emojis[$i+1]
}


True
1f600
😀
True
1f601
😁
True
1f602
😂
True
1f603
😃
True
1f604
😄
True
1f605
😅
True
1f606
😆
请注意,Unicode.GetBytes()方法调用中的unicode引用了utf16le编码。
中国作品。
[char[]]'嗨,您好'




在这里,它使用utf32编码。所有字符均为4个字节长。将每4个字节转换为int32并将其打印为十六进制。
$emoji = '😀😁😂😃😄😅😆'
$utf32 = [System.Text.Encoding]::utf32.GetBytes($emoji)

for($i = 0; $i -lt $utf32.count; $i += 4) {
$int32 = [bitconverter]::ToInt32($utf32[$i..($i+3)],0)
$int32 | % tostring x
}

1f600
1f601
1f602
1f603
1f604
1f605
1f606
或者从int32转到字符串。简单地将int32转换为 [char]是行不通的(必须添加[char]对)。脚本引用: https://www.powershellgallery.com/packages/Emojis/0.1/Content/Emojis.psm1
for ($i = 0x1f600; $i -le 0x1f606; $i++ ) { [System.Char]::ConvertFromUtf32($i) }

😀
😁
😂
😃
😄
😅
😆
另请参阅 How to encode 32-bit Unicode characters in a PowerShell string literal?

关于powershell - 在Powershell中分割表情符号序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62391665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com