Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:50:29+00:00 2026-05-29T05:50:29+00:00

I am coding a converter in C# to convert from integer, binary and hexadecimal

  • 0

I am coding a converter in C# to convert from integer, binary and hexadecimal into same formats. Of course the input format and the output format is given.
Another interesting point is that my input is a string and my output is also a string.

So, now I wonder if there is a way to do all those conversions using the same function because in all questions I explored some solutions are given for only one of my 6 cases and I don’t find it really elegant.

To summarize:

Input String | Output String
-------------|--------------
int32        | hexa
int32        | binary
binary       | int32
binary       | hexa
hexa         | int32
hexa         | binary

EDIT: All exceptions will be handled with try-catch if necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:50:30+00:00Added an answer on May 29, 2026 at 5:50 am

    Make it a two-step process: parsing a string from one of the three formats, and then convert to one of the three formats.

    To parse, you can use the respective Parse (or TryParse if you want to avoid exceptions) methods which exist for the different numeric types. On integer types, you can use the NumberStyles.HexNumber to parse from a hex number.

    To convert to a string, use the overloaded ToString with the appropriate culture and format.

    Note that you can do type conversions through the IConvertible interface, which is supported by all native number types.

    Here’s some pseudocode (will not compile but should illustrate the points made):

    enum NumberKind {
      Int32,
      Decimal,
      Hexa
    }
    
    string ConvertNumber(NumberKind inputKind, string inputValue, NumberKind outputKind) {
      IConvertible intermediate;
      switch (inputKind) {
      case NumberKind.Int32:
        intermediate = Int32.Parse(inputValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
        break;
      case NumberKind.Decimal:
        intermediate = Decimal.Parse(inputValue, NumberStyles.Number, CultureInfo.InvariantCulture);
        break;
      case NumberKind.Hexa:
        intermediate = Int32.Parse(inputValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        break;
      }
      switch (outputKind) {
      case NumberKind.Int32:
        return intermediate.ToInt32().ToString("D", CultureInfo.InvariantCulture);
      case NumberKind.Decimal:
        return intermediate.ToDecimal().ToString("G", CultureInfo.InvariantCulture);
      case NumberKind.Hexa:
        return intermediate.ToInt32().ToString("X", CultureInfo.InvariantCulture);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Microsoft's XAML/HTML converter to convert HTML from a database into a
I am coming from VB6 and I am starting to convert an existing VB6
I need to convert tons of pictures, coming from all kinds of sources, in
I am coding a little bookmarket to convert all the devises in the current
I've been trying to get back into coding for a while, so I figured
Possible Duplicate: Tool to convert python indentation from spaces to tabs? I have a
I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I'm trying to convert my code to Mootools (I like the coding paradigm better).
I'm trying to convert some XML data coming from a CLOB to a XMLType
It's a coding practice. I read these numbers as double from a file: 112233

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.