2

I have a csv file that stores long values in this format:

0x0000000000000001
0x0000000000000002
0x0000000000000020
0x0000000000040000

I want to save the values in long variables. I can save the data directly as long for example:

long t = 0x0000000000040000;

This does not throw an exception.

However, I can't seem to convert this format from string (as i read it from the csv file) to long.

I tried

Convert.ToInt64("0x0000000000040000");

as suggested in this question

I also tried

long.Parse("0x0000000000040000", NumberStyles.HexNumber);

But this throws also an exception

I can parse it when I get rid of the 0x at the front. But the number is then saved without the zeros (e.g. 0x0000000000000001 becomes just 1), which is not what I want.

Tl;dr:

Does anyone know how to parse

string s = "0x0000000000000001"; 

to

long l = 0x0000000000000001;

?

Community
  • 1
  • 1
Tobi o' Bobi
  • 197
  • 9
  • 1
    Possible duplicate of [How to parse hex values into a uint?](http://stackoverflow.com/questions/98559/how-to-parse-hex-values-into-a-uint) – Grundy Feb 18 '16 at 08:44
  • 1
    use `s.Subtring(2)`, thus you remove the first two characters `0x` – Ian Feb 18 '16 at 08:45
  • 3
    »But the number is then saved without the zeros (e.g. 0x0000000000000001 becomes just 1), which is not what I want.« -- The number is the same. If you want to *format* it differently when writing it somewhere (e.g. `l.ToString("x16")`), that's another point, but you shouldn't usually care about how a number is represented when it makes no difference at all. – Joey Feb 18 '16 at 08:46
  • @Joey you are totally right. Now I feel stupid... – Tobi o' Bobi Feb 18 '16 at 08:53
  • Or this: http://stackoverflow.com/questions/4279892/convert-a-string-containing-a-hexadecimal-value-starting-with-0x-to-an-integer – i486 Feb 18 '16 at 08:56

3 Answers3

2

Something like this:

   String source = "0x0000000000040000";

   long result = Convert.ToInt64("0" + source.TrimStart('0', 'x'), 16);

or even

   long result = Convert.ToInt64(source, 16); 

To convert back to string:

   String back = String.Format("0x{0:x16}", result);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    `Convert.ToInt64(source.TrimStart('0', 'x'), 16)` is problematic with `"0x0000000000000000"`. Edited: Removed wrong statement from comment, sorry. – Jeppe Stig Nielsen Feb 18 '16 at 08:49
  • @Jeppe Stig Nielsen: you're quite right: `0x0` is a *counter example*, I've edited the answer: `"0" + source.TrimStart('0', 'x')` – Dmitry Bychenko Feb 18 '16 at 08:54
  • @Dimitry Good answer, but the trimming part is completely pointless, `Convert.ToInt64` handles the `0x` hex prefix just fine. – Gediminas Masaitis Feb 18 '16 at 08:54
  • Interesting, on my machine (.NET vers. 4.5.2), `Convert.ToInt64(null, 16)` succeeds and gives zero, while `Convert.ToInt64("", 16)` blows up with a "wrong" exception (expected `FormatException` but got `ArgumentOutOfRangeException` (from a deeper point in the call stack)). *Edit:* Hmm, I note that this behavior is documented. Still surprising to me. – Jeppe Stig Nielsen Feb 18 '16 at 08:58
1

One alternative is that you could simply omit the first two characters for your conversion by using Substring method:

string s = "0x0000000000040000";
long l = Convert.ToInt64(s.Substring(2), 16);
Ian
  • 30,182
  • 19
  • 69
  • 107
0

This code converts hex to int and checks for invalid inputs plus parses 0x while handling 0x00 too

if (int.TryParse(stringname.TrimStart('0').TrimStart('x'), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int result))
{
    //use the value of "result"
}
else
{
    //check the input
}

You need to add

using System.Globalization;