• ベストアンサー

バイナリの高速読み取り

vb2005でバイナリデータを16進数で読み込みたいのですが、 Dim br As New System.IO.FileStream("フルパス",IO.FileMode.Open, IO.FileAccess.Read) Dim strbyte As String For I As Integer = 0 To CType(br.Length, Integer) - 1 strbyte = Hex(br.ReadByte).ToString If strbyte.Length = 1 Then strbyte = "0" & strbyte End If TextBox1.Text = TextBox1.Text & " " & strbyte Application.DoEvents() Next とか、他に Dim arByte() As Byte = My.Computer.FileSystem.ReadAllBytes("フルパス") Dim obyte As Byte Dim strbyte As String For Each obyte In arByte strbyte = Hex(obyte).ToString If strbyte.Length = 1 Then strbyte = "0" & strbyte End If TextBox1.Text = TextBox1.Text & " " & strbyte Application.DoEvents() Next なんかも試してみたのですが、ぜんぜん読み込みが遅くて困っています。 なんか高速にバイナリデータを16進数で読み込む方法がありましたら教えてください。(一般のバイナリエディタ並に)

質問者が選んだベストアンサー

  • ベストアンサー
  • redfox63
  • ベストアンサー率71% (1325/1856)
回答No.2

Application.DoEventsをループのたびに呼び出しているのを間引いてみてはいかがでしょう たとえば10回に1回とか 1000回に1回とかに 最初の例なら dim blder as new System.Text.StringBuilder For I As Integer = 0 To CType(br.Length, Integer) - 1   'strbyte = Hex(br.ReadByte).ToString   'If strbyte.Length = 1 Then   '  strbyte = "0" & strbyte   'End If   'TextBox1.Text = TextBox1.Text & " " & strbyte   builder.Append( String.Format("{0:X2}", br.ReadByte ) )   if I mod 10 = 0 then     Application.DoEvents()   end if Next TextBox1.Text = builder.ToString といった具合で ・・・ #1の回答のように 文字列の連結にはStringBuilderを使って最終的に ToStringで文字列を取得したほうがいい場合があります

qwerjpo
質問者

お礼

返信ありがとうございます。 ものすごい高速に読み取ることができました。 ありがとうございました。

その他の回答 (1)

  • Gotthold
  • ベストアンサー率47% (396/832)
回答No.1

読み込みが遅いんじゃなくて、文字列連結が遅いんじゃないの? StringBuilderとか使うといいよ。 文字列処理を高速に行う: .NET Tips: C#, VB.NET, Visual Studio http://dobon.net/vb/dotnet/string/stringbuilder.html

