0

Thia is the code I got. I am trying to fix it but I can`t find what is wrong with my code. the point were the codes starts to fail is inside the if, Am I doing something wrong?

class Program
{
    static void Main(string[] args)
    {
        double division;
        double chosenNumber;
        int intDivision;
        int remainder = 1;
        int fullNumber = 0;
        int numberOfTimes = 0;

        Console.WriteLine("Choose a number");
         chosenNumber = int.Parse(Console.ReadLine());


        do
        {
            division = chosenNumber / 16;
            intDivision = Convert.ToInt32(chosenNumber) / 16;
            remainder = Convert.ToInt32((division - intDivision) * 16);
            if (numberOfTimes != 0)
            {
                fullNumber = numberOfTimes ^ 10 * remainder + fullNumber;
            }
            else
            {
                fullNumber = remainder;
            }
            numberOfTimes++;
            chosenNumber = intDivision; 
        } while (remainder > 0);

        Console.WriteLine(fullNumber);
    }
  • What do you mean by "fails". Is there an exception? Which? Also, there are built-in functions for parsing hexadecimal numbers (e.g. int.Parse with NumberStyles.AllowHexSpecifier); no need to re-invent the wheel. – Klaus Gütter Nov 10 '18 at 11:14
  • 1
    It would be awesome if you could provide at least 7 inputs **and the expected results for each of those 7 inputs**. – mjwills Nov 10 '18 at 11:39
  • Also, please explain why 25 gives 16. Looking at https://www.binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result. – mjwills Nov 10 '18 at 11:55
  • Possible duplicate of [How to convert numbers between hexadecimal and decimal in C#?](https://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c) – mjwills Nov 10 '18 at 11:59
  • It probably helps a bit to think about what "hex" really means. It is a way to encode numbers in base 16, using the characters A..F to represent digits beyond 0..9. There are only two practical data types in C# that can store such digits, they are char[] and string. Using *double* can only produce nonsense results, it uses base 2 under the hood and base 10 when displayed. Change *fullNumber* to string to get ahead. – Hans Passant Nov 10 '18 at 14:44

2 Answers2

1

I took a few liberties with what you already have and no, I can't say what's wrong with it, but this seems kinda alright. I hope it helps.

static void Main(string[] args)
{
    Console.WriteLine("Choose a number");
    int chosenNumber = Convert.ToInt32(Console.ReadLine());
    int remainder;
    string result = string.Empty;
    while (chosenNumber > 0)
    {
        remainder = chosenNumber % 16;
        chosenNumber /= 16;
        result = remainder.ToString() + result;
    }

    Console.WriteLine(result);
    Console.Read();
}
Eva
  • 37
  • 6
0

this would be my solution(basically the same as Eva's, but with "built-in" input handling).

    int chiffre = 0;
    int rem = 0;
    string val = "";
    Console.WriteLine("Choose a number");
    if(int.TryParse(Console.ReadLine(), out chiffre))
    {
        while (chiffre != 0)
        {
            rem = chiffre % 16;

            if (rem < 10)
                rem += 48; // ascii key for 0
            else
                rem += 55;
            chiffre /= 16;
            val = (char)rem + val;
        }
    }
    Console.WriteLine(val);
}

            fullNumber = numberOfTimes ^ 10 * remainder + fullNumber;

is ^ 10 supposed to be bitwise XOR? if not then you might have confused ^ with Math.Pow(x, y), which in turn would lead to undefined behaviour in your function.

in any case i suggest you use either Eva's or my solution since these are more idiomatic versions of how one might write such a function(works for any base too).

ats
  • 109
  • 4