Converting binary to base 4 - vb.net

What I hope to achieve:
I want to convert text to DNA (which is a base 4 system, "a,G,T,c")
How I plan to do it:
Convert text string to binary,
Dim BinaryConvert As String = ""
For Each C As Char In Textbox1.Text
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8, "0")
BinaryConvert &= s
Next
Textbox1.Text = BinaryConvert '//Changes the textbox1.Text into binary form
Then convert binary to base 4 via Pseudocode solution:
if (length of binary String is an odd number) add a zero to the front (leftmost position) of the String.
Create an empty String to add translated digits to.
While the original String of binary is not empty {
Translate the first two digits only of the binary String into a base-4 digit, and add this digit to the end (rightmost) index of the new String.
After this, remove the same two digits from the binary string and repeat if it is not empty.
}
The idea behind converting binary to DNA is simply setting G and T equal to one, with c and a equal to zero (G=T=1, a=c=0).
So all I have to do is convert the string to binary first, and then into base 4, in order to convert text to genetic code. Could you please help me write the code to convert binary to base 4.
Thank you for the help!

Converting to base 4 from base 2 is pretty simple. Since 4 itself is the 2nd power of 2, this means you can simply combine two bits to create one base 4 place (2 bits can represent 4 possible values, while 1 base 4 place can also represent 4 possible values). For example:
11100100 (base 2) = 3210 (base 4)

Related

How to shrink 10 digit numeric into 2 character

I have input comprising five character upper-case English letters e.g ABCDE and I need to convert this into two character unique ASCII output.
e.g. ABCDE and ZZZZZ should both give two different outputs
I have converted from ABCDE into hex which gives me 4142434445, but from this can I get to a two character output value I require?
Example:
INPUT1 = ABCDE
Converted to hex = 4142434445
INPUT2 = 4142434445
OUTPUT = ?? Any 2 ASCII Characters
Other examples of INPUT1 =
BIRAL
BRMAL
KLAAX
So you're starting with a 5-digit base-26 number, and you want to squeeze that into some 2-digit scheme with base n?
All possible 1-5 digit base-26 numbers gives you a number space of 26^5 = 11,881,376.
So you want the minimum n where n^2 >= 11,881,376.
Which gives you 3446.
Now it's up to you to go and find a suitable glyph block somewhere in UTF where you can reliably block-out 3446 separate characters to act as your new base/alphabet. And construct a mapping from your 5-char base-26 ABCDE type number onto your 2-char base-3446 wierd-glyph number. Good luck with that.
There's not enough variety in ASCII to do this, since it's only 128 printable characters. Limiting yourself to 2-chars of ASCII means you can only address a number space of 16384.

FormatNumber replacing number with 0

Not understanding this:
Number returned from DataReader: 185549633.66000035
We have a requirement to maintain the number of decimal places per a User Choice.
For example: maintain 7 places.
We are using:
FormatNumber(dr.Item("Field"), 7, TriState.false, , TriState.True)
The result is: 185,549,633.6600000.
We would like to maintain the 3 (or 35) at the end.
When subtracting two numbers from the resulting query we are getting a delta but trying to show these two numbers out to 6,7,8 digits is not working thus indicating a false delta to the user.
Any advice would be appreciated.
Based on my testing, you must be working with Double values rather than Decimal. Not surprisingly, the solution to your problem can be found in the documentation.
For a start, you should not be using FormatNumber. We're not in VB6 anymore ToTo. To format a number in VB.NET, call ToString on that number. I tested this:
Dim dbl = 185549633.66000035R
Dim dec = 185549633.66000035D
Dim dblString = dbl.ToString("n7")
Dim decString = dec.ToString("n7")
Console.WriteLine(dblString)
Console.WriteLine(decString)
and I saw the behaviour you describe, i.e. the output was:
185,549,633.6600000
185,549,633.6600004
I read the documentation for the Double.ToString method (note that FormatNumber would be calling ToString internally) and this is what it says:
By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
I then tested this:
Dim dbl = 185549633.66000035R
Dim dblString16 = dbl.ToString("G16")
Dim dblString17 = dbl.ToString("G17")
Console.WriteLine(dblString16)
Console.WriteLine(dblString17)
and the result was:
185549633.6600004
185549633.66000035

How to format integer as string with 2 digits?

I would like to format an integer 9 to "09" and 25 to "25".
How can this be done?
You can use either of these options:
The "0" Custom Specifier
value.ToString("00")
String.Format("{0:00}", value)
The Decimal ("D") Standard Format Specifier
value.ToString("D2")
String.Format("{0:D2}", value)
For more information:
Custom Numeric Format Strings
Standard Numeric Format Strings
If its just leading zero's that you want, you can use this:
value.tostring.padleft("0",2)
value.ToString().PadLeft(2, '0'); // C#
If you have 2 digits, say 25 for example, you will get "25" back....if you have just one digit, say 9 for example, you will get "09"....It is worth noting that this gives you a string back, and not an integer, so you may need to cast this later on in your code.
String formate is the best way to do that. It's will only add leading zero for a single length. 9 to "09" and 25 to "25".
String.format("%02d", value)
Bonus:
If you want to add multiple leading zero 9 to "0009" and 1000 to "1000". That's means you want a string for 4 indexes so the condition will be %04d.
String.format("%04d", value)
I don't know the exact syntax. But in any language, it would look like this.
a = 9
aString =""
if a < 10 then
aString="0" + a
else
aString = "" + a
end if

VB Convert RGB String to Hex

The title pretty much explains my issue. I need to convert a single string RGB value into a Hex value. I can do this if the value is given in three separate strings, but as the RGB is given from a color picker I'm unable to do this - unless I split the string which I don't want to do as I feel it's unnecessary.
I want to be able to convert a string such as: 0, 112, 192 into it's hexadecimal equivalent. Can I convert the entire string or do I have to split the string into its RGB parts first?
Since you can have varying numbers of decimal digits for the RGB value, you'll need to separate it before you convert it.
s1 = "0, 112, 192"
s2 = ""
For Each s As String In s1.Split(",")
s2 &= CInt(s).ToString("x2")
Next s

How to convert string to byte in Visual Basic

I'm trying to simulate an algoritham in cryptography and I need to convert a string of 0s and 1s back into a word. Example:
I have: 01011110010101101000001101100001101
I have split it into an array of strings:
0101111, 0010101, ...
each member has 7 characters. I want to get a letter that 0101111 represents in UTF8? How do I do this?
I try CType("0010101", Byte), but it fails. I can pass max 111 this way.
Help :/
UTF-8 is 8 bit, those are only 7 bits. Do you mean 7 bit ASCII?
In that case here you go:
Function BinToStr(binStr As String) As String
Dim i As Long
For i = 0 To (Len(binStr) / 7) - 1
[A1] = CLng(Mid(binStr, i * 7 + 1, 7))
BinToStr = BinToStr & Chr([BIN2DEC(A1)])
Next
End Function
If that's not what you're looking for, let me know.