How to loop through, while adding onto string? - vb.net

I need to loop through an array and populate a string in the following way
First --> string = array(0) + array(1) + array (2)
Then --> string = array(1) + array (2)
Then --> string = array(2)
How can I accomplish this if the length of the array is dynamic?
I am using vb.net
Thank you!

You can use 2 for loops to accomplish this. The first for loop would loop through the whole array and the send loop would be looping through the portion of the array that you want to add to the string.
var tempList = new ArrayList();
tempList.Add("Test1");
tempList.Add("Test2");
tempList.Add("Test3");
var tempString = new System.Text.StringBuilder();
for (int i = 0; i < tempList.Count; i++)
{
for (int j = i; j < tempList.Count; j++)
{
tempString.Append(tempList[j]);
}
}

Related

How to get numeric value from excel file cell, using POI

I am pretty new to Selenium and learning through self study. Any help will be appreciable.
Currently I am using
String data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getStringCellValue();
It is working fine for String but not for numeric. I have a cell value of '500' and I want to fetch it. Please help.
Try out following code :
public void readfile(String filepath, String filename, String sheetname) throws IOException {
File file = new File(filepath+"\\"+filename);
FileInputStream fis = new FileInputStream(file);
// for creating .xlsx workbook
Workbook wb = new XSSFWorkbook(fis);
// for reading the sheet by its name
Sheet sh = wb.getSheet(sheetname);
// find the total rows in sheet
int rowcount = sh.getLastRowNum() - sh.getFirstRowNum();
// create a loop to create
for (int i = 0; i < rowcount + 1; i++) {
Row row = sh.getRow(i);
// create a loop to print cell values
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(row.getCell(j).getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
break;
}
}
System.out.println();
}
}
Hope it will help you.
Double data = wb.getSheetAt(sheetnum).getRow(row).getCell(col).getNumericCellValue();

How to sort array of strings by their lengths

I have an array of strings such as "blue", "green", "red" and I wish to sort them so the longest string comes first and the shortest last.
Currently I am creating another array with the lengths of each string in the array in the same index positions and using this array as the key array to sort by as can be seen below, but I think this could be optimised into one line perhaps?
Dim colours() As string = {"blue", "green", "red"}
Dim colourslength() As Integer
For i As Integer = 0 To colours.Length - 1
colourslength(i) = colours(i).Length
Next
Array.Sort(colourslength, colours)
Array.Reverse(colours)
Edit: just realised I defined colours as a list in the example code, it's an array in my actual code.
Another Linq solution (warning, converted from C#)
Dim Sorted = From p In colours Order By p.Length Descending Select p
To my opinion this is the shortes way. Use linq.
Dim strs = New String() {"", "333", "22", "4444", "55555", "1"}
Dim sorted = strs.OrderBy(Function(x) x.Length).ThenBy(Function(x) x).ToArray()
Edit
If you want a reverse order just get rid of extra method call an do the sortin in a reverse order
Dim strs = New String() {"", "333", "22", "4444", "55555", "1"}
Dim sorted = strs.OrderByDescending(Function(x) x.Length).ThenByDescending(Function(x) x).ToArray
Cheers.
Dim colours = {"blue", "green", "red"}
Dim coloursSortedByLength = colours.OrderByDescending(Function(c) c.Length)
Output sequence is: green, blue, red.
The best simple way to do it, is to compare every string with all other strings in your list:
in Java:
for(int i=0; i<list.length-1; i++){
for(int j=i+1; j<list.length; j++){
if(list[i].length() < list[j].length()){
tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
}

How can I search a listbox and save all the results in another listbox?

for now, i am using this code but it doesnt give me what i want..
for example i want to list all the items with the string "1"
Dim x, count As Integer
x = ListBox1.Items.Count
count = 0
Do While count < x
If ListBox1.SelectedIndex = ListBox1.FindString("1") Then
ListBox2.Items.Add(ListBox1.Items(count))
End If
count = count + 1
Loop
Try this.
foreach (string s in listBox1.Items)
{
if(s.contains("1"))
listbox2.add(s);
}

Reading MP3 Frameheader - assigning bit values to variables

I am learning visual basic .net and I am attempting to translate some java source code to a vb.net project. The project reads mp3 details and then splits the file accurately according to the frameheader details etc.
My question relates to reading the frame header of mp3 files. I understand that the frame details are contained in the first 4 (32-bits) bytes of a frame and certain bits represent certain values as detailed here: http://www.mp3-tech.org/programmer/frame_header.html
Using FileStream I have been able to read this data and display it in binary within a text box.
I am looking for help on reading the bits and assigning them to variables within my class. I am not sure what would be the correct procedure to do this as some values as 1, 2 or 4 bits in length e.g. bits 19-20 = MpegType, bits 12-15 = BitrateIndex, bit 9 = Padding etc.
I have looked at similar projects available on codeproject.com but I do not understand how they have achieved the above.
Any help is much appreciated.
EDIT:
Here is the main sub so far, I have not included the code declaring variables and properties etc.
Public Sub decode()
Dim fs As FileStream
Dim bytes(3) As Byte
fs = New FileStream(mFilename, FileMode.Open, FileAccess.Read)
If fs.CanRead Then
fs.Read(bytes, 0, bytes.Length)
For i As Integer = 0 To bytes.Length - 1
Form1.RichTextBox.Text += Convert.ToString(bytes(i), 2).PadLeft(8, "0"c) & vbCrLf
Next
fs.Close()
fs.Dispose()
Else
MsgBox("File CANNOT be read!!!")
End If
End Sub
When this is run the output in the rich text box is as follows:
11111111
11111010
10110011
01001100
I want to read through these bits and assign the appropriate values to the variables e.g.
Read the first 12 bits for sync value.
Read bit 13 for mpegID value.
Read bit 14 and 15 for layerID value etc.
Hope that is clearer.
The java code is as follows:
public FrameHeader() {
this.header32 = 0;
valid = false;
}
public FrameHeader(int header32) {
this.header32 = header32;
decode();
}
public void setHeader32(int header32) {
this.header32 = header32;
decode();
}
private void decode() {
mpegID = (header32 >> 19) & 3;
layerID = (header32 >> 17) & 3;
crc16used = (header32 & 0x00010000) == 0;
bitrateIndex = (header32 >> 12) & 0xF;
samplingrateIndex = (header32 >> 10) & 3;
padding = (header32 & 0x00000200) != 0;
privateBitSet = (header32 & 0x00000100) != 0;
mode = (header32 >> 6) & 3;
modeExtension = (header32 >> 4) & 3;
copyrighted = (header32 & 0x00000008) != 0;
original = (header32 & 0x00000004) == 0; // bit set -> copy
emphasis = header32 & 3;
valid = (mpegID != ILLEGAL_MPEG_ID) && (layerID != ILLEGAL_LAYER_ID) && (bitrateIndex != 0)
&& (bitrateIndex != 15) && (samplingrateIndex != ILLEGAL_SR);
if (valid) {
samplingrateHz = SAMPLING_RATES[samplingrateIndex];
if (mpegID == MPEG2_ID)
samplingrateHz >>= 1; // 16,22,48 kHz
if (mpegID == MPEG25_ID)
samplingrateHz >>= 2; // 8,11,24 kHz
channels = (mode == MODE_MONO) ? 1 : 2;
bitrateKBPS = BITRATE_MAP[mpegID][layerID][bitrateIndex];
if (layerID == LAYER1_ID) {
// layer 1: always 384 samples/frame and 4byte-slots
samplesPerFrame = 384;
bytesPerSlot = 4;
}
else {
// layer 2: always 1152 samples/frame
// layer 3: MPEG1: 1152 samples/frame, MPEG2/2.5: 576
// samples/frame
samplesPerFrame = ((mpegID == MPEG1_ID) || (layerID == LAYER2_ID)) ? 1152 : 576;
bytesPerSlot = 1;
}
frameSize = ((bitrateKBPS * 125) * samplesPerFrame) / samplingrateHz;
if (bytesPerSlot > 1)
frameSize -= frameSize % bytesPerSlot;
if (padding)
frameSize += bytesPerSlot;
}
}
Here is a detailed explanation of frames and a formula on how data is held in the header, the first 4 bytes of a frame.
I am not sure what you are trying to accomplish but, just in case, here you go. There is no point in reinventing the wheel.
.Net has class called BitArray that you would use to store your bits.
I have come across a similar project that uses a function to converts bits to string. I've combined this code with another example I found that changed a binary string to integer. Interested to hear of alternative methods?
Public Function BinaryToInteger(ByVal objBitArray As BitArray, ByVal intStart As Integer, ByVal intEnd As Integer) As Integer
Dim BinaryString As String
Dim BinaryNum As Integer
Dim BitCount As Short
BinaryString = ""
For i As Integer = intStart To intEnd
BinaryString &= IIf(objBitArray.Item(i), "1", "0")
Next
For BitCount = 1 To Len(BinaryString)
BinaryNum = BinaryNum + (CDbl(Mid(BinaryString, Len(BinaryString) - BitCount + 1, 1)) * (2 ^ (BitCount - 1)))
Next BitCount
BinaryToInteger = BinaryNum
End Function

Converting JScript to VB.NET - Questions

I'm trying to convert some functions written in JScript to VB.NET (I'm porting a classic ASP page to ASP.NET) and having issues as I'm not very familiar with JScript. I'm having issues with converting even the function declaration properly in VB.NET. In my converted code VS2008 is giving me an error saying "Array bounds can not be specified in type identifiers". I don't know how to modify my function declaration to return an Array but ALSO accept an array as input as the JScript declaration does. Any ideas? Am I approaching this wrong?
Thanks in advance.
Here is one of the original JScript functions:
function binl2byt(binarray)
{
var hex_tab = "0123456789abcdef";
var bytarray = new Array(binarray.length * 4);
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
bytarray[i] = (binarray[i>>2] >> ((i%4)*8+4) & 0xF) << 4 | binarray[i>>2] >> ((i%4)*8) & 0xF;
}
return bytarray;
}
Here is what I have in VB.NET so far:
Public Function binl2byt() As Array(byval binarray as array)
Dim hex_tab As String = "0123456789abcdef"
Dim bytarray() As Byte
Dim str As String = ""
For I As Integer = 0 To (bytarray.Length * 4) Step 1
bytarray(I) = ((binarray(I >> 2) >> ((I Mod (4)) * 8 + 4) & Oxf) << 4) Or (binarray(I >> 2) >> ((I Mod (4) * 8) & OxF))
Next
Return bytarray
End Function
There is no need for this function, it is already there in .NET for you.
BitConverter.ToString(Bytes);
Where Bytes is your byte array.