decimal to binary, octal, hex conversion

using conversion function Convert.ToInt64(value, base);

Page 1 of 1

5 Replies - 37784 Views - Last Post: 18 September 2009 - 06:12 PM Rate Topic: -----

#1 Charlie IronGleet   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

decimal to binary, octal, hex conversion

Post icon  Posted 18 September 2009 - 11:52 AM

Hello,

I am trying to write a program that converts decimal numbers to binary, octal, and hex.

I want to use the c# library conversion function. Convert.ToInt64

for example, to convert 57 to binary Convert.ToInt64(57, 2)

Here is my difficulty, how do I type the following variable i?

i = Convert.ToInt64(57,2)?

There is no "binary" type, or hex type, or octal type, so how is this done?

Could someone throw up a line of sample code that would do the following for me:

Take int x = 57;
and use Console.WriteLine(......Convert.ToInt64(57,2))
to output the binary value to the console?

Thanks for your help!
IronGleet

Is This A Good Question/Topic? 0
  • +

Replies To: decimal to binary, octal, hex conversion

#2 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 01:18 PM

You have it backwards. Convert.ToInt64 is used to convert a value to an integer, not convert to different bases. The overload you are talking about would take a string as an argument say "100101", which is binary. When you would convert it to an integer you would use this:

myInt = Convert.ToInt64("100101", 2);


Was This Post Helpful? 1

#3 Charlie IronGleet   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 01:42 PM

View PostSixOfEleven, on 18 Sep, 2009 - 12:18 PM, said:

You have it backwards. Convert.ToInt64 is used to convert a value to an integer, not convert to different bases. The overload you are talking about would take a string as an argument say "100101", which is binary. When you would convert it to an integer you would use this:

myInt = Convert.ToInt64("100101", 2);




So then ARE there any built in functions that wilil take a decimal as an argument and convert it to binary, octal, and/or hex?

Or, is that something we have to write ourselves?

Thanks,
IronGleet
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 01:58 PM

There is the Convert.ToString(57, 2); That would convert 57 to binary as a string.
Was This Post Helpful? 1
  • +
  • -

#5 crepitus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 03:13 PM

View PostCharlie IronGleet, on 18 Sep, 2009 - 10:52 AM, said:

There is no "binary" type, or hex type, or octal type, so how is this done?


That's right, because binary, base 10, and hex are just different ways of writing down a number. You can be talking about the same value, say 10 as in 10 fingers, but you would write it differently in binary, octal, base 10 or hex.

In programming you store values in numeric types - byte, integer, long etc, and you display them as Strings.

So, your conversion that you are looking is from a numeric type to a String, with the String being formatted in a particular way.

Random rand = new Random();
int value = rand.Next(0, 1001);
String binaryRepresentation = Convert.ToString(value, 2);
String octalRepresentation = Convert.ToString(value, 8);
String base10Representation = Convert.ToString(value);
String hexRepresentation = Convert.ToString(value, 16);			
// Any other bases and you are on your own.

// Go the other way.			
int binary = Convert.ToInt32(binaryRepresentation, 2);			
int octal = Convert.ToInt32(octalRepresentation, 8);
int base10 = Convert.ToInt32(base10Representation);
int hex = int.Parse(hexRepresentation, System.Globalization.NumberStyles.HexNumber); // alternative
Console.WriteLine("Binary: " + binaryRepresentation + " = " + binary.ToString());
Console.WriteLine("Octal: " + octalRepresentation + " = " + octal.ToString());
Console.WriteLine("Base10: " + base10Representation + " = " + base10.ToString());
Console.WriteLine("Hex: " + hexRepresentation + " = " + hex.ToString());


Mod Edit: When you are posting code please post it tags like these
:code:
Was This Post Helpful? 0
  • +
  • -

#6 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 06:12 PM

I have a complete tutorial set up on my blog. Check it out:

http://easytask.word.../2009/04/28/40/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1