{"id":248,"date":"2021-07-27T13:50:30","date_gmt":"2021-07-27T05:50:30","guid":{"rendered":"https:\/\/aisumura.net\/blog\/?p=248"},"modified":"2021-07-27T13:50:32","modified_gmt":"2021-07-27T05:50:32","slug":"c-hex-string%e8%88%87ascii%e7%9a%84%e4%ba%92%e7%9b%b8%e8%bd%89%e6%8f%9b","status":"publish","type":"post","link":"https:\/\/aisumura.net\/blog\/2021\/07\/27\/c-hex-string%e8%88%87ascii%e7%9a%84%e4%ba%92%e7%9b%b8%e8%bd%89%e6%8f%9b\/","title":{"rendered":"C# Hex string\u8207ASCII\u7684\u4e92\u76f8\u8f49\u63db"},"content":{"rendered":"\n<p>\u4eca\u5929\u78b0\u5230\u70ba\u4e86\u6e1b\u5c11\u901a\u8a0a\u5c01\u5305Size\uff0c<br>\u628aASCII\u585e\u5728hex string\u7684case\uff0c<br>\u4e0a\u7db2\u627e\u4e86\u4e00\u6bb5\u6642\u9593\uff0c\u767c\u73fe\u6c92\u6709\u73fe\u6210\u7684Code\uff0c\u5c31\u96a8\u624b\u7d00\u9304\u4e0b\u4f86\u4e86\u3002<\/p>\n\n\n\n<h4>\u8cc7\u6599\u683c\u5f0f<\/h4>\n\n\n\n<p>HEX string: \u4e00\u9023\u4e3216\u9032\u4f4d\u6578\u5b57\u7d44\u6210\u7684\u5b57\u4e32\uff0c\u5169\u500b\u6578\u5b57\u70ba\u4e00\u500b\u55ae\u4f4d\uff0c\u5c0d\u61c9\u4e00\u500bASCII char\u3002<br>e.g. <br>&#8220;30&#8221;(hex string) =&gt; 48(dec) =&gt; &#8220;0&#8221;(ASCII)<br>&#8220;464C&#8221;(hex string) = &gt; 70 76(dec) =&gt; &#8220;FL&#8221;(ASCII)<\/p>\n\n\n\n<h4>\u8f49\u63db\u51fd\u6578<\/h4>\n\n\n\n<p>1.HexStr2ASCII &#8211; \u5c07hex string\u8f49\u6210\u5c0d\u61c9\u7684ASCII string<br>2.ASCII2HexStr &#8211; \u524d\u8005\u7684\u53cd\u51fd\u6578<br>3.ASCII2HexByte &#8211; \u4e0a\u9762\u7684\u51fd\u6578\u6539\u7528bytes\u56de\u50b3(\u65b9\u4fbf\u901a\u8a0a\u754c\u9762\u8abf\u7528)<\/p>\n\n\n\n<h4>C# Code<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">\/\/\u8f38\u51652 Bytes Hex\u578b\u5f0f\u7684\u5b57\u4e32\uff0c\u56de\u50b3ASCII\u5b57\u4e32\n private string HexStr2ASCII(string hex_str) {\n     string result = \"\";\n     string tmp;\n\n     for(int i = 0; i &lt; hex_str.Length; i += 2){\n         tmp = hex_str.Substring(i, 2);\n         result += Convert.ToChar(Convert.ToUInt32(tmp, 16));\n     }\n     return result;\n }\n\n \/\/\u8f38\u5165ASCII\u5b57\u4e32 \u56de\u50b32 Bytes Hex\u578b\u5f0f\u7684\u5b57\u4e32\n private string ASCII2HexStr(string str){\n     string result = \"\";\n\n     for(int i = 0; i &lt; str.Length; i++){\n         result += Convert.ToInt32(str[i]).ToString(\"X\");\n     }\n     return result;\n }\n\n \/\/\u8f38\u5165ASCII\u5b57\u4e32 \u56de\u50b32 Bytes Hex\u578b\u5f0f\u7684Bytes array\n private byte[] ASCII2HexByte(string str){\n     List result = new List();\n\n     for(int i = 0; i &lt; str.Length; i++){\n         byte tmp = (byte)str[i];\n         result.Add(tmp);\n     }\n     return result.ToArray();\n }<\/code><\/pre>\n\n\n\n<h4>\u7d50\u679c\u9a57\u8b49<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">Debug.WriteLine(\"HexStr2ASCII(\\\"464C\\\"): \" + HexStr2ASCII(\"464C\"));\nDebug.WriteLine(\"ASCII2HexStr(\\\"FL\\\"): \" + ASCII2HexStr(\"FL\"));\nbyte[] hex_bytes = ASCII2HexByte(\"FL\");\nstring hex_string = \"\";\nforeach(byte b in hex_bytes){\n    hex_string += \"0x\" + b.ToString(\"X\") + \" \";\n}\nDebug.WriteLine(\"ASCII2HexByte(\\\"FL\\\"): \" + hex_string.TrimEnd());<\/code><\/pre>\n\n\n\n<p>HexStr2ASCII(&#8220;464C&#8221;): FL<br>ASCII2HexStr(&#8220;FL&#8221;): 464C<br>ASCII2HexByte(&#8220;FL&#8221;): 0x46 0x4C<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u78b0\u5230\u70ba\u4e86\u6e1b\u5c11\u901a\u8a0a\u5c01\u5305Size\uff0c\u628aASCII\u585e\u5728hex string\u7684case\uff0c\u4e0a\u7db2\u627e\u4e86\u4e00\u6bb5\u6642\u9593\uff0c\u767c\u73fe\u6c92\u6709\u73fe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[23],"_links":{"self":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/248"}],"collection":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":8,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/248\/revisions\/256"}],"wp:attachment":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}