Search code examples
c#stringoctal

Transform a string into an octal


I'm trying to convert a random string into an octal int32. If someone would give ABCD i would want to get 101 102 103 104. I tried int i = Convert.ToInt32("ABCD", 8);


Solution

  • As there are no octal integer literals in C# as it is mentioned here Octal equivalent in C#. You could use strings to display/work with octal numbers. For example this way:

    string text = "ABCD";
    foreach (char c in text)
    {
        var octalString = Convert.ToString(c, 8);
        Console.WriteLine(octalString);
    }
    

    Console output is: 101 102 103 104