Results 1 to 3 of 3

Thread: [RESOLVED] Math operation on set of numbers (Textbox)

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] Math operation on set of numbers (Textbox)

    There's a text box containing a set of numbers like example:
    Code:
    9 23 6 17 0 3 5 1
    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:
    1. Split text from left via " " space character,
    2. Multiplying it to 256 then adding to next number (which also separated),
    3. Remove those numbers each step to creating a loop to repeat the matter.
    Last edited by pourkascheff; May 11th, 2021 at 11:00 AM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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
  •  



Click Here to Expand Forum to Full Width