Solved

Please someone fix this code

Posted on 2003-12-04
2
268 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

15 Experts available now in Live!

Get 1:1 Help Now