Mark Chai
Mark Chai

Reputation: 203

How do you convert a string into octal in VB.NET?

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.

Upvotes: 0

Views: 3860

Answers (2)

Tilak
Tilak

Reputation: 30698

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.)

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460098

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. 

Upvotes: 5

Related Questions