Solved

Please someone fix this code

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
The Using statement in C# 5 37
How to compare two table schema using c#? 6 32
ConsoleSql 1 21
Events in static methods 3 31
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

8 Experts available now in Live!

Get 1:1 Help Now