Solved

# Please someone fix this code

Posted on 2003-12-04
271 Views
I'm trying to convert numbers.. The first method works great. but the second one doesnt:

/// <summary>
/// Convert an integer value to hex
/// </summary>
/// <param name="number">Accepts an integer number</param>
/// <returns>String Hex value</returns>
public static String base10to16(int number)
{
String[] hexnumbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
String hex = "";
int remainder;
do
{
remainder = number % 16;
number = number / 16;
hex = hexnumbers[remainder] + hex;
} while (number>0);
return hex;
}

/// <summary>
/// Converts a hex number to integer
/// </summary>
/// <param name="input">Accepts a String</param>
/// <returns>Integer value</returns>
public static int base16to10(string input)
{
string hexnumbers = "0123456789ABCDEF";
int number = 0;
for (int i= 0;i<input.Length;i++)
{
number *= 16;
number += hexnumbers.IndexOf(input[i]);
}
return number;
}
0
Question by:jayrod
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Expert Comment

ID: 9875028
input = input.ToUpper();
in base16to10()?
0

LVL 10

Accepted Solution

Duy Pham earned 500 total points
ID: 9875144
You forgot that the order of the Hex string is right to left so base16to10() must be written like :

public static int base16to10(string input)
{
string hexnumbers = "0123456789ABCDEF";
int number = 0;
for (int i = input.Length;i>0;i--)
{
number *= 16;
number += hexnumbers.IndexOf(input[i-1]);
}
return number;
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
###### Suggested Courses
Course of the Month2 days, 8 hours left to enroll