Welcome to Dream.In.Code
Become a C# Expert!

Join 150,386 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,078 people online right now. Registration is fast and FREE... Join Now!




Decimal>Octal, Hexadecimal, Binary

 
Reply to this topicStart new topic

Decimal>Octal, Hexadecimal, Binary, Making a small calculator.

papuccino1
2 Mar, 2008 - 02:18 PM
Post #1

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Edit: I've finally managed to make the Decimal to Octal conversion work. Octal to decimal is the same, just work reverse it. I'll make a small tutorial when everything is done 100%.

In the meantime I have one other problem.

I use this piece of code to convert:
CODE
Int64 x =Convert.ToInt64(value,8);
Where 8, is the base number you want to convert to.

The main problem is if I want to convert to Hex, I can't just put ToInt64(value,16); because of the A-F characters.

How can I work around this guys?

This post has been edited by papuccino1: 2 Mar, 2008 - 02:34 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Decimal>Octal, Hexadecimal, Binary
2 Mar, 2008 - 05:12 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
QUOTE(papuccino1 @ 2 Mar, 2008 - 02:18 PM) *

CODE
Int64 x =Convert.ToInt64(value,8);
Where 8, is the base number you want to convert to.


The 8 in your code represents the base that you are converting from, not to. So if you are converting from hexadecimal to an Int64, then that is how you would convert it using a 16 in place of the 8.

To convert to hex, use the ToString method and store the result in a string variable.

CODE

string x = value.ToString("X")

User is offlineProfile CardPM
+Quote Post

papuccino1
RE: Decimal>Octal, Hexadecimal, Binary
2 Mar, 2008 - 05:15 PM
Post #3

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Thanks for the tip.

Would you mind telling me exactly what Int64 means? I just copied it from MSDN and it worked. But I want to know what it means/does.

User is offlineProfile CardPM
+Quote Post

killnine
RE: Decimal>Octal, Hexadecimal, Binary
2 Mar, 2008 - 06:34 PM
Post #4

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
QUOTE(papuccino1 @ 2 Mar, 2008 - 06:15 PM) *

Thanks for the tip.

Would you mind telling me exactly what Int64 means? I just copied it from MSDN and it worked. But I want to know what it means/does.




IntXX is the size of the integer. Int64 means it is a 64 bit integer, and the compiler allocates enough memory to handle 64-bit integers (9223372036854775807 to -9223372036854775808 I think...., just pulled it off a site real quick).

To make integers even bigger you need to look into big-endian and little-endian integer creation, but for another day...


EDIT: oh yeah, its SIGNED, too. That means that there is a bit to determine if the value is positive or negative.

This post has been edited by killnine: 2 Mar, 2008 - 06:36 PM
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: Decimal>Octal, Hexadecimal, Binary
2 Mar, 2008 - 07:02 PM
Post #5

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Here's my code to convert:

CODE
string n1 = Convert.ToString(textBox2.Text);
            Int64 x = Convert.ToInt64(n1, 10);
            return x;


Is something wrong with my code? When I press the button to convert (to use this piece of code) I just get the same number in textbox placed in the label.

Some help?

This post has been edited by papuccino1: 2 Mar, 2008 - 07:02 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Decimal>Octal, Hexadecimal, Binary
2 Mar, 2008 - 08:25 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Topics merged. Do not create duplicate posts. All your questions relate to the same thing, conversion, and do not require more than one topic.
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: Decimal>Octal, Hexadecimal, Binary
3 Mar, 2008 - 06:15 AM
Post #7

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
QUOTE(papuccino1 @ 2 Mar, 2008 - 08:02 PM) *

Here's my code to convert:

CODE
string n1 = Convert.ToString(textBox2.Text);
            Int64 x = Convert.ToInt64(n1, 10);
            return x;


Is something wrong with my code? When I press the button to convert (to use this piece of code) I just get the same number in textbox placed in the label.

Some help?


Can someone please respond?

User is offlineProfile CardPM
+Quote Post

Enslave
RE: Decimal>Octal, Hexadecimal, Binary
26 Mar, 2008 - 01:06 AM
Post #8

New D.I.C Head
*

Joined: 26 Mar, 2008
Posts: 1

QUOTE(papuccino1 @ 2 Mar, 2008 - 08:02 PM) *

Here's my code to convert:

CODE
string n1 = Convert.ToString(textBox2.Text);
            Int64 x = Convert.ToInt64(n1, 10);
            return x;


Is something wrong with my code? When I press the button to convert (to use this piece of code) I just get the same number in textbox placed in the label.

Some help?


What you do here is converting a string from decimal to an int, and then returning the int. If you get the same value in the label that you entered in textBox2, it's not because of this piece of code.

If the target is that you want to see the data hexadecimal or something like that, you have to tell the program to output the data in that way.

For example:

CODE

Int64 x = yourExtractionMethod(); //To get the value as an int
label.Text = x.ToString("X"); //This will output your data in hexadecimal


Hope this shed some light. =)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month