C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

No.86235 の関連記事表示

<< 0 >>
■86235  Re[1]: バイナリデータの途中から切り出して出力する方法
□投稿者/ 魔界の仮面弁士 -(2018/01/01(Mon) 23:20:41)
    No86230 (へなちょこ さん) に返信
    > また同じ構造のデータがあるので、連続して同じ処理を行い、データテーブルの新しい
    > 列に代入したいという意味です。)

    とりあえず VB2017 向けのコードを書いてみました。

    「ファイルが見つからなかった場合」や「ファイルレイアウトが破損していた場合」の
    エラー対処は省略しています。


    Imports System.IO
    Module Module1

     Sub Main()
      For Each entry In EnumerateTuple("Sample.dat", System.Text.Encoding.Unicode)
       MsgBox(entry.Hena & vbCrLf & entry.Choco)
      Next
     End Sub

     Public Iterator Function EnumerateTuple(fileName As String, enc As System.Text.Encoding) As IEnumerable(Of (Hena As String, Choco As String))
      Using stm As New FileStream(fileName, FileMode.Open), reader As New BinaryReader(stm)
       Do While stm.Position < stm.Length
        reader.ReadBytes(2) '最初の2バイトは読み捨て
        Dim len1 As Integer = 2 * reader.ReadByte() '文字1のバイト数
        Dim len2 As Integer = 2 * reader.ReadByte() '文字2のバイト数
        reader.ReadBytes(4) '次の4バイトは読み捨て
        Dim bin1 As Byte() = reader.ReadBytes(len1) '文字列1のデータ
        Dim bin2 As Byte() = reader.ReadBytes(len2) '文字列2のデータ
        Dim txt1 As String = enc.GetString(bin1)
        Dim txt2 As String = enc.GetString(bin1)
        Yield (txt1, txt2) '文字列1, 文字列2 のタプルを順次返却
       Loop
      End Using
     End Function

    End Module
記事No.86230 のレス /過去ログ147より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -