0

While i was writing today i've met a very stupid problem: On one place i must give a long value like 0x70, 0x3BC0 or something like that. This values i must take from a text file. I am using System.IO like this:

string[] stringTakenByFile = File.ReadAllLines(@"thePathToFile.txt");

From here i can take the value but in string format. I mean the same value like 0x70 but not like a long or int but like a text (string). When i try to parse it or convert it from string to int/long it gives me an exception witch says: Input string was not in a correct format. If you could help me that would be awesome! Thanks! EDIT: I dont wanna "How to convert numbers between hexadecimal and decimal in C#?" i need from string to LONG

Ch00pi
  • 47
  • 1
  • 8
  • 3
    And how do you try to convert it? – Bgl86 Sep 07 '15 at 14:47
  • 1
    string variableWhereIStoreTheStringResult; long variableWhereIwannaStoreTheResult = Convert.ToInt64(variableWhereIStoreTheStringResult); long variableWhereIwannaStoreTheResult = int64.Parse(variableWhereIStoreTheStringResult); – Ch00pi Sep 07 '15 at 14:48
  • 2
    `Convert.ToInt64("Your Hex String", 16);` should be the thing you are looking for. 16 here is the number base. – Alpay Sep 07 '15 at 14:51
  • 2
    try this code hexNumber = hexNumber.Replace("x", string.Empty); long result = 0; long.TryParse(hexNumber, System.Globalization.NumberStyles.HexNumber, null, out result); – Std_Net Sep 07 '15 at 14:53
  • @Std_Net , would you post it like an answer. This works PERFECT! – Ch00pi Sep 07 '15 at 14:59

1 Answers1

0

Update : see @Std_Net answer in post comments for an updated solution

If you want to parse a Long in hexa format you must specify it like this :

Long.parseLong(myStringToConvert, 16);

See MSDN documentation

bviale
  • 5,245
  • 3
  • 28
  • 48