[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Convert bases decimal, hexadecimal, octal, and binary in C#

convert bases

It's easy to convert bases if you use the Convert class's ToInt64 and ToString methods. For example, the following statement parses the text in the TextBox named source and saves the result in the long variable value. The "16" means the method should parse the text as a base 16 (hexadecimal) value.

value = Convert.ToInt64(source.Text, 16);

The following code does the opposite: it converts the value in variable value into a hexadecimal string and displays it in the TextBox named txtHexadecimal.

txtHexadecimal.Text = Convert.ToString(value, 16)

To parse and display values in other bases, simply replace 16 with the base: 8 for octal and 2 for binary.

I told you it was easy!

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.