Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently working on a school project that is supposed to convert IP Adresses into another number system, while keeping the general 'syntax' of an IP adress and do the same thing vice versa and I am currently running into a problem while trying to convert a hexadecimal value back to a decimal value.

The procedure I use to convert from decimal to hex, for example, would look like this

string DecimalToHex(string input)
{
    return String.Join(".", (input.Split('.').Select(x => Convert.ToString(Int32.Parse(x), 16))).ToArray());
}


With (string input) being the text of a textbox. And if that input is "c0", I'd like the output to - of course - be 192.

What I have tried:

I've tried a few things I found around the net, like working with System.Globalization.NumberStyles.HexNumber, but to no avail so far.
Posted
Updated 4-Dec-18 23:42pm

1 solution

Start by avoiding compound statements like that, where you cannot see the results at each step. Use String.Split to get an array containing the four fields, then convert each one to its integer value. From that you can use the String.Format Method (System) | Microsoft Docs[^] to display them as decimal values. And a minor change would allow you to convert decimal strings to hex.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900