我就廢話不多說了,大家還是直接看**吧~
///
/// 字串轉unicode
///
/// 源字串
/// unicode編碼後的字串
public static string string2unicode(string source)
", bytes[i + 1], bytes[i]);
}return stringbuilder.tostring();
} ///
/// 字串轉為unicode碼字串
///
///
///
public static string stringtounicode(string s)
", buffer[1], buffer[0]));
}return sb.tostring();
} ///
/// unicode字串轉為正常字串
///
///
///
public static string unicodetostring(string srctext)
return dst;
}補充:c# unicode string 轉換 codepoint
c# 的string和stringbuilder都支援使用codepoint直接構造字串。unicode的字串形式一般都是'\u1234'這種轉義模式。 其中『1234'就是unicode codepoint的16進製制形式。
通過計算,可以把這種形式的字串,直接轉化為int32型別的codepoint。
///
/// get the unicode code point
///
private static int getunicodecodepoint(char c1, char c2, char c3, char c4)
///
/// single unicode char convert to int
///
private static int unicodechartoint(char c)
throw new exception(string.format("unicode char '' error", c));
}使用的時候codepoint需要強轉為char型別。比如:
stringbuilder sb = new stringbuilder();
sb.append((char) getunicodecodepoint(c1, c2, c3, c4));
本文標題: c# 字串與unicode互相轉換實戰案例
本文位址:
C 字串與unicode互相轉換
普通字串與unicode字串間轉換 public static class stringtounicode return sb.tostring unicode字串轉為正常字串 public static string unicodetostring string srctext return ds...
Ansi 與 Unicode 字串型別的互相轉換
widechartomultibyte 實現寬位元組轉換到窄位元組 multibytetowidechar 實現窄位元組轉換到寬位元組 widechartomultibyte 的 頁用來標記與新轉換的字串相關的 頁 multibytetowidechar 的 頁用來標記與乙個多位元組字串相關的 頁,...
原始字串 Unicode
原始字串以r開頭,可以在原始字串中放入任何字元,但不能以反斜線 結尾,用來防止反斜線轉義!在普通字串中,反斜線 會轉義,在字串中通常不能直接加入的內容。如 print c nowhere c owhere要輸出c nowhere,必須加反斜槓轉義。print c nowhere c nowhere ...