-2

I have this code:

string IDNumber = "0x0037D70D";

long x = 0x0037D70D; //Working

long x = long.Parse(IDNumber); //Error Input string was not in a correct format.

Required to send the string above (IDNumber) to long y and should maintain the variable format and value typical as its start with 0x.... same as in the IDNumber string

Kindly help me.

Edit:

I have function in DLL file, this function accept one parameter with data type long

If I give this long parameter the value like 0x0037D70D then the function is working correctly and do the required job but if I give the long parameter the value in any other format like 3659533 function is not working

string example1 = "0x0037D70D";
long example2 = 0x0037D70D;

At the end I have the value coming in string format like example1 which I want to convert to be like example2 because if I have the value written like example2 format and saved in long variable then is working

Update: The problem solved, I use this function to communicate with external hardware device and after many times trying the device hangs, I rest the device and the solution advised by @Kirill Polishchuk working for me.

long l = Convert.ToInt64(IDNumber, 16);
  • 1
    This duplicate answers your question: [Convert integer to hexadecimal and back again](https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again) – Ňɏssa Pøngjǣrdenlarp Jun 24 '20 at 23:14
  • Please include the error you are receiving. – Llama Jun 25 '20 at 00:14
  • @Ňɏssa Pøngjǣrdenlarp this not the case because I want to keep the hexadecimal as its in the long variable without convert it to integer – Waleed ELerksosy Jun 25 '20 at 07:48
  • @John the error I got is Input string was not in a correct format. – Waleed ELerksosy Jun 25 '20 at 07:49
  • `long` is short for "long integer". Hexadecimal is a string representation of binary data. – Llama Jun 25 '20 at 07:54
  • Please advise what you want to do and why the two answers are not suitable for you. – Llama Jun 25 '20 at 08:04
  • Something else must be at play here. Using Kirill's method, the resulting value is [identical](https://rextester.com/EQD49441). This seems to be [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Llama Jun 25 '20 at 08:28
  • I use this function with external hardware device and I found that the problem was in the hardware that hangs after many times try, after reset the device I became able to use the advised solutions and is working now. Thank you for help and support – Waleed ELerksosy Jun 25 '20 at 09:14

2 Answers2

1

You should remove 0x prefix:

long y = long.Parse(IDNumber.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
long x = 0x0037D70D; //Working
Console.WriteLine(x.ToString("X")); //prints "37D70D", no prefix
Guru Stron
  • 21,224
  • 3
  • 19
  • 37
1

I would suggest using Convert class:

long l = Convert.ToInt64(IDNumber, 16);
Kirill Polishchuk
  • 51,053
  • 10
  • 118
  • 119
  • This will convert the hexadecimal to integer and the result will be 3659533 and not 0x0037D70D – Waleed ELerksosy Jun 25 '20 at 07:20
  • @WaleedELerksosy ...and what exactly is wrong with that? [`0x0037D70D` is a hexadecimal representation of `3659533`](https://i.stack.imgur.com/xU9hv.png). What else could you possibly be trying to do? If you want it in a hexadecimal (string) format, why are you converting it to a long? – Llama Jun 25 '20 at 07:50
  • I have function in DLL file, this function accept one parameter with data type long If I give this parameter the value like 0x0037D70D then the funcation is working correctly and do the required job but if I do it in any other format like 3659533 function is not working string example1 = "0x0037D70D"; long example2 = 0x0037D70D; At the end I have the value coming in string format like example1 which I want to convert to be like example2 because if I have the value written like example2 then is working – Waleed ELerksosy Jun 25 '20 at 08:21
  • @John I updated the question :) – Waleed ELerksosy Jun 25 '20 at 08:24
  • 1
    Thank you, problem solved :) – Waleed ELerksosy Jun 25 '20 at 09:15
  • @WaleedELerksosy, cool – Kirill Polishchuk Jun 28 '20 at 22:03