{"id":1181,"date":"2014-05-16T04:18:04","date_gmt":"2014-05-16T04:18:04","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1181"},"modified":"2014-05-16T04:18:04","modified_gmt":"2014-05-16T04:18:04","slug":"c-dechex-%e8%bd%89%e6%8f%9b%e7%82%ba-bytes-%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1181\/","title":{"rendered":"C# Dec\/Hex \u8f49\u63db\u70ba Bytes \u7684\u65b9\u6cd5."},"content":{"rendered":"<p>\u5f9e Decimal \u76f4\u63a5\u8f49\u63db\u70ba Bytes.<\/p>\n<pre class=\"brush:csharp\">\/\/ using System.Collections.Generic;\r\nint _num = 2550;\r\nList&lt;byte&gt; _list = new List&lt;byte&gt;();\r\nwhile( _num &gt; 0 )\r\n{\t\/\/ do while number more then 0\r\n\t_list.Add( (byte)(_num &amp; 0x00ff) );\t\/\/ Byte mask : number &gt; 255 will ignore.\r\n\t_num &gt;&gt;= 8;\t\/\/ move 8 digit right in binary\r\n}\r\n_list.Reverse();\t\/\/ we calculate form EOL, remember? reverse it.\r\nreturn _list.ToArray(); \/\/ output as Byte array 09,F6<\/pre>\n<p>\u5f9e Hexadecimal \u8f49\u63db\u70ba Bytes<\/p>\n<pre class=\"brush:csharp\">public static byte[] ToByte(string _hexString, int _digit=2)\r\n{\r\n\t\/\/ make sure the _hexString's length is matching the byte format, group by 2\r\n\tint _no = (Mathf.FloorToInt(_hexString.Length\/_digit)*_digit) + ((_hexString.Length%_digit)*_digit);\r\n\t_hexString = _hexString.PadLeft(_no,'0');\r\n\r\n\tbyte[] _bytes = new byte[_hexString.Length\/_digit];\r\n\tfor (int i = 0; i &lt; _bytes.Length; i++)\r\n\t{\r\n\t\t\/\/ only taken target hex number range\r\n\t\tstring _hexByte = _hexString.Substring(i * _digit, _digit);\r\n\t\t\/\/ Convert \"_hexByte\" to bytes.\r\n\t\t_bytes[i] = byte.Parse(_hexByte, System.Globalization.NumberStyles.HexNumber);\r\n\t}\r\n\treturn _bytes;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f9e Decimal \u76f4\u63a5\u8f49\u63db\u70ba Bytes. \/\/ using System.Collections &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[16,43],"class_list":["post-1181","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c-2","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1181"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1181\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}