Reading a Random Access file.. how to parse bytes?
-
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
, andDate
.. 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 -
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
, andDate
.. 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 -dzhi, 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 !
-
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 !
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
-
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 !
-
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 !
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
-
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
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.
-
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
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 !
-
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
, andDate
.. 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 -dzTrying 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 theFileStream
and then converts them to a 16-bit integer (short in c# and c++). I usedInt16
just to show the actual size of the return integer. Look at all the members ofBinaryReader
for more information. Hope this helps, Nathan --------------------------- Hmmm... what's a signature?