By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Streaming Text File: How to properly convert to integer

P: 44
I have been working with one of my projects in asp.net. The user is to upload a text file. I read the file line by line, I first store the line in a string, and since the data is in a fix position, i just have to do a substring to it. I have to first validate the data in the text file before saving it to the database. I have used:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim ldt_file_data As DataTable
  3. Dim ldr_file_data As DataRow
  4. Dim lo_File As System.IO.StreamReader
  5. lo_File = System.IO.File.OpenText(import_path & file_name)
  6.  
  7. While Not (lo_File.EndOfStream)
  8.   ls_line = lo_File.ReadLine
  9.   ldr_file_data.Item("LNAME") = ls_line.Substring(0, 20).Trim
  10.   ldr_file_data.Item("FNAME") = ls_line.Substring(20, 20).Trim
  11.   ldr_file_data.Item("YEAR") = cint(ls_line.Substring(40, 4).Trim)
  12.   ldt_file_data.Rows.Add(ldr_file_data)
  13. End While
  14.  
  15.  
  16.  
I am retrieving the string just the way i wanted. The problem is that, when i try to use CINT to it, it returns a different value.

e.g. when i convert "2012" using CInt, it returns "&H7DC"

I think, it has something to do with encoding, i tried to use the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim lo_fs As FileStream
  3. Dim lo_File As StreamReader
  4. lo_fs = System.IO.File.Open(import_path & file_name, FileMode.OpenOrCreate)
  5. lo_File = New StreamReader(lo_fs, Encoding.UTF32)
  6.  
I have tried all sorts of encoding to no avail. Any help would be greatly appreciated. Thanks.
Jun 29 '12 #1

✓ answered by kadghar

great, you have the hexadecimal representation. Just change hex to dec and you're done.

Share this Question
Share on Google+
1 Reply


kadghar
Expert 100+
P: 1,295
great, you have the hexadecimal representation. Just change hex to dec and you're done.
Jul 3 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.