Binary and Hex Challenge

Hi,
I tried to figure how to come up with a solution for the challenge and it got a bit complicate. And then I found a 3 lines of code for Hex converion which is also in the Start method.

public class BinHexChallenge : MonoBehaviour
{
    List<int> RemainderBin = new List<int>();
    List<string> RemainderHex = new List<string>();

    void Start()
    {
        BinReturn(25);
        BinReturn(91);

        HexReturn(331);
        HexReturn(173);

        int value = 173;
        string specifier = "X";  // X or x is a special mapping for Hexadecimal indication
        Debug.Log(value.ToString(specifier));
    }

    int BinReturn(int a)
    {
        int x = a;
        int y = 0;
        while (x >= 1)
        {
            y = (int) Math.Ceiling(Decimal.Remainder(x, 2));
            x = (int) Math.Floor(x / 2.0);
            RemainderBin.Add(y);
        }
       
        RemainderBin.Reverse();
        Debug.Log(string.Join("", RemainderBin));
        return RemainderBin.Count; //anything
    }

    int HexReturn(int b)
    {
        int x = b;
        int y = 0;
        while (x >= 1)
        {
            y = (int) Math.Ceiling(Decimal.Remainder(x, 16));
            x = (int) Math.Floor(x / 16.0);
            if (y == 10) RemainderHex.Add("A");
            if (y == 11) RemainderHex.Add("B");
            if (y == 12) RemainderHex.Add("C");
            if (y == 13) RemainderHex.Add("D");
            if (y == 14) RemainderHex.Add("E");
            if (y == 15) RemainderHex.Add("F");
            if (y < 10) RemainderHex.Add(y.ToString());
        }
        
        RemainderHex.Reverse();
        Debug.Log(string.Join("", RemainderHex));
        return RemainderHex.Count;
    }
}
1 Like

Phenomenal job :100:

1 Like

Privacy & Terms