0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Chai
  • 203
  • 2
  • 4
  • 11
  • What exactly do you mean by "Octal numbers"? Please explain expected inputs and outputs. – Oded May 08 '12 at 18:32
  • 1
    Similar to http://stackoverflow.com/questions/10504034/how-do-you-convert-a-string-into-hexadecimal-in-vb-net/10504215#10504215 – Tilak May 08 '12 at 18:44
  • Agreed base are different. But if you understand how to convert to hexa, converting to octa is trivial. – Tilak May 08 '12 at 18:55

2 Answers2

5

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. 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

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

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131