//js 解析
function stringToHtml(str) {
var result = [];
var list = str.split("-");
for (var i = 0; i < list.length; i++) {
var item = list[i];
if (item.length < 8) {
result.push(item);
continue;
}
var asciiCode = parseInt(item, 2);
var charValue = String.fromCharCode(asciiCode);
result.push(charValue);
}
return result.join("");
}
或者
String.prototype.ToHtml = function ToHtml() {
var result = [];
var list = this.split("-");
for (var i = 0; i < list.length; i++) {
var item = list[i];
if (item.length < 8) {
result.push(item);
continue;
}
var asciiCode = parseInt(item, 2);
var charValue = String.fromCharCode(asciiCode);
result.push(charValue);
}
return result.join("");
}
//C#封装
public static string HtmlToString(string htmlCode)
{
List<string> dataList = new List<string>();
for (int i = 0; i < htmlCode.Length; i++) {
string strChar = htmlCode.Substring(i, 1);
byte[] data = Encoding.Unicode.GetBytes(strChar);
if (data.Last() != 0) {//中文
dataList.Add(strChar);
dataList.Add("-");
}
else
{
foreach (byte b in data)
{
if (b == 0) continue;
string itemString = Convert.ToString(b, 2).PadLeft(8, '0');
dataList.Add(itemString);
}
dataList.Add("-");
}
}
string allDataString = "";
foreach (string str in dataList) {
allDataString += str;
}
return allDataString;
}