Reputation: 51
I am looking for a way to fill a binary array in vb.net by binary numbers. I filled the array line by line but I need to fill the array by using a loop
DayArray(0) = "000"
DayArray(1) = "001"
DayArray(2) = "010"
DayArray(3) = "011"
DayArray(4) = "100"
DayArray(5) = "101"
DayArray(6) = "110"
DayArray(7) = "111"
Any idea please??
Upvotes: 0
Views: 1004
Reputation: 801
Dim DayArr(8) As String
For b As Integer = 0 To 8
DayArr(b) = Convert.ToString(b, 2).PadLeft(3, "0"c)
Next
The Convert.ToString(b, 2)
trims the leading zeros, so we need the PadLeft
to make each string exactly three characters long.
Upvotes: 1
Reputation: 349
As an array of strings? Increment a counter and have a function to convert that int to binary string..I believe ToString can do it..or maybe the Convert class -- look for the one where you provide 'base' (ie. 2 for binary, 16 for hex, etc). And apparently you only want last 3 digits: use SubString() of string class.
Upvotes: 1