Streaming Text File: How to properly convert to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chibbie23
    New Member
    • Mar 2010
    • 44

    #1

    Streaming Text File: How to properly convert to integer

    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:

    Code:
    Dim ldt_file_data As DataTable
    Dim ldr_file_data As DataRow
    Dim lo_File As System.IO.StreamReader
    lo_File = System.IO.File.OpenText(import_path & file_name)
    
    While Not (lo_File.EndOfStream)
      ls_line = lo_File.ReadLine
      ldr_file_data.Item("LNAME") = ls_line.Substring(0, 20).Trim
      ldr_file_data.Item("FNAME") = ls_line.Substring(20, 20).Trim
      ldr_file_data.Item("YEAR") = cint(ls_line.Substring(40, 4).Trim)
      ldt_file_data.Rows.Add(ldr_file_data)
    End While
    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:

    Code:
    Dim lo_fs As FileStream
    Dim lo_File As StreamReader
    lo_fs = System.IO.File.Open(import_path & file_name, FileMode.OpenOrCreate)
    lo_File = New StreamReader(lo_fs, Encoding.UTF32)
    I have tried all sorts of encoding to no avail. Any help would be greatly appreciated. Thanks.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

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

    Comment

    Working...