3

I want to Convert hexadecimal numbers to binary numbers.I search some posts but couldn't find a c# language program.

I used this:

value = 0xFFFF;
decimalNum = Convert.ToString(value, 16);
Console.WriteLine(value);

and then I converted it to binary number. Are there any easier and fast way to do this?

1
  • Easier than a single method call for each base conversion? I would assume the .NET runtime implementation of ToString() will perform well. Commented Sep 3, 2015 at 22:37

2 Answers 2

2

You can do this:

var value = 0xFFFF; 
value = Convert.ToString(Convert.ToInt32(value.ToString(), 16), 2);
Console.WriteLine(value);
// 1100101010100110101
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Jonathan this is what I want!
Semicolon. five characters to go
Thanks Millie. Fixed ;)
0
// Generate date in hex
DateTime dt = new DateTime();
dt = DateTime.Now;
string str = dt.ToString("yyyyMMddhhmmss");
string hexDate = dt.Ticks.ToString("X2");

// Convert hex date to date string
UInt64 numericDate = UInt64.Parse(hexDate, 
       System.Globalization.NumberStyles.AllowHexSpecifier);
long binaryDate = Convert.ToInt64(numericDate);
string strDate = DateTime.FromBinary(binaryDate).ToShortDateString();

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.