Solved

Please someone fix this code

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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

752 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