Skip to content

String Conversion ✅

::: tip

  • 1 % 10 = 1
  • 1 / 10 = 0
  • 0 % 10 = 0
  • 0 / 10 = 0 :::

Int to String

  • ASCII_KEY = '0' + int (49 = '0' + 1)
  • ASCII_VALUE = (char)(ASCII_KEY) ('1' = (char)('0' + 1))

Hint: Build the result one digit at a time

String to Int

  • ASCII_VALUE_DIFF = 'char' - 'char' ((int) 1 = '1' - '0' or 1 = 49 - 48)

Hint: Build the result one digit at a time

Base Conversion

Write a program that performs base conversion. The input is a string, an integer b1, and another integer b2. The string represents an integer in base b1. The output should be the string representing the integer in base b2. Assume 2 ≤ b1; b2 ≤ 16. Use “A” to represent 10, “B” for 11, . . ., and “F” for 15. (For example, if the string is “615”, b1 is 7 and b2 is 13, then the result should be “1A7”, since 6 × 72 + 1 × 7 + 5 = 1 × 132 + 10 × 13 + 7.)

Hint: What base can you easily covert to and from?

C# Solution

using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithms.Simple
{
  public class StringConversion
  {
    public static string IntToString(int v)
    {
      bool isNegative = v < 0 ? true : false;

      var sb = new StringBuilder();
      do
      {
        var remainder = Math.Abs(v) % 10;
        sb.Insert(0, (char)('0' + remainder)); // remainder Math.Abs(v) % 10)
        v /= 10;  // times divisible
      } while (v != 0);

      if (isNegative) sb.Insert(0, '-');
      return sb.ToString();
    }

    public static int StringToInt(string v)
    {
      var result = 0;
      bool isNegative = v[0] == '-' ? true : false;

      for (var i = isNegative ? 1 : 0; i < v.Length; i++)
      {
        var digit = v[i] - '0';
        result = result * 10 + digit;
      }

      return isNegative ? -result : result;
    }

    public static string ConvertBase(string s, int b1, int b2)
    {
      bool isNegative = s[0] == '-' ? true : false;
      var numAsInt = 0;

      for (var i = isNegative ? 1 : 0; i < s.Length; i++)
      {
        numAsInt *= b1;
        numAsInt += Char.IsDigit(s[i]) ? s[i] - '0' : s[i] - 'A' + 10;
      }

      return (isNegative ? "-" : "") + (numAsInt == 0 ? "" : ConstructFromBase(numAsInt, b2));
    }

    private static string ConstructFromBase(int numAsInt, int b2)
    {
      return numAsInt == 0 ? "" : ConstructFromBase(numAsInt / b2, b2) + (char)(numAsInt % b2 >= 10 ? numAsInt % b2 - 10 + 'A' : numAsInt % b2 + '0');
    }
  }
}

C# Tests

using Algorithms.Simple;
using Xunit;

namespace AlgorithmTests.Simple
{
  public class StringConversionTests
  {
    [Fact]
    public void IntToString_Test()
    {
      Assert.Equal("123", StringConversion.IntToString(123));
      Assert.Equal("-123", StringConversion.IntToString(-123));
      Assert.Equal("0", StringConversion.IntToString(0));
    }

    [Fact]
    public void StringToInt_Test()
    {
      Assert.Equal(123, StringConversion.StringToInt("123"));
      Assert.Equal(-123, StringConversion.StringToInt("-123"));
    }

    [Fact]
    public void ConvertBase_Test()
    {
      Assert.Equal("1A7", StringConversion.ConvertBase("615", 7, 13));
      Assert.Equal("149A", StringConversion.ConvertBase("3000", 10, 13));
    }
  }
}