Solved

Please someone fix this code

Posted on 2003-12-04
2
266 Views
Last Modified: 2010-04-16
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
Comment
Question by:jayrod
2 Comments
 
LVL 3

Expert Comment

by:WiB
ID: 9875028
what about to add
input = input.ToUpper();
in base16to10()?
0
 
LVL 10

Accepted Solution

by:
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now