String to Byte from CSV

enonod

Well-Known Member
Licensed User
Longtime User
I have data in list of arrays from loadCSV.
The data are intended to be bytes but are of course Strings.
I can access a single element and do a numerical calculation directly on one of the arrays in the list, i.e. add 3 and get the correct answer.
This led me to believe that some kind of auto casting is occuring.
Not so... I cannot store the numbers in an Byte or Int array.

Therefore the question is, how do I convert the original numbers entered in the csv file which then became strings but that act as numbers, back to Bytes?

I know that the answer is going to shock me.
[EDIT]
B4X:
Sub convCSV
Dim acsv(20,4) As String  ' Byte  ' Int
    Dim su As StringUtils
    Dim list1 As List
      Dim i As Int
    list1 = su.LoadCSV(File.DirAssets, "csv.txt", ",")
    For i = 0 To list1.Size-1
        Dim sCol() As String 
            sCol = list1.Get(i)
            Log(sCol(11)*2)    'Calculation on string
       ' acsv(i,i Mod 4,0)=sCol
    Next    
End Sub
 
Last edited:

MLDev

Active Member
Licensed User
Longtime User
It should work. This works for me:

B4X:
Dim s As String = "127"
Dim i As Byte = s
Log(i)

I did notice that you declared acsv as a 2 dimensional array and in the commented out line it's a 3 dimensional array.
 
Last edited:
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks for replying and getting me to look more closely.
You are correct it does convert as you say. However I 'thought' it had failed because there are a large number of items in the returned List1 but the loop only operates once, so I misunderstood the reason. List1.Size returns 1. So the code I copied from the forum http://www.b4x.com/forum/basic4android-updates-questions/17132-csv-file-array-help-2.html seems incorrect. (list1.Size-1)

The array is dimensioned correctly in my program but elsewhere. This line was added to the post to show I had not neglected it but is a typo.

The correct code, somewhat different to original, is below it does work now with For i = 0 To sCol.Length-1

B4X:
Sub convCSV
    Dim su As StringUtils
    Dim list1 As List
    Dim i, acsv(20,4,2) As Int
    list1 = su.LoadCSV(File.DirAssets, "tests.txt", ",")
    Dim sCol() As String 
    sCol = list1.Get(0)  'parameter value seems to have no effect
    Log(sCol.Length)   'shows correct
    For i = 0 To sCol.Length-1  'loops correctly multiple times
      acsv(i,i Mod 4,0)=sCol(i)   
    Next    
End Sub
 
Last edited:
Upvote 0
Top