在VB.net中的特定偏移处查找HEX值

时间:2015-08-05 02:06:55

标签: vb.net filestream binaryreader

我正在试图弄清楚如何从特定地址开始读取一段字节(Say 16),比如说0x2050。我想将十六进制值的16位输出转换为标签。

我一直想弄清楚BinaryReader和FileStreams,但我不完全确定它的区别是什么,或者我应该使用哪一个。

*我看过很多线程提到文件大小可能是一个问题,我想指出一些我要检查的文件大小可能高达4GB。

我尝试了以下内容:

 #include "CLIENT.h"

int main() {
    //Locals
    long SUCCESSFUL;
    WSAData WinSockData;
    WORD DLLVersion;
    DLLVersion = MAKEWORD(2,1);
    SUCCESSFUL = WSAStartup(DLLVersion, &WinSockData);

    string RESPONSE;
    string CONVERTER;
    char MESSAGE[200];

    SOCKADDR_IN ADDRESS;

    SOCKET sock;
    sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    ADDRESS.sin_addr.s_addr = inet_addr("173.194.219.93");
    ADDRESS.sin_family = AF_INET;
    ADDRESS.sin_port = htons(443);

    cout << "\n\tCLIENT: Do you want to connect to this SERVER? (Y/N)" << endl;
    cin >> RESPONSE;

    RESPONSE[0] = tolower(RESPONSE[0]);

    if(RESPONSE == "n") {
        cout << "\n\tOK. Quitting." << endl;
    }
    else if(RESPONSE == "y") {
        connect(sock, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS));

        SUCCESSFUL = recv(sock, MESSAGE, sizeof(MESSAGE), MSG_PEEK);

        CONVERTER = MESSAGE;

        cout << "\n\tMessage from SERVER:\n\n\t" << CONVERTER << endl;
    }
    else {
        cout << "That was an inappropriate RESPONSE!" << endl;
    }

    cout << "\n\n\tMSG_PEEK'd data: " << SUCCESSFUL << endl;
    system("PAUSE");
    return 0;

}

但这只是将6个字节写入文件,我不知道为什么。列表框中没有输出。

1 个答案:

答案 0 :(得分:1)

如下所示:?

Sub Main()
    Dim pos As Long = 8272
    Dim requiredBytes As Integer = 2
    Dim value(0 To requiredBytes - 1) As Byte
    Using reader As New BinaryReader(File.Open("File.bin", FileMode.Open))
        ' Loop through length of file.
        Dim fileLength As Long = reader.BaseStream.Length
        Dim byteCount As Integer = 0
        reader.BaseStream.Seek(pos, SeekOrigin.Begin)
        While pos < fileLength And byteCount < requiredBytes
            value(byteCount) = reader.ReadByte()
            pos += 1
            byteCount += 1
        End While
    End Using

    Dim displayValue As String
    displayValue = BitConverter.ToString(value)
End Sub