Convert Decimal to Ascii? - vb.net

this is how to convert decimal to Ascii, but i have decimal in defferent format, and i want ot convert it to ascii.
Public Shared Function DecimalToASCII(dec As String) As String
Dim ascii As String = String.Empty
For i As Integer = 0 To dec.Length - 1 Step 3
ascii += CChar(ChrW(Convert.ToByte(dec.Substring(i, 3))))
Next
Return ascii
End Function
EX:
This is in Decimal and i want ot input it in textbox1.text:
"912,697,583,1065,261"
and i want to do operation to each group of numbers between comma and then convert it to Ascii???

To split the string into groups, you can just use the Split command. Your code is a long winded way of doing this - although it will fall over when it tries to deal with the four digit number.
Create a string array with no predefined number of elements like this -
Dim myArray()
Populate it with this code
myArray = Split (dec,",")
So now, using your input example, your ascii string array contains this data
myArray (0) = "912"
myArray (1) = "697"
myArray (2) = "583"
myArray (3) = "1065"
myArray (4) = "261"
If you want to have numbers that you can use in arithmetic operations, use this code instead. The function assumes that you're using integers, but if you want to use another type, just change all the occurrences of Integer to the type you want to handle, and change the CInt function to CDbl or cSng.
Public Shared Function DecimalToASCII(dec As String) As Integer()
'create an array of strings
Dim ascii() As String
'split each group into the array
ascii = Split(dec, ",")
'declare numbers array that is the same size as the ascii array
Dim numbers(ascii.GetUpperBound(0)) As Integer
'convert the array of strings to an array of numbers
For i As Integer = 0 To ascii.GetUpperBound(0) - 1
numbers(i) = CInt(ascii(i))
Next
'return an array of numbers containing each group
Return numbers
End Function
Use it like this
Dim dec As String = "912,697,583,1065,261"
Dim MyNumbersArray() As Integer = DecimalToASCII(dec)
With this code, you will have an array of Integers like this
MyNumbersArray(0) = 912
MyNumbersArray(1) = 697
MyNumbersArray(2) = 583
MyNumbersArray(3) = 1065
MyNumbersArray(4) = 261
Now you can perform whatever math you want using the array elements.

Related

How to properly turn this string into a correct structure?

