Преглед изворни кода

增加了两个扩展类,方便日志输出string的hex格式

tanghai пре 13 година
родитељ
комит
e3e47c0fe4

+ 24 - 0
CSharp/Platform/Helper/ByteHelper.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Helper
+{
+	public static class ByteHelper
+	{
+		public static string ToHex(this byte b)
+		{
+		    return b.ToString("X2");
+		}
+		
+		public static string ToHex(this IEnumerable<byte> bytes)
+		{
+		    var stringBuilder = new StringBuilder();
+			foreach (byte b in bytes)
+			{
+				stringBuilder.Append(b.ToString("X2"));
+			}
+			return stringBuilder.ToString();
+		}
+	}
+}

+ 2 - 0
CSharp/Platform/Helper/Helper.csproj

@@ -35,7 +35,9 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="ByteHelper.cs" />
     <Compile Include="LoaderHelper.cs" />
+    <Compile Include="StringHelper.cs" />
     <Compile Include="XmlHelper.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 20 - 0
CSharp/Platform/Helper/StringHelper.cs

@@ -0,0 +1,20 @@
+using System.Collections.Generic;
+using System.Text;
+
+namespace Helper
+{
+	public static class StringHelper
+	{
+		public static IEnumerable<byte> ToBytes(this string str)
+		{
+			byte[] byteArray = Encoding.Default.GetBytes(str);
+			return byteArray;
+		}
+
+		public static string ToHex(this string str)
+		{
+			IEnumerable<byte> byteArray = str.ToBytes();
+			return byteArray.ToHex();
+		}
+	}
+}