none
Problem converting byte array to string RRS feed

  • Pregunta

  • Hi, i have the following problem, i have a byte array with 7 positions, i save the content of positions 3, 4, 5 and 6 into 4 int variables.
    Then I obtain the ascii character according to each int value and build a string with these four characters. The resulting string should have a lenght of 4, it has a lenth of four characters, but if i evaluate its contents inside visual studio, or save it in a text file or make msgbox(stringvariable) it has 5 characters, i think it has something to do with converting the byte array to string, or maybe with the source of this byte array (if i use utf8 encoding . GetString(bytearray) the same happens.

    So I enclose here the hole function for you to know where i take the array from, I basically need to know the file system type of  the "c" drive, for this i read mft and, as i found out there, i should get "NTFS" in bytes 3, 4, 5, 6 to know if the drive is an ntfs drive.

    ------Declarations
     Public Structure SECURITY_ATTRIBUTES
            Dim nLength As Integer
            Dim lpSecurityDescriptor As Integer
            Dim bInheritHandle As Boolean
        End Structure
    
        Public Const GENERIC_READ As Int32 = &H80000000
        Public Const GENERIC_WRITE As Int32 = &H40000000
        Public Const OPEN_EXISTING As Int32 = 3
        Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
    
        Public Const FILE_SHARE_READ = &H1
        Public Const FILE_SHARE_WRITE = &H2
        Public Const INVALID_HANDLE_VALUE = -1
        Public Const INVALID_SET_FILE_POINTER = -1
        Public Const FILE_BEGIN = 0
    
        Public Declare Auto Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As String, _
        ByVal dwDesiredAccess As Int32, ByVal dwShareMode As Int32, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
             ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As Integer
    
        Public Declare Auto Function SetFilePointer Lib "kernel32" (ByVal hFile As IntPtr, _
          ByVal lDistanceToMove As Integer, ByRef lpDistanceToMoveHigh As Integer, _
          ByVal dwMoveMethod As Integer) As Integer
    
        Public Declare Auto Function ReadFile Lib "Kernel32.dll" ( _
       ByVal hndRef As Integer, ByVal lpBuffer As Byte(), _
       ByVal numberOfBytesToRead As Integer, ByRef numberOfBytesRead As Integer, ByVal flag As Integer) As Boolean
    
        Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
    

     

    ----The function itself

     

      Dim handle As Integer, ret As Integer, nRead As Integer
    
            Dim Buffer(7) As Byte
    
            handle = CreateFile("\\.\C:", GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
            Nothing, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
            If (handle <> INVALID_HANDLE_VALUE) Then
                ret = SetFilePointer(handle, 0, Nothing, FILE_BEGIN)
                If (ret <> INVALID_SET_FILE_POINTER) Then
                    If (ReadFile(handle, Buffer, 512, nRead, 0&)) Then
                        CloseHandle(handle)
                        Dim i1, i2, i3, i4 As Integer
                        i1 = Buffer(3)
                        i2 = Buffer(4)
                        i3 = Buffer(5)
                        i4 = Buffer(6)
                        Dim str As String = Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4)
                        Dim aaa As String = str.Clone
                        MsgBox(str)
                    Else
                        MsgBox(Marshal.GetLastWin32Error())
                    End If
                End If
            End If

    If I run the function, i1= 78, i2=84, i3= 70, i4=83, so the value of the string variable should be "NTFS", but actually it is "NTFS栘".

    Thanks from Argentina!!!


    Antonio Briones Sistemas Plenario vb and c# .net developer for win and web forms. I work for
    miércoles, 17 de marzo de 2010 13:23

Respuestas

Todas las respuestas

  • hola

    si eres de Argentina porque no has escrito la consulta en español ?


    - veo que usas las api para trabajar con un archivo binario

    porque no usas la funcionalidad de System.IO, mas puntualmente


    creo que es ams simple que usar las api

    saludos
    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina
    miércoles, 17 de marzo de 2010 14:06
  • Que tal, el tema es que suelo pensar que hay mas posibilidades de respuesta en ingles que en castellano!! jaja

    El tema es que no tengo que abrir en si un archivo para leerlo sino saber de esa forma si la unidad es NTFS o FAT, y segun lei leyendo esos bits puedo saberlo, no se si con esas clases del framework puedo acceder directamente ahi o no, lo voy a probar igualmente, gracias.
    Antonio Briones Sistemas Plenario vb and c# .net developer for win and web forms. I work for
    miércoles, 17 de marzo de 2010 14:16
  • hola

    no has probado usar

    DriveInfo (Clase)

    como veras este retorna la informacion de tipo de File system de esa unidad


    saludos

    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina
    miércoles, 17 de marzo de 2010 14:26