0

I'm currently tasked with building a programm that can convert different numeric strings into other number systems I've been mostly using methods similar to this

    string ConvToDec(string input)
    {
        return String.Join(".", (input.Split('.').Select(x => Convert.ToString(Convert.ToInt32(x, 2)))).ToArray());
    }

To convert, which I can't seem to get working when I want to convert from Hex to Dec. I've tried a few solutions I've found on here like the ones found in this thread, but nothing seemed to work for me.

S. Kohl
  • 1
  • 1
  • 1
    Can you show what's on input? Also `ConvToDec` sounds misleading, if your input always a hex string, then name it something like `ConvHexToDec` or something. – SᴇM Dec 05 '18 at 10:10
  • @SeM input will be the contents of a textbox, executed it looks like this `TextBox2.Text = ConvToDec(TextBox1.Text);` – S. Kohl Dec 05 '18 at 10:12
  • 1
    It doesn't really matter from where your input will come. Can you show what you have on your input (what have you tested)? – SᴇM Dec 05 '18 at 10:13
  • That's another method I am using and I actually got to work properly, as an example of how it is 'supposed' to look like. Anyhow, I test it like [this](https://i.imgur.com/CbWvicH.png) – S. Kohl Dec 05 '18 at 10:18
  • Can you just add a `string input` value into your question please? – SᴇM Dec 05 '18 at 10:21
  • Could you provide *some examples*, please? Input and the desired output? – Dmitry Bychenko Dec 05 '18 at 10:28
  • You need to be clearer about your input and output. A 'hex-string' could have any length. A 'decimal string', too. But the conversion is not clear at all. If it is supposed to be a number the length/size is important and limited. If you want to use it in a hex-editor it is not about numbers but about bytes.. – TaW Dec 05 '18 at 10:29
  • Possible duplicate of [How to convert numbers between hexadecimal and decimal in C#?](https://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c) – ikerbera Dec 05 '18 at 10:34

1 Answers1

1

Convert.ToInt32(x, 2) converts a binary number string to an int (that's what the 2 says).

Use Convert.ToInt32(x, 16) in your expression above to convert a hex string to an int.

Input c0.80 will then give 192.128 as output.

Haukinger
  • 10,420
  • 2
  • 15
  • 28