| VB.NET | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 |
Imports System Imports System.Text Module Module1 Sub Main() Dim text As String Dim source() As Byte Dim encoded() As Byte Dim destEncoding As Encoding ' 変換される文字列 text = "ABCあいう亜井宇" ' 文字列をバイト配列に変換 source = Encoding.Unicode.GetBytes(text) ' エンコーディングを取得 (シフトJISコードページ) destEncoding = Encoding.GetEncoding(932) ' コードページをUnicodeからシフトJISに変換 encoded = Encoding.Convert(Encoding.Unicode, destEncoding, source) Console.WriteLine("文字列: {0}", text) Console.WriteLine("文字列の長さ: {0}", text.Length) Console.WriteLine("文字列のバイト長: {0}", encoded.Length) End Sub End Module |
| C# | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 |
using System; using System.Text; namespace CalcStringByteLength { class Class1 { [STAThread] static void Main(string[] args) { string text; byte[] source; byte[] encoded; Encoding destEncoding; // 変換される文字列 text = "ABCあいう亜井宇"; // 文字列をバイト配列に変換 source = Encoding.Unicode.GetBytes( text ); // エンコーディングを取得 (シフトJISコードページ) destEncoding = Encoding.GetEncoding( 932 ); // コードページをUnicodeからシフトJISに変換 encoded = Encoding.Convert( Encoding.Unicode, destEncoding, source ); Console.WriteLine( "文字列: {0}", text ); Console.WriteLine( "文字列の長さ: {0}", text.Length ); Console.WriteLine( "文字列のバイト長: {0}", encoded.Length ); } } } |
| 出力結果 | |
文字列: ABCあいう亜井宇 文字列の長さ: 9 文字列のバイト長: 15 Press any key to continue | |