Structure WeightElement 'The Weights are stored in an array of Weight Elemenets, which are used as a tree
Dim LowPointer As Integer
Dim HighPointer As Integer
Dim TraitPointer As Integer 'This pointer allows another tree to be appended to certain elements, for the sake of dependent traits
Dim Values As WeightedTrait()
Dim Num As Integer 'The Weight Elements are named by numbers for ease of comparison
End Structure
Structure WeightedTrait
Dim TraitName As String 'This is the value that is displayed to the user when a character is made
Dim TraitNum As Integer 'The trait number is used to find the correct element in trait sub-trees
Dim WeightValue As Decimal
End Structure
Function ChewQuery(QueryList As String)
'This function takes in a string and partitions it into a weight tree
Dim ElementList() As String
ElementList = QueryList.Split(";")
Dim LoadedWeight(ElementList.Length) As WeightElement
For x = 0 To ElementList.Length - 1
'Using this method, in which the front portion of the query is repeatedly removed, allows for a simpler query structure, so that we don't need to partition between the pointers and values
LoadedWeight(x).LowPointer = CInt(BiteQuery(ElementList(x)))
LoadedWeight(x).HighPointer = CInt(BiteQuery(ElementList(x)))
LoadedWeight(x).TraitPointer = CInt(BiteQuery(ElementList(x)))
LoadedWeight(x).Num = CInt(BiteQuery(ElementList(x)))
ChewQueryValues(ElementList(x), LoadedWeight(x))
Next
Return LoadedWeight
End Function
Function BiteQuery(ByRef QueryList As String)
Dim Marker As Integer
Dim Bite As String
'This function partitions the input string around the first comma
'It returns the section before the comma, and stores the section behind the comma as the new value for the string
Try
Marker = InStr(QueryList, ",")
Bite = Left(QueryList, Marker - 1)
Marker = Len(QueryList) - Marker
QueryList = Right(QueryList, Marker)
Catch
'This is used in the case that a list without a comma is input
Bite = QueryList
QueryList = ""
End Try
Return Bite
End Function
Sub ChewQueryValues(ByRef QueryList As String, ByRef LoadedWeight As WeightElement)
LoadedWeight.Values = {}
While Len(QueryList) > 0
'This While loop is so that an arbitrary number of values can be inserted
'Because BiteQuery takes in functions by reference, each loop reduces the length of the string until it is empty
ReDim Preserve LoadedWeight.Values(LoadedWeight.Values.Length + 1)
LoadedWeight.Values(LoadedWeight.Values.Length - 1).TraitName = BiteQuery(QueryList)
LoadedWeight.Values(LoadedWeight.Values.Length - 1).TraitNum = CInt(BiteQuery(QueryList))
LoadedWeight.Values(LoadedWeight.Values.Length - 1).WeightValue = CDec(BiteQuery(QueryList))
End While
End Sub
These subroutines should transform a properly formatted string into a WeightElement array. The string I am using is:
1,2,0,5,Black,26,1,White,30,1,EastAsian,27,1,SouthAsian,28,1,MiddleEastern,29,1;3,4,25,3,N/A,0,1;28,5,6,13,N/A,0,1;-1,7,0,1,Male,16,1,Female,17,1,Non-Binary,18,1;-1,-1,8,4,N/A,0,1;-1,34,9,15,N/A,0,1;10,11,0,28,Light,-1,1,Medium,-1,1;-1,-1,12,2,N/A,0,1;13,14,0,21,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;15,16,0,28,Straight,-1,1,Wavy,-1,1,Curly,-1,1;-1,17,0,26,Light,-1,1,Medium,-1,1,Dark,-1,1;18,-1,0,30,Pale,-1,1,Light,-1,1;19,20,0,17,Male,31,1,Female,32,1,Intersex,33,1;-1,21,0,19,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;22,-1,0,23,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,23,0,26,Curly,-1,1,Coily,-1,1;24,-1,0,30,Straight,-1,1,Wavy,-1,1,Curly,-1,1;-1,-1,0,27,Pale,-1,1,Light,-1,1,Medium,-1,1;-1,-1,0,29,Pale,-1,1,Light,-1,1,Medium,-1,1;-1,-1,0,16,Male,31,1,Female,32,1,Intersex,33,1;-1,-1,0,18,Male,31,1,Female,32,1,Intersex,33,1;-1,-1,0,20,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,22,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,27,Straight,-1,1,Wavy,-1,1;-1,-1,0,29,Straight,-1,1,Wavy,-1,1,Curly,-1,1;26,27,0,17,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,16,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;-1,-1,0,18,Andro,19,1,Gyno,20,1,A,21,1,Bi,22,1,Pan,23,1;29,30,0,8,Albinism,-1,1,ChronicFatigue,-1,1,Dwarfism,-1,1;-1,31,0,6,Autistic,-1,1,ADHD,-1,1,OCD,-1,1,Tourrette,-1,1,Epilepsy,-1,1;32,33,35,10,N/A,0,1;-1,-1,0,7,OneArmAbsent,-1,1,BothArmsAbsent,-1,1,OneHandwithAnomalies,-1,1,BothHandswithAnomalies,-1,1,OneLegAbsent,26,1,BothLegsAbsent,26,1;-1,-1,36,9,N/A,0,1;-1,-1,0,11,GoodHearing,-1,1,HardofHearing,-1,1,Deaf,-1,1,NoHearing,-1,1;-1,-1,0,34,Scars,-1,1,NaturalAnomalies,-1,1,OneEyeAbsent,-1,1,BothEyesAbsent,-1,1;-1,37,0,0,GoodVision,-1,1,Blind,-1,1,NoVision,-1,1;-1,38,0,0,Unaided,-1,1,Cane,-1,1,Wheelchair,-1,1,Full-TimeWheelchair,-1,1;-1,-1,0,25,N/A,0,1;-1,-1,0,26,Unaided,-1,1,Cane,-1,1,Wheelchair,-1,1,Full-TimeWheelchair,-1,1
The format is that the first four values between each semicolon are the low pointer, high pointer, trait pointer, and Number, in that order. After this point are the values, with the trait name, trait number, and weight value, repeating in that order
When I use this string, the program decides to return a WeightElement array of the correct length, but where all the values are 0 or empty strings. How do I make it produce a proper WeightElement, as by the format above?

Change a string and insert characters in between

