Solved

Please someone fix this code

Posted on 2003-12-04
2
272 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
[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
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 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