ConvertHelper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /** 1. 功能:处理数据类型转换,数制转换、编码转换相关的类
  2. * 2. 作者:周兆坤
  3. * 3. 创建日期:2010-3-19
  4. * 4. 最后修改日期:2010-3-19
  5. **/
  6. using System;
  7. using System.Text;
  8. namespace Ant.Service.Utilities
  9. {
  10. /// <summary>
  11. /// 处理数据类型转换,数制转换、编码转换相关的类
  12. /// </summary>
  13. public sealed class ConvertHelper
  14. {
  15. #region 补足位数
  16. /// <summary>
  17. /// 指定字符串的固定长度,如果字符串小于固定长度,
  18. /// 则在字符串的前面补足零,可设置的固定长度最大为9位
  19. /// </summary>
  20. /// <param name="text">原始字符串</param>
  21. /// <param name="limitedLength">字符串的固定长度</param>
  22. public static string RepairZero(string text, int limitedLength)
  23. {
  24. //补足0的字符串
  25. string temp = "";
  26. //补足0
  27. for (int i = 0; i < limitedLength - text.Length; i++)
  28. {
  29. temp += "0";
  30. }
  31. //连接text
  32. temp += text;
  33. //返回补足0的字符串
  34. return temp;
  35. }
  36. #endregion
  37. #region 各进制数间转换
  38. /// <summary>
  39. /// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
  40. /// </summary>
  41. /// <param name="value">要转换的值,即原值</param>
  42. /// <param name="from">原值的进制,只能是2,8,10,16四个值。</param>
  43. /// <param name="to">要转换到的目标进制,只能是2,8,10,16四个值。</param>
  44. public static string ConvertBase(string value, int from, int to)
  45. {
  46. try
  47. {
  48. int intValue = Convert.ToInt32(value, from); //先转成10进制
  49. string result = Convert.ToString(intValue, to); //再转成目标进制
  50. if (to == 2)
  51. {
  52. int resultLength = result.Length; //获取二进制的长度
  53. switch (resultLength)
  54. {
  55. case 7:
  56. result = "0" + result;
  57. break;
  58. case 6:
  59. result = "00" + result;
  60. break;
  61. case 5:
  62. result = "000" + result;
  63. break;
  64. case 4:
  65. result = "0000" + result;
  66. break;
  67. case 3:
  68. result = "00000" + result;
  69. break;
  70. }
  71. }
  72. return result;
  73. }
  74. catch
  75. {
  76. //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
  77. return "0";
  78. }
  79. }
  80. #endregion
  81. #region 使用指定字符集将string转换成byte[]
  82. /// <summary>
  83. /// 使用指定字符集将string转换成byte[]
  84. /// </summary>
  85. /// <param name="text">要转换的字符串</param>
  86. /// <param name="encoding">字符编码</param>
  87. public static byte[] StringToBytes(string text, Encoding encoding)
  88. {
  89. return encoding.GetBytes(text);
  90. }
  91. #endregion
  92. #region 使用指定字符集将byte[]转换成string
  93. /// <summary>
  94. /// 使用指定字符集将byte[]转换成string
  95. /// </summary>
  96. /// <param name="bytes">要转换的字节数组</param>
  97. /// <param name="encoding">字符编码</param>
  98. public static string BytesToString(byte[] bytes, Encoding encoding)
  99. {
  100. return encoding.GetString(bytes);
  101. }
  102. #endregion
  103. #region 将byte[]转换成int
  104. /// <summary>
  105. /// 将byte[]转换成int
  106. /// </summary>
  107. /// <param name="data">需要转换成整数的byte数组</param>
  108. public static int BytesToInt32(byte[] data)
  109. {
  110. //如果传入的字节数组长度小于4,则返回0
  111. if (data.Length < 4)
  112. {
  113. return 0;
  114. }
  115. //定义要返回的整数
  116. int num = 0;
  117. //如果传入的字节数组长度大于4,需要进行处理
  118. if (data.Length >= 4)
  119. {
  120. //创建一个临时缓冲区
  121. byte[] tempBuffer = new byte[4];
  122. //将传入的字节数组的前4个字节复制到临时缓冲区
  123. Buffer.BlockCopy(data, 0, tempBuffer, 0, 4);
  124. //将临时缓冲区的值转换成整数,并赋给num
  125. num = BitConverter.ToInt32(tempBuffer, 0);
  126. }
  127. //返回整数
  128. return num;
  129. }
  130. #endregion
  131. }
  132. }