Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading a Random Access file.. how to parse bytes?

Reading a Random Access file.. how to parse bytes?

Scheduled Pinned Locked Moved C#
csharpdata-structureshelptutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dazinith
    wrote on last edited by
    #1

    i have been given what seems like an imposible task of trying to read in an old random access file generated by VB6.. i have to parse out a ton of values out of this file, and to be honest i have no clue on if this is even possible.. here is what im doing currently:

    FileStream aFile = new FileStream(m_strReportFilename, FileMode.Open);
    byData = new byte[2];
    aFile.Read(byData, 0, byData.Length);
    int nInt = Convert.ToInt32(byData);

    this crashes on the last line with the error 'Specified cast is not valid'.. i have a listing of the structure of the file which consists of the following types: Integer, String (of set length), Currency (which is not a type in .NET), Boolean, and Date.. i had it where i read in the whole file into a byte array to parse, but now im trying to just get the first integer out of the file.. can someone point me in the right direction please? still a newb.. cut me some slack :P -dz

    F N 2 Replies Last reply
    0
    • D dazinith

      i have been given what seems like an imposible task of trying to read in an old random access file generated by VB6.. i have to parse out a ton of values out of this file, and to be honest i have no clue on if this is even possible.. here is what im doing currently:

      FileStream aFile = new FileStream(m_strReportFilename, FileMode.Open);
      byData = new byte[2];
      aFile.Read(byData, 0, byData.Length);
      int nInt = Convert.ToInt32(byData);

      this crashes on the last line with the error 'Specified cast is not valid'.. i have a listing of the structure of the file which consists of the following types: Integer, String (of set length), Currency (which is not a type in .NET), Boolean, and Date.. i had it where i read in the whole file into a byte array to parse, but now im trying to just get the first integer out of the file.. can someone point me in the right direction please? still a newb.. cut me some slack :P -dz

      F Offline
      F Offline
      firat kocak
      wrote on last edited by
      #2

      hi, i couldn't see any function call as you made. There is no any ToInt32 function accepting a byte array as a parameter. But i can suggest you a few way to do this. But first you should correct the array length of byData to the value 4 ( for int values there needs a 4-byte space ). Way 1 - unsafe public static int AddressOf(byte[] variable) { int ptrptr; fixed(byte* ptr = variable) {ptrptr = (int) ptr;} return ptrptr; } int byDataAddr = AddressOf( byData ); int * iValue = ( int * ) byDataAddr; int nInt = *iValue; Way 2 - int nInt = ( ( int ) byData[3] ) << 24 + ( ( int ) byData[2] ) << 16 + ( ( int ) byData[1] ) << 8 + ( int ) byData[0]; there is a way more but not so important. those above may help you, i hope Doing something is better than doing nothing. So ... Move !

      D 3 Replies Last reply
      0
      • F firat kocak

        hi, i couldn't see any function call as you made. There is no any ToInt32 function accepting a byte array as a parameter. But i can suggest you a few way to do this. But first you should correct the array length of byData to the value 4 ( for int values there needs a 4-byte space ). Way 1 - unsafe public static int AddressOf(byte[] variable) { int ptrptr; fixed(byte* ptr = variable) {ptrptr = (int) ptr;} return ptrptr; } int byDataAddr = AddressOf( byData ); int * iValue = ( int * ) byDataAddr; int nInt = *iValue; Way 2 - int nInt = ( ( int ) byData[3] ) << 24 + ( ( int ) byData[2] ) << 16 + ( ( int ) byData[1] ) << 8 + ( int ) byData[0]; there is a way more but not so important. those above may help you, i hope Doing something is better than doing nothing. So ... Move !

        D Offline
        D Offline
        dazinith
        wrote on last edited by
        #3

        thanks for the suggestion, i will try what you have suggested.. according to what ive been told an int in .net is 4 bytes, but in vb6 an int is 2 bytes, let me know if im mistaken. thanks for the help, ill let you know how it goes! still a newb.. cut me some slack :P -dz

        1 Reply Last reply
        0
        • F firat kocak

          hi, i couldn't see any function call as you made. There is no any ToInt32 function accepting a byte array as a parameter. But i can suggest you a few way to do this. But first you should correct the array length of byData to the value 4 ( for int values there needs a 4-byte space ). Way 1 - unsafe public static int AddressOf(byte[] variable) { int ptrptr; fixed(byte* ptr = variable) {ptrptr = (int) ptr;} return ptrptr; } int byDataAddr = AddressOf( byData ); int * iValue = ( int * ) byDataAddr; int nInt = *iValue; Way 2 - int nInt = ( ( int ) byData[3] ) << 24 + ( ( int ) byData[2] ) << 16 + ( ( int ) byData[1] ) << 8 + ( int ) byData[0]; there is a way more but not so important. those above may help you, i hope Doing something is better than doing nothing. So ... Move !

          D Offline
          D Offline
          dazinith
          wrote on last edited by
          #4

          this has helped a bit with integers, but im having a hard time seeing how i would use either of these methods to read strings or bools, any suggestions? thanks in advance! still a newb.. cut me some slack :P -dz

          1 Reply Last reply
          0
          • F firat kocak

            hi, i couldn't see any function call as you made. There is no any ToInt32 function accepting a byte array as a parameter. But i can suggest you a few way to do this. But first you should correct the array length of byData to the value 4 ( for int values there needs a 4-byte space ). Way 1 - unsafe public static int AddressOf(byte[] variable) { int ptrptr; fixed(byte* ptr = variable) {ptrptr = (int) ptr;} return ptrptr; } int byDataAddr = AddressOf( byData ); int * iValue = ( int * ) byDataAddr; int nInt = *iValue; Way 2 - int nInt = ( ( int ) byData[3] ) << 24 + ( ( int ) byData[2] ) << 16 + ( ( int ) byData[1] ) << 8 + ( int ) byData[0]; there is a way more but not so important. those above may help you, i hope Doing something is better than doing nothing. So ... Move !

            D Offline
            D Offline
            dazinith
            wrote on last edited by
            #5

            way #1 - works, but is unsafe way #2 - does not give correct values, i changed it to what is listed below since the Integers im reading are 2 bytes

            int nInt = ( ( int ) byData[1] ) << 8 + ( int ) byData[0];

            any suggestions on how to get the correct value without being 'unsafe', and how to parse other types such as strings, dates, and booleans? still a newb.. cut me some slack :P -dz

            L F 2 Replies Last reply
            0
            • D dazinith

              way #1 - works, but is unsafe way #2 - does not give correct values, i changed it to what is listed below since the Integers im reading are 2 bytes

              int nInt = ( ( int ) byData[1] ) << 8 + ( int ) byData[0];

              any suggestions on how to get the correct value without being 'unsafe', and how to parse other types such as strings, dates, and booleans? still a newb.. cut me some slack :P -dz

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              dazinith wrote: without being 'unsafe' unsafe only if used incorrectly. What u have works, thus is cant be unsafe in a danger sense. dazinith wrote: how to parse other types such as strings, dates, and booleans? Have a look at the Encoder classes as well as the Convert class. A good knowledge of how types are allocated in VB would also help in your case.

              leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              1 Reply Last reply
              0
              • D dazinith

                way #1 - works, but is unsafe way #2 - does not give correct values, i changed it to what is listed below since the Integers im reading are 2 bytes

                int nInt = ( ( int ) byData[1] ) << 8 + ( int ) byData[0];

                any suggestions on how to get the correct value without being 'unsafe', and how to parse other types such as strings, dates, and booleans? still a newb.. cut me some slack :P -dz

                F Offline
                F Offline
                firat kocak
                wrote on last edited by
                #7

                way - 1 : it may be unsafe. but unsafe doesn't mean that it is really unsafe :). it means that "be carefulu on using unsafe, if you incorrectly use it you may crash the system LOL ). bu if you want your code pure Managed then you are wright. way - 2 : When you wrote an int value to a file or a memory it is written as fallows, first low part of the value and then the high part. 2 bytes value (byte0-byte1) byte1 and then byte2 4 bytes value (byte0-byte1-byte2-byte3) byte3 and then byte2 and then byte1 and latest byte0 So you must know the layout of the value of the byte array you try to read from a file. so lıater you can write the function below to read different types of value from a byte array public static bool GetBool( byte [] MyData , int index ) { return ( MyData[ index ] == 0 ? false : true ); } public static short GetShort( byte [] MyData , int index ) { short s = 0; s = (short) ( ( ( int ) MyData[ index + 1 ] ) << 8 ); s += (short ) Mydata[ index ]; return s; } public static int GetInt( byte [] MyData , int index ) { int s = 0; s = ( ( int ) MyData[ index + 3 ] ) << 24; s = ( ( int ) MyData[ index + 2 ] ) << 16; s = ( ( int ) MyData[ index + 1 ] ) << 8; s += ( int ) Mydata[ index ]; return s; } public static string GetString( byte [] MyData , int index , int len ) { string s; s = Encoding.ASCII.GetString( MyData , index , len ); return s; } Before converting date values from a byte array, you must know what format it has. Because i don't know the format, so i cann't help you cheers, Doing something is better than doing nothing. So ... Move !

                1 Reply Last reply
                0
                • D dazinith

                  i have been given what seems like an imposible task of trying to read in an old random access file generated by VB6.. i have to parse out a ton of values out of this file, and to be honest i have no clue on if this is even possible.. here is what im doing currently:

                  FileStream aFile = new FileStream(m_strReportFilename, FileMode.Open);
                  byData = new byte[2];
                  aFile.Read(byData, 0, byData.Length);
                  int nInt = Convert.ToInt32(byData);

                  this crashes on the last line with the error 'Specified cast is not valid'.. i have a listing of the structure of the file which consists of the following types: Integer, String (of set length), Currency (which is not a type in .NET), Boolean, and Date.. i had it where i read in the whole file into a byte array to parse, but now im trying to just get the first integer out of the file.. can someone point me in the right direction please? still a newb.. cut me some slack :P -dz

                  N Offline
                  N Offline
                  Nathan Blomquist
                  wrote on last edited by
                  #8

                  Trying doing something like: FileStream aFile = new FileStream(m_strReportFilename, FileMode.Open); BinaryReader br = new BinaryReader(aFile); Int16 nInt = br.ReadInt16(); This reads in 2 bytes from the FileStream and then converts them to a 16-bit integer (short in c# and c++). I used Int16 just to show the actual size of the return integer. Look at all the members of BinaryReader for more information. Hope this helps, Nathan --------------------------- Hmmm... what's a signature?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups