gpt4 book ai didi

c# - 转换为 UCS2

转载 作者:行者123 更新时间:2023-12-02 02:08:15 25 4
gpt4 key购买 nike

Vb.net(或 C#)中是否有任何函数可以对 UCS2 中的字符串进行编码?

谢谢

最佳答案

使用以下函数将 unicode 字符串编码为“UCS2”格式:

    //================> Used to encoding GSM message as UCS2
public static String UnicodeStr2HexStr(String strMessage)
{
byte[] ba = Encoding.BigEndianUnicode.GetBytes(strMessage);
String strHex = BitConverter.ToString(ba);
strHex = strHex.Replace("-", "");
return strHex;
}

public static String HexStr2UnicodeStr(String strHex)
{
byte[] ba = HexStr2HexBytes(strHex);
return HexBytes2UnicodeStr(ba);
}

//================> Used to decoding GSM UCS2 message
public static String HexBytes2UnicodeStr(byte[] ba)
{
var strMessage = Encoding.BigEndianUnicode.GetString(ba, 0, ba.Length);
return strMessage;
}

public static byte[] HexStr2HexBytes(String strHex)
{
strHex = strHex.Replace(" ", "");
int nNumberChars = strHex.Length / 2;
byte[] aBytes = new byte[nNumberChars];
using (var sr = new StringReader(strHex))
{
for (int i = 0; i < nNumberChars; i++)
aBytes[i] = Convert.ToByte(new String(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
}
return aBytes;
}

例如:

String strE = SmsEngine.UnicodeStr2HexStr("سلام به گچپژ پارسي");
// strE = "0633064406270645002006280647002006AF0686067E06980020067E062706310633064A"
String strD = SmsEngine.HexStr2UnicodeStr("0633064406270645002006280647002006AF0686067E06980020067E062706310633064A");
// strD = "سلام به گچپژ پارسي"

关于c# - 转换为 UCS2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3438340/

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