「Excel VBA 文字列をバイト配列に変換」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==文字列をバイト配列に変換== [Excel VBA]{{category 文字化け}} {{amazon|4798122084}} ===Shift_JISに変換=== ====StringをByteに代入時にstrConv…」) |
|||
| 1行目: | 1行目: | ||
==文字列をバイト配列に変換== | ==文字列をバイト配列に変換== | ||
| − | [Excel VBA]{{category 文字化け}} | + | [[Excel VBA]]{{category 文字化け}} |
{{amazon|4798122084}} | {{amazon|4798122084}} | ||
| 66行目: | 66行目: | ||
b = s.getBytes("Shift_JIS"); | b = s.getBytes("Shift_JIS"); | ||
| − | for (int i=0; i | + | for (int i=0; i<b.length; i++) { |
System.out.format("%X ", b[i]); | System.out.format("%X ", b[i]); | ||
} | } | ||
| 72行目: | 72行目: | ||
b = s.getBytes("UTF-16LE"); | b = s.getBytes("UTF-16LE"); | ||
| − | for (int i=0; i | + | for (int i=0; i<b.length; i++) { |
System.out.format("%X ", b[i]); | System.out.format("%X ", b[i]); | ||
} | } | ||
| 78行目: | 78行目: | ||
} | } | ||
=====結果===== | =====結果===== | ||
| − | + | >java PrintByteTest | |
61 62 63 82 A0 82 A2 82 A4 | 61 62 63 82 A0 82 A2 82 A4 | ||
61 0 62 0 63 0 42 30 44 30 46 30 | 61 0 62 0 63 0 42 30 44 30 46 30 | ||
{{amazon|4798122092}} | {{amazon|4798122092}} | ||
2020年2月15日 (土) 08:02時点における版
目次
文字列をバイト配列に変換
Shift_JISに変換
StringをByteに代入時にstrConvで、Shift_JISに変換
Sub PrintByteTest()
Dim s As String
Dim b() As Byte
s = "abcあいう"
b = StrConv(s, vbFromUnicode)
' Shift_JIS
GoSub print_byte
Exit Sub
print_byte:
Dim i As Integer
For i = 0 To UBound(b)
Debug.Print Hex(b(i)) + " ";
Next
Return
End Sub
結果
call PrintByteTest 61 62 63 82 A0 82 A2 82 A4
Unicode(UTF-16LE)に変換
StringをそのままByteに代入
Sub PrintByteTest()
Dim s As String
Dim b() As Byte
s = "abcあいう"
b = s
' Unicode (UTF-16LE)
GoSub print_byte
Exit Sub
print_byte:
Dim i As Integer
For i = 0 To UBound(b)
Debug.Print Hex(b(i)) + " ";
Next
Return
End Sub
結果
call PrintByteTest 61 0 62 0 63 0 42 30 44 30 46 30
Javaと確認
- Javaでの文字コードの扱い
Shift_JISとUTF-16LEに変換
public class PrintByteTest {
public static void main(String[] args) throws Exception {
String s = "abcあいう";
byte[] b = null;
b = s.getBytes("Shift_JIS");
for (int i=0; i<b.length; i++) {
System.out.format("%X ", b[i]);
}
System.out.println("");
b = s.getBytes("UTF-16LE");
for (int i=0; i<b.length; i++) {
System.out.format("%X ", b[i]);
}
}
}
結果
>java PrintByteTest 61 62 63 82 A0 82 A2 82 A4 61 0 62 0 63 0 42 30 44 30 46 30
© 2006 矢木浩人