I have this string: 71892378917238978
I want to do this: 71892-37891723-8978
I was trying this, but how i can do it multiple times?
String.Insert(6, "-")
We could start with this idea. Create an array of the substrings that you want to exist between the dash symbol. In your expected string you have 71892-37891723-8978, so the first block is composed of 5 char and the second block is 8 char.
Using the string.Substring method we could extract these substrings parts and store them in an array. Finally we could use string.Join to rebuild the string with the expected separator
Dim s As String = "71892378917238978"
' Define the substring blocks
Dim blocks As Integer() = New Integer() {5,8}
' Define the array that will contain the substrings
Dim substrings(blocks.Length) As String
' Get the blocks
For x As Integer = 0 To blocks.Length - 1
substrings(x) = s.Substring(0,blocks(x))
s = s.Substring(blocks(x))
Next
' Join together with the last part not included in the substrings
Dim result = String.Join("-", substrings) & s
Console.WriteLine(result)
You could generalize this code putting everything in a method where you could pass the string, the blocks and the separator.

Convert Arabic string to an array of bytes

I have a function which converts string to an array of bytes. If the string is written in English, the function works fine. But if the input string is Arabic, the function doesn't return, and I get this error:
Value was either too large or too small for an unsigned byte
Friend Function StringtoByteArray(ByRef value As String) As Byte()
Dim temp() As Byte
ReDim temp(Len(value) - 1)
Dim i As Integer
For i = 0 To Len(value) - 1 Step 1
temp(i) = Convert.ToByte(Convert.ToChar(Mid(value, i + 1, 1)))
Next
StringtoByteArray = temp
End Function
What should I change to convert Arabic characters to byte?
I am using VB.NET.
You don't need to write your function for that, this should work:
Dim b As Byte() = System.Text.Encoding.Unicode.GetBytes(value)

Convert string to integer with CINT

Const gconintRows1st As Integer = 15
Const gconintRows4th As Integer = 20
I am trying to convert String to Integer
by using:
intNumber = CInt(strNumberSelected(intFirst5Balls))
Professor's
intTemperature = CInt(strTemperatures(intMonth))
But some how this does not work.
The professor's version worked fine but I cannot figure out why the intNumber does not.
Yes, this is my first time doing vb
It looks like strNumberSelected is a string array, correct? And you're asking for the string in the array at position intFirst5Balls?
Take this for an example:
Dim strArray As String() = Split("Hi|there|everybody", "|")
The Split() function will split our long string at each occurrence of a pipe ("|").
The resulting string array will have the following 3 elements:
"Hi"
"there"
"everybody"
And you'd reference these elements by their indexes:
strArray(0) = "Hi"
strArray(1) = "there"
strArray(2) = "everybody"
If you're getting the error 'Char' values cannot be converted to 'Integer' then your array is more likely an array of type Char, and the CInt() function cannot convert it to an integer.
You can use
Integer.TryParse()
to try to get a valid Integer from your array, but it sounds a bit like you're unsure what sort of data actually lives in the array.

build an array of integers inside a for next loop vb.net

i got this far ... my data string, "num_str" contains a set of ~10 numbers, each separated by a comma. the last part of the string is a blank entry, so i use '.Trim' to avoid an error
Dim i As Integer
Dim m_data() As String
m_data = num_str.Split(",")
For i = 0 To UBound(m_data)
If m_data(i).Trim.Length > 0 Then
MsgBox(Convert.ToInt32(m_data(i).Trim))
End If
Next i
as you can see from the Msgbox, each of the numbers successfully pass through the loop.
where i am stuck is how to place all of the 'Convert.ToInt32(m_data(i).Trim)' numbers, which are now presumably integers, into an array.
how do i build an array of integers inside the For / Next loop so i can find MAX and MIN and LAST
TIA
You just need to initialize the array with the zero-based indexer. You can deduce it's initial size from the size of the string():
Dim m_data = num_str.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim intArray(m_data.Length) As Int32
For i = 0 To m_data.Length - 1
intArray(i) = Int32.Parse(m_data(i).Trim())
Next i
Note that i've also used the overload of String.Split which removes empty strings.
This way is more concise using the Select LINQ Operator.
Dim arrayOfInts = num_str.
Split({","c},
StringSplitOptions.RemoveEmptyEntries).
Select(Function(v) Int32.Parse(v.Trim()))
Dim minInt = arrayOfInts.Min()
Dim maxint = arrayOfInts.Max()