Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created July 20, 2012 05:41
Show Gist options
  • Save masaru-b-cl/3148904 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/3148904 to your computer and use it in GitHub Desktop.
文字列のbyte[]をエンコーディングを指定して取得する拡張メソッド
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);
}
}
}
}
@masaru-b-cl
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment