|
-
May 11th, 2021, 07:23 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Math operation on set of numbers (Textbox)
There's a text box containing a set of numbers like example:
First odd digits are decades and even ones are single ones to create a full numbers (From hex. in dec. base) ignore what I said. Following mathematical actions on specific numbers are needed.
Merging is not allowed. Due to inequality of first part x100 then +second part.
Required function(s) would be like:
(9 *256)+23
(6 *256)+17
(0 *256)+3
(5 *256)+1
How can I do such thing in VB.NET?
It could be discrete in 3 smaller steps as I know but not sure how:
- Split text from left via " " space character,
- Multiplying it to 256 then adding to next number (which also separated),
- Remove those numbers each step to creating a loop to repeat the matter.
Last edited by pourkascheff; May 11th, 2021 at 11:00 AM.
-
May 11th, 2021, 08:56 AM
#2
Re: Math operation on set of numbers (Textbox)
Split text from left via " " space character
This can be done using the String.Split method: https://docs.microsoft.com/en-us/dot...m.string.split
Multiplying it to 256 then adding to next number (which also separated)
This can be done using the mathematical operators * and +
Remove those numbers each step to creating a loop to repeat the matter.
You don't need to remove the numbers, but you will need to create a loop. A For/Next loop with the step set to 2 would suffice: https://docs.microsoft.com/en-us/dot...next-statement
Here is an example:
Code:
Dim numbers = MyTextBox.Text.Split(" "c, StringSplitOptions.RemoveEmptyEntries)
Dim hex, base, result As Integer
For index = 0 To numbers.Length - 1 Step 2
If (Not Integer.TryParse(numbers(index), hex) OrElse Not Integer.TryParse(numbers(index + 1), base)) Then
Throw New Exception("Invalid number")
End If
result = hex * 256 + base
' do something with result
Next
Last edited by dday9; May 11th, 2021 at 04:39 PM.
Reason: Updated answer to use the updated value of 256
-
May 11th, 2021, 08:57 AM
#3
Re: Math operation on set of numbers (Textbox)
Yeah, you described it, except I'm not so sure about the third step. Not much point in removing those numbers. Split the string on space, then you can do something like:
Code:
For x = 0 to arrayLength-1 Step 2
result = array(x) * 255 + array(x+1)
Next
At least that's the rough idea. The Step 2 would allow you to work with each pair. I'd want a bit more validation ahead of that, to make sure that the number of array elements is an even number, for example, but that's the rough idea.
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|