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);
}
c#
add a comment |
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);
}
c#
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
add a comment |
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);
}
c#
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#
c#
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
add a comment |
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
add a comment |
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();
}
that works well I want to know for the future what is wrong.
– Yuval Amir
yesterday
add a comment |
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).
New contributor
add a comment |
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();
}
that works well I want to know for the future what is wrong.
– Yuval Amir
yesterday
add a comment |
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();
}
that works well I want to know for the future what is wrong.
– Yuval Amir
yesterday
add a comment |
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();
}
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();
}
answered 2 days ago
Eva
276
276
that works well I want to know for the future what is wrong.
– Yuval Amir
yesterday
add a comment |
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
add a comment |
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).
New contributor
add a comment |
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).
New contributor
add a comment |
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).
New contributor
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).
New contributor
New contributor
answered 2 days ago
ats
162
162
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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