# decimal to binary, octal, hex conversion

Page 1 of 1

## 5 Replies - 37784 Views - Last Post: 18 September 2009 - 06:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=126626&amp;s=4436dc63e73955422f9d4021cf3ae09e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Charlie IronGleet

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

# decimal to binary, octal, hex conversion

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?

IronGleet

Is This A Good Question/Topic? 0

## Replies To: decimal to binary, octal, hex conversion

### #2 SixOfEleven

• Planeswalker

Reputation: 1055
• 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);

```

### #3 Charlie IronGleet

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

## Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 01:42 PM

SixOfEleven, 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

### #4 SixOfEleven

• Planeswalker

Reputation: 1055
• 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.

### #5 crepitus

• D.I.C Regular

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

## Re: decimal to binary, octal, hex conversion

Posted 18 September 2009 - 03:13 PM

Charlie 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

### #6 papuccino1

• His name was Robert Paulson.

Reputation: 63
• 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: