Вопрос

Is it possible to convert a string of text from a textbox into octal numbers? If so, how do I convert octal to text and text to octal?

Oh, now I understand how it works. I thought that hex and octal were two different things, but really it's two different bases. Sorry for the second post.

Это было полезно?

Решение

You can use Convert.ToInt32(String, Int32) Method and pass 8 as base.

Dim octal As Int32 = Convert.ToInt32(TxtOctalNumber.Text, 8)

The second parameter fromBase

Type: System.Int32
The base of the number in value, which must be 2, 8, 10, or 16. 

Другие советы

To convert into octal, use Convert.ToInt32 (val, 8). Convert.ToInt32 supports limited bases, 2, 8, 10, and 16.

To convert into any base,

Public Shared Function IntToString(value As Integer, baseChars As Char()) As String
    Dim result As String = String.Empty
    Dim targetBase As Integer = baseChars.Length

    Do
        result = baseChars(value Mod targetBase) + result
        value = value / targetBase
    Loop While value > 0

    Return result
End Function

The above function comes from this question. C#-to-VB conversion was done using this. (I have pasted my response from a similar question.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top