{"id":2311,"date":"2018-06-11T18:30:13","date_gmt":"2018-06-11T09:30:13","guid":{"rendered":"http:\/\/okamurax.com\/?p=2311"},"modified":"2021-01-08T00:21:55","modified_gmt":"2021-01-07T15:21:55","slug":"%e3%82%a8%e3%83%b3%e3%82%b3%e3%83%bc%e3%83%89%e3%82%aa%e3%83%96%e3%82%b8%e3%82%a7%e3%82%af%e3%83%88%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2311","title":{"rendered":"C# Encoding\/Byte\/Stream\/IO \u30e1\u30e2"},"content":{"rendered":"<p>\u899a\u3048\u3066\u3089\u308c\u306a\u3044\u5185\u5bb9\u3002\u5c11\u3057\u305a\u3064\u307e\u3068\u3081\u308b\u3002<\/p>\n<h2>Encoding<\/h2>\n<p>\u30fb\u30e1\u30bd\u30c3\u30c9<br \/>\n<span style=\"color: #0000ff;\">System.Text.Encoding.GetEncoding(&#8220;utf-8&#8221;)<\/span><\/p>\n<p>\u30fb\u9759\u7684\u30d7\u30ed\u30d1\u30c6\u30a3<br \/>\n<span style=\"color: #0000ff;\">System.Text.Encoding.UTF8<\/span><\/p>\n<p>\u30fb\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf<br \/>\n<span style=\"color: #0000ff;\">new System.Text.UTF8Encoding(false)<\/span><br \/>\nASCII\u3068Unicode\u306f\u3001Encoding\u30af\u30e9\u30b9\u304b\u3089\u6d3e\u751f\u3057\u305f\u5c02\u7528\u30af\u30e9\u30b9\u304c\u3042\u308a\u3001false\u306b\u3059\u308b\u3068BOM\u7121\u3057\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<h2>Byte<\/h2>\n<p>\u30fbString -&gt; Byte[]<br \/>\n<span style=\"color: #0000ff;\">byte[] bytes = Encoding.UTF8.GetBytes(string);<\/span><\/p>\n<p>\u30fbByte[] -&gt; String<br \/>\n<span style=\"color: #0000ff;\">string text = System.Text.Encoding.UTF8.GetString(bytes);<\/span><\/p>\n<p>\u30fbByte[] -&gt; StreamReader -&gt; String<br \/>\n<span style=\"color: #0000ff;\">var r = new System.IO.StreamReader(bytes, Encoding.UTF8);<\/span><br \/>\n<span style=\"color: #0000ff;\">r.ReadToEnd();<\/span><\/p>\n<pre class=\"lang:c# decode:true \">InitializeComponent();\r\n\r\nstring source = \"\u3042\u3044\u3046\u3048\u304a\";\r\n\r\n\/\/\u6587\u5b57\u5217 -&gt; \u30d0\u30a4\u30c8\u914d\u5217\r\nbyte[] bytes = Encoding.UTF8.GetBytes(source);\r\n\r\n\/\/\u30d0\u30a4\u30c8\u914d\u5217 -&gt; \u6587\u5b57\u5217\r\nstring str = Encoding.UTF8.GetString(bytes);\r\nSystem.Diagnostics.Debug.Print(str); \/\/\u3042\u3044\u3046\u3048\u304a\r\n\r\n\/\/\u30d0\u30a4\u30c8\u914d\u5217 -&gt; \u30d0\u30a4\u30c8\u6587\u5b57\u5217\r\nstring byteString1 = BitConverter.ToString(bytes);\r\nSystem.Diagnostics.Debug.Print(byteString1); \/\/E3-81-82-E3-81-84-E3-81-86-E3-81-88-E3-81-8A\r\n\r\n\/\/\u30d0\u30a4\u30c8\u6587\u5b57\u5217(\u30cf\u30a4\u30d5\u30f3\u9664\u304f)\r\nstring byteString2 = BitConverter.ToString(bytes).Replace(\"-\",\"\");\r\nSystem.Diagnostics.Debug.Print(byteString2); \/\/E38182E38184E38186E38188E3818A\r\n\r\n\/\/\u30d0\u30a4\u30c8\u6587\u5b57\u5217 -&gt; \u30d0\u30a4\u30c8\u914d\u5217 -&gt; \u6587\u5b57\u5217\r\nList&lt;byte&gt; b = new List&lt;byte&gt;();\r\nfor (int i = 0; i &lt; byteString2.Length; i += 2)\r\n{\r\nb.Add(byte.Parse(byteString2.Substring(i, 2),\r\n    System.Globalization.NumberStyles.AllowHexSpecifier));\r\n}\r\nbyte[] bb = b.ToArray();\r\n\r\nstring str2 = Encoding.UTF8.GetString(bb);\r\nSystem.Diagnostics.Debug.Print(str2); \/\/\u3042\u3044\u3046\u3048\u304a<\/pre>\n<h2>Stream\/Reader\/Writer<\/h2>\n<h2>TextReader\u7cfb<\/h2>\n<p>\u30a8\u30f3\u30b3\u30fc\u30c9\u6307\u5b9a\u3057\u3001\u6587\u5b57\u306e\u5165\u51fa\u529b\u304c\u57fa\u672c\u3002<br \/>\nTextReader\u7cfb\u306f\u3001ReadToEnd\u3084ReadLine\u304c\u4f7f\u3048\u308b\u3002<br \/>\n\u9806\u6b21\u30a2\u30af\u30bb\u30b9seek\u304c\u4f7f\u3048\u306a\u3044\u3002<\/p>\n<p><span style=\"color: #0000ff;\">System.IO.StreamReader<\/span><br \/>\n<span style=\"color: #0000ff;\">System.IO.StreamWriter<br \/>\n<\/span><span style=\"color: #0000ff;\">System.IO.StringReader<\/span><br \/>\n<span style=\"color: #0000ff;\">System.IO.StringWriter<\/span><\/p>\n<h2>Stream\u7cfb<\/h2>\n<p>\u30d0\u30a4\u30c8\u306e\u5165\u51fa\u529b\u304c\u57fa\u672c\u3002<br \/>\n\u57fa\u672cseek\u3067\u304d\u308b\u3002<\/p>\n<p><span style=\"color: #0000ff;\">System.IO.FileStream<\/span><br \/>\n<span style=\"color: #0000ff;\">System.IO.MemoryStream<\/span><br \/>\n<span style=\"color: #0000ff;\">System.IO.BufferedStream<\/span><\/p>\n<pre class=\"lang:c# decode:true \">byte[] byteString = Encoding.UTF8.GetBytes(\"Hello World !!!\");\r\n\r\nvar ms = new System.IO.MemoryStream();\r\n\r\nms.Write(byteString,0,byteString.Length);\r\n\/\/ MemoryStream\u306b\u66f8\u304d\u8fbc\u307f\r\n\r\nvar buf1 = ms.ToArray();\r\n\/\/ MemoryStream\u3092\u30d0\u30a4\u30c8\u914d\u5217\u3078\r\n\r\nMessageBox.Show(Encoding.UTF8.GetString(buf1));\r\n\r\nms.Position = 0;\r\n\r\nbyte[] buf2 = new byte[byteString.Length]; \/\/ Length = 15\r\nms.Read(buf2,0,byteString.Length);\r\n\/\/ MemoryStream\u3092\u30d0\u30a4\u30c8\u914d\u5217\u3078\r\n\r\nMessageBox.Show(Encoding.UTF8.GetString(buf2));<\/pre>\n<h2>TextReader\u3068Stream<\/h2>\n<p>\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u6587\u5b57\u3092\u8aad\u3080\u3068\u304d\u3001FileStream\u3068StreamReader\u304c\u3042\u308b\u3002FileStream\u306f\u30e9\u30f3\u30c0\u30e0\u30a2\u30af\u30bb\u30b9\u3067seek\u304c\u4f7f\u3048\u308b\u3002<\/p>\n<p>Stream\u306e\u578b\u3092\u53d7\u3051\u4ed8\u3051\u308b\u3068\u3053\u308d\u3067\u306fTextReader\u306f\u4f7f\u3048\u306a\u3044\u3002<\/p>\n<p>\u30fb\u4f8b(XmlSerializer -&gt; MemoryStream -&gt; Byte[] -&gt; String)<\/p>\n<pre class=\"lang:c# decode:true\">System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(HogeClass));\r\n\r\nusing (System.IO.MemoryStream ms = new System.IO.MemoryStream())\r\n{\r\nx.Serialize(ms, HogeClassInstance);\r\nstring str= Encoding.UTF8.GetString(ms.ToArray());\r\n\/\/ ms.ToArray()\u3067MemoryStream\u304b\u3089Byte[]\u3092\u8aad\u3080\u3002Postion=0\u306e\u5fc5\u8981\u306a\u3057\u3002\r\nMessageBox.Show(str);\r\n}<\/pre>\n<h2>\u30d5\u30a1\u30a4\u30eb\/\u30d5\u30a9\u30eb\u30c0<\/h2>\n<p>\u30fb\u30d5\u30a9\u30eb\u30c0\u4e00\u89a7<br \/>\n<span style=\"color: #0000ff;\">Directory.GetDirectories<br \/>\nDirectory.EnumerateDirectories<br \/>\n<\/span><\/p>\n<p>\u30fb\u30d5\u30a1\u30a4\u30eb\u4e00\u89a7<br \/>\n<span style=\"color: #0000ff;\">Directory.GetFiles<br \/>\nDirectory.EnumerateFiles<br \/>\n<\/span><\/p>\n<p>\u30fb\u30d5\u30a1\u30a4\u30eb\u540d<br \/>\n<span style=\"color: #0000ff;\">Path.GetFileName<\/span><br \/>\n<span style=\"color: #0000ff;\">Path.GetFileNameWithoutExtension<\/span><\/p>\n<p>\u30fb\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d<br \/>\n<span style=\"color: #0000ff;\">Path.GetDirectoryName<\/span><\/p>\n<p>\u30fb\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u66f8\u304d(Reader\/Writer\u306e\u30e9\u30c3\u30d7)<br \/>\n<span style=\"color: #0000ff;\">File.WriteAllLines<\/span><br \/>\n<span style=\"color: #0000ff;\">File.ReadAllLines<\/span><br \/>\n<span style=\"color: #0000ff;\">File.WriteAllText<\/span><br \/>\n<span style=\"color: #0000ff;\">File.ReadAllText<\/span><\/p>\n<p>\u30fb\u5b58\u5728\u78ba\u8a8d<br \/>\n<span style=\"color: #0000ff;\">File.Exists<\/span><br \/>\n<span style=\"color: #0000ff;\">Directory.Exists<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u899a\u3048\u3066\u3089\u308c\u306a\u3044\u5185\u5bb9\u3002\u5c11\u3057\u305a\u3064\u307e\u3068\u3081\u308b\u3002 Encoding \u30fb\u30e1\u30bd\u30c3\u30c9 System.Text.Encoding.GetEncoding(&#8220;utf-8&#8221;) \u30fb\u9759\u7684\u30d7\u30ed\u30d1\u30c6\u30a3 System.Text. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2311\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Encoding\/Byte\/Stream\/IO \u30e1\u30e2&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-2311","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2311"}],"version-history":[{"count":20,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2311\/revisions"}],"predecessor-version":[{"id":4149,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2311\/revisions\/4149"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}