Community discussion forum

How to Develop a programm to Convert Binary to Dec,Hex,Oct

Tags: csharp India
  • 2 years ago
    hi Friends

    Can you post me the Code in C# where i could convert Decimal to Binary or Binary to decimal
    or Convert binary to Hexa or Octal Number system with out using any exsiting function that may be avalable

    Thank you








  • 2 years ago

    Here is a link to a page that explains how to convert decimal to hex, as an algorithm.

    The same method can be used to convert decimal to any base. (8 for octal, 4.. 2 for binary...)

    http://www.permadi.com/tutorial/numDecToHex/

    In essense, it is: 

    Divide your number by the base. note the value, and the modulus (what is left over)

     

    the reverse process  is

    Take the right most character

    Add this to the next character, multiplied by the base

    Add the next character, multiplied by base power 2, and so on.

    Example for binary:

    value 1101 becomes

    1   plus

    0 x 2  plus

    1 x 4 plus

    1 x 8

    = 13 decimal

    if 1101 was hex, the decimal value would be

    1 +  (0x 16)  + (1 x 256) + (1 x 4096) =   4353

     

     

     

     

     

  • 2 years ago
    i got what you want to say but the problem is that when you start programming
    the matter is different
    e.g. when you say that you want to saperate the string into indivigual carater it can be done
    "STRING"
    'S'  'T'  'R'  'I'  'N'  'G'  this is correct for a string
    but for number it is dificult
    e.g. 1100
    '1'  '1'  '0'  '0'
    this how can i do this to indivigualy calculate  which can be used for convertion









  • 2 years ago

    <Variable> .ToString   will turn the number into a string.

    Once it is a string, then '1' is the same as 'F'

    You can use SubString functions to get individual letters

  • 2 years ago

    I wrote a program which includes converting IP address to Decimal and  Hex.I hope this sample can give you some ideas.

    http://www.treaple.com/ArticlesDetails.aspx?id=55

     regards,

  • 2 years ago
    "with out using any exsiting function"

    Does this mean that this is a school assignment?  If so then I really do encourage you not to simply ask for the completed solution.  What I would suggest you do is research different number base systems and attempt to devlope your algorithm from your research.  You can ask about specfic problems/difficulties that arise, but asking for the solution ready made to turn in is dishonest and unproductive.

    If that is not the case than I apologize but let it serve as advice to everyone else.

    And to add to the many links, I wrote an article about this:  http://www.developerfusion.co.uk/show/5046/







  • 2 years ago
    hi Nimpo

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!