?
Solved

How to read from a file to an integer/short/long etc...

Posted on 2003-02-23
8
Medium Priority
?
224 Views
Last Modified: 2010-04-15
How can you read in C# (some bytes) from a file to an integer, short, long, etc... (I'm a newbie).
0
Comment
Question by:VirtLink
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 10

Expert Comment

by:smegghead
ID: 8003962
Read it as normal, as a string, then use

int x=Convert.ToInt32(YourString);

Smg.
0
 
LVL 1

Author Comment

by:VirtLink
ID: 8007230
So I just have to read, for example, two bytes in a string and convert it to integer.

I assume that my string needs to have a number of bytes equal to the number of bytes an integer uses.
Or should I use char instead of bytes?

And for a short, what do I use then?
0
 
LVL 10

Expert Comment

by:smegghead
ID: 8007733
If the file is a text file, then the numbers will probably be represented by a string of characters, i.e. "123", so the best way to read this is to read at as a string, then convert it into an integer.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:VirtLink
ID: 8034411
I'm sorry for the confusion.
I don't want to read (string)"123" to (int)123, but I want to read some bytes to an int.
Like this:
I can read one byte with value 210 from a file to a byte data type variable. Now I want to read two (or four, or as many as there are nescessary) bytes to an integer/short etc..
I just want to read the hexadecimal values of some bytes to a datatype, so I want to read an int (4 bytes), then a short (2 bytes), then a long (8 bytes, i think) from a file.
It can be '©$¶_', for example.
0
 
LVL 10

Accepted Solution

by:
smegghead earned 240 total points
ID: 8038243
This should do the trick.

          static void Main(string[] args)
          {
               Console.WriteLine(ConvStrToDec(")$6_"));
               Console.WriteLine(ConvStrToDec(")$6"));
               Console.WriteLine(ConvStrToDec(")$"));
               Console.WriteLine((int)ConvStrToDec(")"));
               Console.ReadLine();
          }

          private static decimal ConvStrToDec(string inchars)
          {
               decimal result;
               int c1;
               result=0;
               for (c1=0 ; c1<inchars.Length ; c1++)
               {
                    result*=256;
                    result+=inchars[c1];
               }
               return result;
          }
0
 
LVL 1

Author Comment

by:VirtLink
ID: 8040429
Thanks for your time.
With your code, I indeed can get some bytes and convert them to decimal, but I think that what I want is even much easier and I think that I am still confusing.

In Visual Basic, I can use the following code:
'Define some variables
Dim intVersion As Integer
Dim lngCount As Long

'This creates what's called a struct in C#:
Public Type tpeHeader
intFiles As Integer
strName As String * 10
End Type
Dim headHeader As tpeHeader

In a procedure, I use:
f = Freefile
'Open a file
Open "C:\Path\File.hak" For Binary As #f
'Here I read some variables:
Get #f, , intVersion
Get #f, , lngCount
'Then a struct:
Get #f, , tpeHeader
Close #f

I can use the same for outputting to a file:
f = Freefile
'Open a file
Open "C:\Path\File.hak" For Binary As #f
'Here I write some variables:
Put #f, , intVersion
Put #f, , lngCount
'Then the struct:
Put #f, , tpeHeader
Close #f

I already found a thread about reading/writing structs, but that seems to be too complicated. If there isn't a simple solution for reading/writing structs, then forget that part.
Via the above VB example, I actually read two bytes for the integer (in VB, an integer has the same size as a short in C#), then 4 bytes for the long, then 12 bytes for the struct (an integer + a string with a length of 10).
I want to do the same in C#.

Once again, thanks for your time.
0
 
LVL 1

Author Comment

by:VirtLink
ID: 8435049
Since no one is going to answer my question, and I have figured it out myself now, I'll give you, smegghead, the 80 points. You had the best idea so far.
0
 
LVL 10

Expert Comment

by:smegghead
ID: 8435574
To be honest with you, I didn't get an email notification of your previous comment..

Anyway, thanks for the points.

Smg.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question