#author("2020-05-09T23:14:01+09:00","","") #navi(../) * 符号付バイトsbyte,符号なしbyteをintegerに変換する [#ac359db3] byte, sbyte, byte[], sbyte[] を integer に変換するサンプルコードを C#, Visual Basic で紹介ます。~ Convert.ToInt32, byte[].GetValue(index), sbyte[].GetValue(index)の使い方になります。 #contents * 関連サイト [#sbf7c674] -[[Microsoft .NET | Convert.ToInt32 メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.convert.toint32]] -[[Microsoft .NET | Array.GetValue メソッド>https://docs.microsoft.com/ja-jp/dotnet/api/system.array.getvalue]] #htmlinsert(windev-top.html) * 動作確認環境 [#pc9874fa] -Windows 10 ver.1909 -Visual Studio 2019 -.NET Core -コンソールアプリケーション * byte, sbyte -> integer 変換サンプルコード [#kbb17e6c] 以下にbyte, sbyte を integer に変換するサンプルコードを紹介します。~ 符号なし、符号付き、符号なしの配列、符号付きの配列を integer に変換するサンプルコードになります。 ** C# [#uac1953a] #ref(Program.cs) using System; class Program { static void Main(string[] args) { int i; // signed byte Console.WriteLine("*** signed byte ***"); Console.WriteLine("-- System.Convert.ToInt32() --"); sbyte sb; sb = 123; i = Convert.ToInt32(sb); Console.WriteLine(i); sb = -123; i = Convert.ToInt32(sb); Console.WriteLine(i); sbyte[] sb_ary = { 10, -20, 30, -40 }; Console.WriteLine("-- foreach and System.Convert.ToInt32() --"); foreach (sbyte s in sb_ary) { Console.WriteLine(Convert.ToInt32(s)); } Console.WriteLine("-- foreach and sbyte[].GetValue(index) --"); for (int j = 0; j < sb_ary.Length; j++) { Console.WriteLine(sb_ary.GetValue(j)); } // unsigned byte Console.WriteLine("*** unsigned byte ***"); Console.WriteLine("-- System.Convert.ToInt32() --"); byte b; b = 200; i = Convert.ToInt32(b); Console.WriteLine(i); byte[] b_ary = { 0, 10, 100, 200, 255 }; Console.WriteLine("-- foreach and System.Convert.ToInt32() --"); foreach (byte ub in b_ary) { Console.WriteLine(Convert.ToInt32(ub)); } Console.WriteLine("-- foreach and byte[].GetValue(index) --"); for (int k = 0; k < b_ary.Length; k++) { Console.WriteLine(b_ary.GetValue(k)); } } } ** Visual Basic [#w5c2307d] #ref(Program.vb) Imports System Module Program Sub Main(args As String()) Dim i As Integer Console.WriteLine("*** signed byte ***") Console.WriteLine("-- System.Convert.ToInt32() --") Dim sb As SByte sb = 123 i = Convert.ToInt32(sb) Console.WriteLine(i) sb = -123 i = Convert.ToInt32(sb) Console.WriteLine(i) Dim sb_ary As SByte() = {10, -20, 30, -40} Console.WriteLine("-- foreach and System.Convert.ToInt32() --") For Each s As SByte In sb_ary Console.WriteLine(Convert.ToInt32(s)) Next Console.WriteLine("-- foreach and sbyte[].GetValue(index) --") For j As Integer = 0 To sb_ary.Length - 1 Console.WriteLine(sb_ary.GetValue(j)) Next Console.WriteLine("*** unsigned byte ***") Console.WriteLine("-- System.Convert.ToInt32() --") Dim b As Byte b = 200 i = Convert.ToInt32(b) Console.WriteLine(i) Dim b_ary As Byte() = {0, 10, 100, 200, 255} Console.WriteLine("-- foreach and System.Convert.ToInt32() --") For Each ub As Byte In b_ary Console.WriteLine(Convert.ToInt32(ub)) Next Console.WriteLine("-- foreach and byte[].GetValue(index) --") For k As Integer = 0 To b_ary.Length - 1 Console.WriteLine(b_ary.GetValue(k)) Next End Sub End Module ** 実行結果 [#y4e2320e] 上記のサンプルコードを実行した時のキャプチャになります。 #ref(run.png) 以上、byte, sbyte, byte[], sbyte[] を integer に変換するサンプルコードでした。 #htmlinsert(windev-btm.html)