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

How can you read in C# (some bytes) from a file to an integer, short, long, etc... (I'm a newbie).
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Read it as normal, as a string, then use

int x=Convert.ToInt32(YourString);

VirtLinkAuthor Commented:
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?
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.

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

VirtLinkAuthor Commented:
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.
This should do the trick.

          static void Main(string[] args)

          private static decimal ConvStrToDec(string inchars)
               decimal result;
               int c1;
               for (c1=0 ; c1<inchars.Length ; c1++)
               return result;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VirtLinkAuthor Commented:
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.
VirtLinkAuthor Commented:
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.
To be honest with you, I didn't get an email notification of your previous comment..

Anyway, thanks for the points.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.