関連するQ&A

  • [VB.NET] 処理の高速化を行いたい。

    教えて下さい。 あるファイルの1バイトづつローテートを行いファイルの書き出しを行っています。(ビット演算による暗号化) しかし以下の処理の場合、サイズの大きいファイルを扱った場合に多くの処理時間がかかってしまいます。 もっと処理を高速化する事は可能でしょうか? ----------------------------------------------------------------- ' 読み込みファイル Private Const READ_FILE As String = "c:\temp\date.Text" ' 書き込みファイル Private WRIT_FILE As String = "c:\temp\date.dat" ' オブジェクト作成 Dim br As New System.IO.FileStream(READ_FILE, IO.FileMode.Open, IO.FileAccess.Read) Dim bw As New System.IO.StreamWriter(WRIT_FILE, False, System.Text.Encoding.GetEncoding("iso-8859-1")) Dim m As Integer = 3 ' 3ビット左回転 Dim n As Integer ' 1バイトづつ処理 For I As Integer = 0 To CType(br.Length, Integer) - 1 ' 16進数を10進数へ変換 n = ("&h" & String.Format("{0:X2}", br.ReadByte)) ' mビット左へローテート処理を行いファイルへ書き込み bw.Write(ChrW("&h" & Hex(Int((n / 2 ^ (8 - m)) + (n * 2 ^ m And 255))))) Next ' 閉じる bw.Close() br.Close() ----------------------------------------------------------------- よろしくお願いします。

  • VB2008 バイナリデータの書き込みで

    現在、VB2008でバイナリデータを書き込むプログラムを作成しているのですが、String型のデータをバイナリデータに書き込むと、なぜかそのバイナリデータの先頭に05が入ってしまいます。 コードは以下のように記述しています。 TextBox1.Text = "TEST" Dim Bin As New System.IO.BinaryWriter(New System.IO.FileStream(filename, IO.FileMode.Create)) ~~~省略~~~ Bin.Write(TextBox1.Text) ~~~省略~~~ Bin.Close() 書き込まれたバイナリデータは、 05 54 45 53 54 で、テキスト欄では、 .TEST と表示されます。 分かりにくい質問で申し訳ありませんが、回答よろしくお願いします。

  • VBAでバイナリエディタを作ろうとしています

    テキストファイルを内部でバイナリとして開いて 16進数のダンプファイルとして新たなテキストに保存することはできたのですが 逆がどうしてもできません。 やり方としては、 03 E3 BD 71 80… のようなテキストデータをバイナリにして16進数にして保存したテキストファイルをダイアログから呼び出してもう一度元のテキスト文章に戻すという作業です。 とりあえず、まずは以下のコードでエクセル内で16進数を元の文章データに変換しようとしています。 Sub binaryToText() Dim fname As String Dim str() As Byte Dim row As Integer row = 1 fname = "Test.txt" '16進数ファイル Open fname For Binary As #1 Do Until EOF(1) ReDim Preserve str(row) Get #1, , str(row) row = row + 1 Loop Close #1 End Sub hex関数やchr関数を使う必要があるのでしょうか? VBAは始めたばかりで根本的なところで間違っているかもしれませんがよろしくお願いします。

  • VB添削

    このプログラムは 例えば3 3 4とテキストボックスに数字が打ち込まれると 3×3行列が4個分 のテキストボックスがでてきます。 ここに数字を打ち込んでいき、ボタン2を押すと3×3のテキスト トボックスが出てくると同時に足し算した結果が出てくるようにしたいです。 以下のプログラムはできたところまで作成しています。 どこを直せばよいのでしょうか。 Public Class Form1 Private number As Integer Private rows As Integer Private columns As Integer Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For i As Integer = 1 To 3 AddHandler Me.Controls("TextBox" & i).TextChanged, AddressOf TextBox_TextChanged Next End Sub Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) If System.Text.RegularExpressions.Regex.IsMatch(CType(sender, TextBox).Text, "[^0-9]") Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) With CType(sender, TextBox) .Text = .Text.Substring(0, .Text.Length - 1) .SelectionStart = .Text.Length End With End If Dim cnt1 As Integer Dim cnt2 As Integer Dim cnt3 As Integer If Integer.TryParse(TextBox1.Text, cnt1) And Integer.TryParse(TextBox2.Text, cnt2) And Integer.TryParse(TextBox3.Text, cnt3) Then For k = 1 To cnt3 For i = 1 To cnt1 For j = 1 To cnt2 Dim tb As TextBox = New TextBox() tb.Name = "tb" + i.ToString() Me.Controls.Add(tb) tb.Top = (i - 1) * 28 + 55 tb.Left = (j - 1) * 30 + 40 * (cnt2 * (k - 1)) + 10 tb.Width = 25 Next Next Next End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Bounds = New Rectangle(10, 10, 1350, 800) Me.AutoScroll = True End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sum As Double Dim cnt As Integer = 0 For i As Integer = 1 To rows For j As Integer = 1 To columns Dim tb As TextBox = New TextBox() cnt += 1 : If cnt > rows * columns Then cnt = 1 tb.Name = "tb" + cnt.ToString Me.Controls.Add(tb) tb.Top = (i - 1) * 30 + (80 + 40 * rows) tb.Left = (j - 1) * 60 + 10 tb.Width = 40 sum = 0 For k As Integer = 1 To number sum += Double.Parse(CType(Me.Controls("tb" + (cnt + (rows * columns * (k - 1))).ToString()), TextBox).Text) Next tb.Text = sum.ToString() Next Next End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged End Sub End Class

  • VBA バイナリ―から文字列にする方法

    この度はお世話になります。 現在、バイナリ―ファイル(xxxx.bin)をVBAで読み込み、バイナリ―データを文字列化して、エクセルで解析できるようなシートを作っています。 バイナリ―ファイルの中身が31 39 32 31 ・・・・・となっていたら、31393231・・・と文字列化にしたいです。 そこで、自分でプログラムを考えてみたのですが、バイナリ―が 01 などの場合、など“1”として読み込まれて、“0”が入らず、ずれてしまいます。 Sub 電文解析プログラム() Dim Deciphering_file As Variant '読み込みファイル Dim buf As Byte '1バイト格納 Dim fLen As Long 'ファイルサイズ Dim TEMP(1) As String ' Dim S_JIS As String '文字コード(2バイト) Dim str As String '文字列データ Dim i As Long Deciphering_file = Application.GetOpenFilename("BINファイル(*.bin),*.bin") fLen = FileLen(Deciphering_file) Open Deciphering_file For Binary As #1 For i = 1 To fLen Get #1, i, buf S_JIS = Hex(buf) If buf = 0 Then S_JIS = "00" End If TEMP(0) = Mid(S_JIS, 1, 1) TEMP(1) = Mid(S_JIS, 2, 1) str = myChr & TEMP(0) & TEMP(1) Next i End Sub ホントは3行くらいで済みそうな気がするんですが、あまりプログラミングをやったことありません。なので、すみませんがご教授お願いいたします。

  • VBのGUI 行列の和を求める

    VBのGUIです。 行列の足し算を行うプログラムをつくりたいです。 以下のプログラムはできたところまで作成しています。 □個の□行□列(□はテキストボックス)の所に例えばユーザーが3 3 3と入力したとします。 ボタン1を押すと3×3の3個個分のテキストボックスがでてきて、要素を打ち込めるようになります。 そしてユーザが要素を打ち込みます。次に要素が 1 2 1  2 1 2   2 1 2 2 1 2  1 2 1   1 2 1 1 2 1  2 1 2   1 2 1 というように入力されたとします。 ボタン2を押すと 3×3のテキストボックスが出てきて この3つの行列の和を足した 5 4 5 4 5 4 4 5 4というようにテキストボックスに表示されるようにしたいです。 3この3行3列の和だけでなく何個の何行何列の場合でもできるようにしたいです。 どのようなソースでこのプログラムはできるのでしょうか。 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Bounds = New Rectangle(10, 10, 1300, 800) Dim number As Integer Dim rows As Integer Dim columns As Integer If Not Integer.TryParse(TextBox3.Text, number) Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If If Not Integer.TryParse(TextBox1.Text, rows) Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If If Not Integer.TryParse(TextBox2.Text, columns) Then MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) Exit Sub End If For k = 1 To number For i = 1 To rows For j = 1 To columns Dim tb As TextBox = New TextBox() tb.Name = "R" & i.ToString() & "C" & j.ToString() & "No" & k.ToString() Me.Controls.Add(tb) tb.Top = (i - 1) * 30 + 80 tb.Left = (j - 1) * 60 + 70 * (columns * (k - 1)) + 10 tb.Width = 50 Next Next Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim number As Integer Dim rows As Integer Dim columns As Integer Dim r As Integer Dim n As Integer Dim m As Integer Dim sum As Double sum = 0 For n = 1 To columns For m = 1 To rows sum = 0 For r = 1 To number Next For i As Integer = 1 To rows For j As Integer = 1 To columns Dim tb As TextBox = New TextBox() tb.Name = "R" & i.ToString() & "C" & j.ToString() Me.Controls.Add(tb) tb.Top = (i - 1) * 30 + 80 tb.Left = (j - 1) * 60 + 10 tb.Width = 40 Next Next Next Next End Sub End Class

  • バイナリファイルのデータから特定の番地のHEXデータの表示方法

    現在、バイナリファイルのデータから特定の番地のデータだけをテキストボックスに表示させたいというプログラムを作成しております。 特定番地は0x1F65番地~の6バイトを抜き出し、そのHEXデータをテキストボックスにそのHEXがわかるテキストデータとして表示させたいと考えているのですが、この場合どのようにコーディングしたらよいのでしょうか。 現在のプログラムは private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { //指定したファイル名でStreamReaderを設定する try{ System::IO::StreamReader^ sr = gcnew System::IO::StreamReader("sample.bin"); //受け皿 array<wchar_t>^ buf=gcnew array<wchar_t>(6); //先頭から0x1f63バイト動かす sr->BaseStream->Seek(0x1f65,System::IO::SeekOrigin::Begin); //2バイト読み込む sr->Read(buf, 0, buf->Length); //此処でテキストボックスに入れる this->textBox1->Text= gcnew System::String(buf); } catch(System::Exception^ ex){ System::Windows::Forms::MessageBox::Show(ex->ToString()); } //this->textBox1->Text = L"変更1"; this->textBox2->Text = L"変更2"; this->textBox3->Text = L"変更3"; } コマンドボタンを押して、特定番地をBUFに入れた後テキストボックスに代入しています。これだとAsciiコードに準じたの文字が表示されてしまうので、そのHEX番号がわかるようにテキストボックスに入れたいとおもっています。 どうぞ、ご教授よろしくお願いいたします。

  • TcpClientとWebbrowser

    TcpClientでストリームからバイトでhttpデータを取得しましたが、 EUC-JPの文字コードのサイトだけがhtml内に本来入るはずのない数値が沢山現れます。 いっそのことWebBrowserコントロールを使ってサイトにアクセスし、DocumentTextを参照した方が手っ取り早いでしょうか? バイトから文字列の変換はわかりましたが、文字列から文字列へ文字コードを変換する方法がわからず困っています。ご存知の方教えていただけますか? 以下、EUC-JPの時に数値が混入するサンプルです。 '--------------------------------------------------------------------------------------------------- ' httpファイルを読み込む '--------------------------------------------------------------------------------------------------- Private Sub GetHttp(ByVal host As String, ByVal port As Integer, ByVal cmd As String, ByRef retcode As Integer, ByRef http_data As String) 'TCP/IP接続を行う Dim client As New TcpClient() Try client.Connect(host, port) 'ストリームを取得する Dim stream As NetworkStream = client.GetStream() Dim param As String = cmd + " HTTP/1.0" + ControlChars.CrLf+ControlChars.CrLf Dim buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(param) stream.Write(buffer, 0, buffer.Length) Dim sb As New System.Text.StringBuilder() Dim len As Integer http_data = "" Dim bytData() As Byte = New Byte(1048576) {} '1MB Dim strCharset As String = "" 'すべて受信する Dim cnt As Integer For cnt = 1 To 1000 '受信 len = stream.Read(bytData, 0, bytData.Length) 'バッファサイズを与えて、受信サイズを得る sb.Append(Encoding.GetEncoding("utf-8").GetString(bytData, 0, len)) 'utf-8 If Not stream.DataAvailable Then '受信キューにデータがある場合はTrue Exit For End If Next '正常に受信できた場合 http_data = sb.ToString retcode = 0 Catch ex As Exception retcode = -1 http_data = "" Finally client.Close() End Try End Sub '----- Dim host As String = "ホスト名" Dim port As Integer = 80 Dim cmd As String = "GET /index.html" '戻り値 Dim retcode As Integer Dim http_data As String = "" Dim charset As String = "" Try GetHttp(host, port, cmd, retcode, http_data) Catch ex As Exception MessageBox.Show(ex.Message, "エラーです。") End Try

  • VB2008

    VB初心者です、よろしくお願いいたします。 現在、VB2008を使い、顧客名簿を作成しています。 前回の質問で、テキストファイルのファイル名だけを取得する方法は分かったのですが、そのファイルの内容をテキストボックスに表示する方法がうまくいきません。 ファイルの内容は 1行目→名前 2行目→メールアドレス 3行目→郵便番号 4行目→住所 5行目→購入した商品 をそれぞれ、textbox1~5に表示させたいと思っています。 書籍などで調べた結果 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim totalcount As Integer Dim oneline As String Dim sr As System.IO.StreamReader sr = New System.IO.StreamReader("C:hogehoge\" & ComboBox1.Text & ".txt") oneline = sr.ReadLine() sr.Close() Dim skipline As Integer Dim r As Random = New Random() skipline = r.Next(totalcount) sr = New System.IO.StreamReader("C:hogehoge\" & ComboBox1.Text & ".txt") Dim i As Integer = 0 Do While i < skipline sr.ReadLine() i += 1 Loop oneline = sr.ReadLine() sr.Close() TextBox1.Text = oneline TextBox2.Text = (skipline + 1).ToString() End Sub このような記述になりました。 textbox1には問題なく名前が表示されますが、textbox2には"1"と表示されてしまいます。 すみませんが、修正箇所を教えていただけませんか? よろしくお願いいたします。

  • 正規表現で<BR>の行を除外したいです。 VB.NET

    VB.NETで正規表現を使って2つの文字列に囲まれた部分を 取得したいのですが、文字列の先頭と末尾の部分の<BR>の行と 空行を削除したいです。 具体的には対象の文字列が ”ABC <BR> <BR> あいうえお <BR> かきくけこ <BR> さしすせそ <BR> <BR> <BR> XYZ” となっていた場合に ”あいうえお <BR> かきくけこ <BR> さしすせそ” を取得したいです。 現在このようになっています。 Public STR As String Sub Form1_Load Dim sr As New System.IO.StreamReader("c:\0.html", system.Text.Encoding.Default) STR = sr.ReadToEnd TextBox2.Text = "(?<data>.+?)" End Sub Sub Button1_Click  Dim A As String = "ABC"  Dim Z As String = "XYZ"  Label1.Text = A & TextBox2.Text & Z  Dim r As New Regex(Label1.Text, RegexOptions.Singleline)  Dim m As System.Text.RegularExpressions.Match  Try  TextBox1.Text = r.Match(STR).Result("${data}")  Catch ex As Exception  Console.WriteLine("Error")  End Try End Sub <BR>が<br>や<Br><bR>< br >などにも対応したりと 条件がいろいろありすぎて色々試したのですが、 どうしても分かりませんでした。 申し訳ございませんが、お助け頂ければ幸いです。