Trying to convert dec to hex in c# but every time I try this code it fails, why?











up vote
0
down vote

favorite












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);
}









share|improve this question
























  • 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
    2 days ago






  • 1




    It would be awesome if you could provide at least 7 inputs and the expected results for each of those 7 inputs.
    – mjwills
    2 days ago










  • Also, please explain why 25 gives 16. Looking at binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result.
    – mjwills
    2 days ago












  • Possible duplicate of How to convert numbers between hexadecimal and decimal in C#?
    – mjwills
    2 days ago










  • 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
    2 days ago















up vote
0
down vote

favorite












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);
}









share|improve this question
























  • 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
    2 days ago






  • 1




    It would be awesome if you could provide at least 7 inputs and the expected results for each of those 7 inputs.
    – mjwills
    2 days ago










  • Also, please explain why 25 gives 16. Looking at binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result.
    – mjwills
    2 days ago












  • Possible duplicate of How to convert numbers between hexadecimal and decimal in C#?
    – mjwills
    2 days ago










  • 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
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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);
}









share|improve this question















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);
}






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Yuval Amir

113




113












  • 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
    2 days ago






  • 1




    It would be awesome if you could provide at least 7 inputs and the expected results for each of those 7 inputs.
    – mjwills
    2 days ago










  • Also, please explain why 25 gives 16. Looking at binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result.
    – mjwills
    2 days ago












  • Possible duplicate of How to convert numbers between hexadecimal and decimal in C#?
    – mjwills
    2 days ago










  • 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
    2 days ago


















  • 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
    2 days ago






  • 1




    It would be awesome if you could provide at least 7 inputs and the expected results for each of those 7 inputs.
    – mjwills
    2 days ago










  • Also, please explain why 25 gives 16. Looking at binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result.
    – mjwills
    2 days ago












  • Possible duplicate of How to convert numbers between hexadecimal and decimal in C#?
    – mjwills
    2 days ago










  • 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
    2 days ago
















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
2 days ago




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
2 days ago




1




1




It would be awesome if you could provide at least 7 inputs and the expected results for each of those 7 inputs.
– mjwills
2 days ago




It would be awesome if you could provide at least 7 inputs and the expected results for each of those 7 inputs.
– mjwills
2 days ago












Also, please explain why 25 gives 16. Looking at binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result.
– mjwills
2 days ago






Also, please explain why 25 gives 16. Looking at binaryhexconverter.com/hex-to-decimal-converter I am a little confused as to why that is the expected result.
– mjwills
2 days ago














Possible duplicate of How to convert numbers between hexadecimal and decimal in C#?
– mjwills
2 days ago




Possible duplicate of How to convert numbers between hexadecimal and decimal in C#?
– mjwills
2 days ago












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
2 days ago




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
2 days ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote













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();
}





share|improve this answer





















  • that works well I want to know for the future what is wrong.
    – Yuval Amir
    yesterday


















up vote
0
down vote













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






share|improve this answer








New contributor




ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238320%2ftrying-to-convert-dec-to-hex-in-c-sharp-but-every-time-i-try-this-code-it-fails%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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();
    }





    share|improve this answer





















    • that works well I want to know for the future what is wrong.
      – Yuval Amir
      yesterday















    up vote
    1
    down vote













    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();
    }





    share|improve this answer





















    • that works well I want to know for the future what is wrong.
      – Yuval Amir
      yesterday













    up vote
    1
    down vote










    up vote
    1
    down vote









    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();
    }





    share|improve this answer












    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();
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    Eva

    276




    276












    • that works well I want to know for the future what is wrong.
      – Yuval Amir
      yesterday


















    • that works well I want to know for the future what is wrong.
      – Yuval Amir
      yesterday
















    that works well I want to know for the future what is wrong.
    – Yuval Amir
    yesterday




    that works well I want to know for the future what is wrong.
    – Yuval Amir
    yesterday












    up vote
    0
    down vote













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






    share|improve this answer








    New contributor




    ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













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






      share|improve this answer








      New contributor




      ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









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






        share|improve this answer








        New contributor




        ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









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







        share|improve this answer








        New contributor




        ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 days ago









        ats

        162




        162




        New contributor




        ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        ats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238320%2ftrying-to-convert-dec-to-hex-in-c-sharp-but-every-time-i-try-this-code-it-fails%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation