Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-02-23
8
Medium Priority
?
225 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

580 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