You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
15 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Text;
  6. namespace Apewer.Internals
  7. {
  8. internal class TextConverter
  9. {
  10. /// <summary>将文本以 UTF-8 转换为字节数组。</summary>
  11. public static byte[] ToBinary(string argText)
  12. {
  13. return ToBinary(argText, Encoding.UTF8);
  14. }
  15. /// <summary>将文本以转换为字节数组。</summary>
  16. public static byte[] ToBinary(string argText, Encoding argEncoding)
  17. {
  18. var vnull = Constant.EmptyBytes;
  19. if (string.IsNullOrEmpty(argText)) return vnull;
  20. if (argEncoding == null) return vnull;
  21. try { return argEncoding.GetBytes(argText); }
  22. catch { return vnull; }
  23. }
  24. /// <summary>将字节数组以 UTF-8 转换为文本。</summary>
  25. public static string FromBinary(byte[] argBytes)
  26. {
  27. var vnull = "";
  28. if (argBytes.Length < 1) return vnull;
  29. try { return Encoding.UTF8.GetString(argBytes); }
  30. catch { return vnull; }
  31. }
  32. /// <summary>将字节数组转换为文本。</summary>
  33. public static string FromBinary(byte[] argBytes, Encoding argEncoding)
  34. {
  35. var vnull = "";
  36. if (argBytes.Length < 1) return vnull;
  37. if (argEncoding == null) return vnull;
  38. try { return argEncoding.GetString(argBytes); }
  39. catch { return vnull; }
  40. }
  41. /// <summary>将明文文本以 UTF-8 转换为 Base64 文本。</summary>
  42. public static string ToBase64(string argPlain)
  43. {
  44. var vbytes = ToBinary(argPlain);
  45. var vcipher = BinaryUtility.ToBase64(vbytes);
  46. return vcipher;
  47. }
  48. /// <summary>将 Base64 文本以 UTF-8 转换为明文文本。</summary>
  49. public static string FromBase64(string argCipher)
  50. {
  51. var vbytes = BinaryUtility.FromBase64(argCipher);
  52. var vplain = FromBinary(vbytes);
  53. return vplain;
  54. }
  55. /// <summary>将文本转换为 DataTable 对象。</summary>
  56. public static DataTable ToDataTable(List<string[]> argNestedText)
  57. {
  58. var vtable = new DataTable();
  59. if (argNestedText == null) return vtable;
  60. var vcolumns = 0;
  61. foreach (var vrow in argNestedText)
  62. {
  63. if (vrow == null) continue;
  64. if (vrow.Length > vcolumns) vcolumns = vrow.Length;
  65. for (int i = 0; i < vcolumns; i++) if (vrow[i] == null) vrow[i] = "";
  66. }
  67. for (int i = 0; i < vcolumns; i++) vtable.Columns.Add("Column_" + vtable.Columns.Count.ToString());
  68. foreach (var vrow in argNestedText) vtable.Rows.Add(vrow);
  69. return vtable;
  70. }
  71. /// <summary>将文本转换为 DataTable 对象。</summary>
  72. public static DataTable ToDataTable(List<List<string>> argNestedText)
  73. {
  74. var vtable = new DataTable();
  75. if (argNestedText == null) return vtable;
  76. var vcolumns = 0;
  77. foreach (var vrow in argNestedText)
  78. {
  79. if (vrow == null) continue;
  80. if (vrow.Count > vcolumns) vcolumns = vrow.Count;
  81. for (int i = 0; i < vcolumns; i++) if (vrow[i] == null) vrow[i] = "";
  82. }
  83. for (int i = 0; i < vcolumns; i++) vtable.Columns.Add("Column_" + vtable.Columns.Count.ToString());
  84. foreach (var vrow in argNestedText) vtable.Rows.Add(vrow.ToArray());
  85. return vtable;
  86. }
  87. /// <summary>byte -> plain</summary>
  88. public static string EncodeByte(byte argByte)
  89. {
  90. return Constant.HexCollection[argByte / 16].ToString() + Constant.HexCollection[argByte % 16].ToString();
  91. }
  92. /// <summary>binary -> plain</summary>
  93. public static string EncodeBinary(byte[] argBytes)
  94. {
  95. try
  96. {
  97. int vlength = argBytes.Length;
  98. if (vlength > 0)
  99. {
  100. var vb = new TextBuilder();
  101. for (int i = 0; i < vlength; i++) vb.Append(EncodeByte(argBytes[i]));
  102. return vb.Value;
  103. }
  104. }
  105. finally { }
  106. return "";
  107. }
  108. /// <summary>text -> plain</summary>
  109. public static string EncodeText(string argText, bool argDelimiter = false)
  110. {
  111. if (!string.IsNullOrEmpty(argText))
  112. {
  113. if (argDelimiter)
  114. {
  115. string vcell;
  116. var vb = new TextBuilder();
  117. for (int i = 0; i < argText.Length; i++)
  118. {
  119. vcell = argText.Substring(i, 1);
  120. vcell = EncodeBinary(Encoding.UTF8.GetBytes(vcell));
  121. vb.Append("<" + vcell + ">");
  122. }
  123. return vb.Value;
  124. }
  125. else
  126. {
  127. return EncodeBinary(Encoding.UTF8.GetBytes(argText));
  128. }
  129. }
  130. else return "";
  131. }
  132. /// <summary>plain -> binary</summary>
  133. public static byte[] DecodeBinary(string hex)
  134. {
  135. if (string.IsNullOrEmpty(hex)) return Constant.EmptyBytes;
  136. string vplain = TextHelper.LCase(hex);
  137. byte[] vresult;
  138. if (TextHelper.Len(vplain) >= 2)
  139. {
  140. MemoryStream vmsold = null;
  141. MemoryStream vmsnew = null;
  142. try
  143. {
  144. int vcell;
  145. vmsold = new MemoryStream(Encoding.ASCII.GetBytes(vplain));
  146. vmsnew = new MemoryStream();
  147. long vlength = vmsold.Length;
  148. if ((vlength % 2) == 0)
  149. {
  150. for (int i = 1; i <= (vlength / 2); i++)
  151. {
  152. vcell = GetHex(vmsold.ReadByte()) * 16;
  153. vcell = vcell + GetHex(vmsold.ReadByte());
  154. vmsnew.WriteByte(Convert.ToByte(vcell));
  155. }
  156. }
  157. vresult = vmsnew.ToArray();
  158. }
  159. catch
  160. {
  161. vresult = Constant.EmptyBytes;
  162. }
  163. finally
  164. {
  165. vmsnew.Dispose();
  166. vmsold.Dispose();
  167. }
  168. }
  169. else
  170. {
  171. vresult = Constant.EmptyBytes;
  172. }
  173. return vresult;
  174. }
  175. /// <summary>plain -> text</summary>
  176. public static string DecodeText(string argPlain)
  177. {
  178. if (string.IsNullOrEmpty(argPlain)) return "";
  179. string vplain = TextHelper.LCase(argPlain);
  180. vplain = vplain.Replace("<", "");
  181. vplain = vplain.Replace(">", "");
  182. if (vplain.Length >= 2)
  183. {
  184. byte[] vbytes = DecodeBinary(vplain);
  185. if (vbytes.Length > 0) return Encoding.UTF8.GetString(vbytes);
  186. }
  187. return "";
  188. }
  189. /// <summary>ascii -> hex</summary>
  190. public static byte GetHex(byte argAscii)
  191. {
  192. if ((argAscii >= 48) && (argAscii <= 57)) return Convert.ToByte(argAscii - 48);
  193. if ((argAscii >= 97) && (argAscii <= 122)) return Convert.ToByte(argAscii - 87);
  194. return 0;
  195. }
  196. /// <summary>ascii -> hex</summary>
  197. public static byte GetHex(int argAscii)
  198. {
  199. try
  200. {
  201. if ((argAscii >= 48) && (argAscii <= 57)) return Convert.ToByte(argAscii - 48);
  202. if ((argAscii >= 97) && (argAscii <= 122)) return Convert.ToByte(argAscii - 87);
  203. }
  204. catch { }
  205. return 0;
  206. }
  207. /// <summary>将字节数组格式化为字符串。</summary>
  208. public static string FormatX2(params byte[] argBytes)
  209. {
  210. var sb = new System.Text.StringBuilder();
  211. for (int i = 0; i < argBytes.Length; i++) sb.Append(argBytes[i].ToString("x2"));
  212. return sb.ToString();
  213. }
  214. /// <summary>获取单精度浮点对象。</summary>
  215. public static Single GetSingle(string text)
  216. {
  217. if (!string.IsNullOrEmpty(text))
  218. {
  219. try
  220. {
  221. var t = text.Trim();
  222. t = t.Replace(" ", "").Replace(",", "").Replace(",", "");
  223. t = t.Replace("。", ".");
  224. var p = 0;
  225. while (t.Length > 0 && t.EndsWith("%"))
  226. {
  227. t = t.Substring(0, t.Length - 1);
  228. p += 1;
  229. }
  230. var v = Convert.ToSingle(t);
  231. if (p > 0) v /= Convert.ToSingle(Math.Pow(100D, p));
  232. return v;
  233. // if (TextVerifier.IsNumber(argValue)) return Convert.ToSingle(argValue);
  234. }
  235. catch { }
  236. }
  237. return 0F;
  238. }
  239. /// <summary>获取双精度浮点对象。</summary>
  240. public static Double GetDouble(string text)
  241. {
  242. if (!string.IsNullOrEmpty(text))
  243. {
  244. try
  245. {
  246. var t = text.Trim();
  247. t = t.Replace(" ", "").Replace(",", "").Replace(",", "");
  248. t = t.Replace("。", ".");
  249. var p = 0;
  250. while (t.Length > 0 && t.EndsWith("%"))
  251. {
  252. t = t.Substring(0, t.Length - 1);
  253. p += 1;
  254. }
  255. var v = Convert.ToDouble(t);
  256. if (p > 0) v /= Math.Pow(100D, p);
  257. return v;
  258. // if (TextVerifier.IsNumber(text)) return Convert.ToDouble(text);
  259. }
  260. catch { }
  261. }
  262. return 0D;
  263. }
  264. /// <summary>获取 Decimal 对象。</summary>
  265. public static decimal GetDecimal(string text)
  266. {
  267. if (!string.IsNullOrEmpty(text))
  268. {
  269. try
  270. {
  271. var t = text.Trim();
  272. t = t.Replace(" ", "").Replace(",", "").Replace(",", "");
  273. t = t.Replace("。", ".");
  274. var p = 0;
  275. while (t.Length > 0 && t.EndsWith("%"))
  276. {
  277. t = t.Substring(0, t.Length - 1);
  278. p += 1;
  279. }
  280. var v = Convert.ToDecimal(t);
  281. if (p > 0) v /= Convert.ToDecimal(Math.Pow(100D, p));
  282. return v;
  283. }
  284. catch
  285. {
  286. try
  287. {
  288. var t = text.Trim();
  289. t = t.Replace(" ", "").Replace(",", "").Replace(",", "");
  290. t = t.Replace("。", ".");
  291. var p = 0;
  292. while (t.Length > 0 && t.EndsWith("%"))
  293. {
  294. t = t.Substring(0, t.Length - 1);
  295. p += 1;
  296. }
  297. var v = decimal.Parse(t, System.Globalization.NumberStyles.Float);
  298. if (p > 0) v /= Convert.ToDecimal(Math.Pow(100D, p));
  299. return v;
  300. }
  301. catch { }
  302. }
  303. }
  304. return 0M;
  305. }
  306. /// <summary>获取 Byte 对象。</summary>
  307. public static Byte GetByte(string argValue)
  308. {
  309. if (!string.IsNullOrEmpty(argValue))
  310. {
  311. try
  312. {
  313. if (TextVerifier.IsInteger(argValue))
  314. {
  315. return Convert.ToByte(argValue);
  316. }
  317. }
  318. catch { }
  319. }
  320. return 0;
  321. }
  322. /// <summary>获取 SByte 对象。</summary>
  323. public static SByte GetSByte(string argValue)
  324. {
  325. if (!string.IsNullOrEmpty(argValue))
  326. {
  327. try
  328. {
  329. if (TextVerifier.IsInteger(argValue))
  330. {
  331. return Convert.ToSByte(argValue);
  332. }
  333. }
  334. catch { }
  335. }
  336. return 0;
  337. }
  338. /// <summary>获取 Int16 对象。</summary>
  339. public static Int16 GetInt16(string argValue)
  340. {
  341. if (!string.IsNullOrEmpty(argValue))
  342. {
  343. try
  344. {
  345. if (TextVerifier.IsInteger(argValue))
  346. {
  347. return Convert.ToInt16(argValue);
  348. }
  349. }
  350. catch { }
  351. }
  352. return 0;
  353. }
  354. /// <summary>获取 UInt16 对象。</summary>
  355. public static UInt16 GetUInt16(string argValue)
  356. {
  357. if (!string.IsNullOrEmpty(argValue))
  358. {
  359. try
  360. {
  361. if (TextVerifier.IsInteger(argValue))
  362. {
  363. return Convert.ToUInt16(argValue);
  364. }
  365. }
  366. catch { }
  367. }
  368. return 0;
  369. }
  370. /// <summary>获取 Int32 对象。</summary>
  371. public static Int32 GetInt32(string argValue)
  372. {
  373. if (!string.IsNullOrEmpty(argValue))
  374. {
  375. try
  376. {
  377. if (TextVerifier.IsInteger(argValue))
  378. {
  379. return Convert.ToInt32(argValue);
  380. }
  381. }
  382. catch { }
  383. }
  384. return 0;
  385. }
  386. /// <summary>获取 UInt32 对象。</summary>
  387. public static UInt32 GetUInt32(string argValue)
  388. {
  389. if (!string.IsNullOrEmpty(argValue))
  390. {
  391. try
  392. {
  393. if (TextVerifier.IsInteger(argValue))
  394. {
  395. return Convert.ToUInt32(argValue);
  396. }
  397. }
  398. catch { }
  399. }
  400. return 0;
  401. }
  402. /// <summary>获取 Int64 对象。</summary>
  403. public static Int64 GetInt64(string argValue)
  404. {
  405. if (!string.IsNullOrEmpty(argValue))
  406. {
  407. try
  408. {
  409. if (TextVerifier.IsInteger(argValue))
  410. {
  411. return Convert.ToInt64(argValue);
  412. }
  413. }
  414. catch { }
  415. }
  416. return 0;
  417. }
  418. /// <summary>获取 UInt64 对象。</summary>
  419. public static UInt64 GetUInt64(string argValue)
  420. {
  421. if (!string.IsNullOrEmpty(argValue))
  422. {
  423. try
  424. {
  425. if (TextVerifier.IsInteger(argValue))
  426. {
  427. return Convert.ToUInt64(argValue);
  428. }
  429. }
  430. catch { }
  431. }
  432. return 0;
  433. }
  434. }
  435. }