Created
July 20, 2012 05:41
-
-
Save masaru-b-cl/3148904 to your computer and use it in GitHub Desktop.
文字列のbyte[]をエンコーディングを指定して取得する拡張メソッド
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
public static class StringExtensions | |
{ | |
public static byte[] ToShiftJISBytes(this string value) | |
{ | |
return ToBytes(value, Encoding.GetEncoding("Shift-JIS")); | |
} | |
public static byte[] ToBytes(this string value, Encoding encoding) | |
{ | |
return encoding.GetBytes(value); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
foreach (var item in "GIF87a".ToShiftJISBytes()) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.WriteLine(); | |
foreach (var item in "GIF87a".ToBytes(Encoding.GetEncoding("UTF-16"))) | |
{ | |
Console.WriteLine(item); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twitter.com/hidori/status/226191697237929985
https://twitter.com/ishisaka/status/226192137321058304
という指摘を頂いたので直しました。