Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this hex string into a long?

I have: "0xE94C827CEB" in hex but as a string.

Which is: 1002011000043 (dd mm yyyy HH mm ss)

Unfortunately I don't know how to do the conversion if I only have it in string format, and I don't have a Convert.ToLong("0xE94C827CEB", 16) function because I'm using the .NET Micro Framework (also, don't have NumberStyles namespace available.)

Is there a function out there that will convert this for me?

Thanks

like image 939
Daniel Minnaar Avatar asked May 15 '13 16:05

Daniel Minnaar


People also ask

How can convert string to long?

There are many methods for converting a String to a Long data type in Java which are as follows: Using the parseLong() method of the Long class. Using valueOf() method of long class. Using constructor of Long class.

How do you convert hex to string?

In order to convert a hex string into a normal string, the hex string has to be converted into a byte array, which is indexed and converted into smaller hex strings of two digits. The smaller hex strings are then concatenated into a normal string. For some values, a two digit hex string will start with a zero.

How long is a hex string?

Hex, like decimal, combines a set of digits to create large numbers. It just so happens that hex uses a set of 16 unique digits.

Can string be converted to long java?

We can convert String to long in java using Long. parseLong() method.


2 Answers

For those of you looking for the answer using the full .NET framework for pc.

long answer = Convert.ToInt64("E94C827CEB",16);

see: MSDN Documentation

like image 177
Zapnologica Avatar answered Sep 29 '22 00:09

Zapnologica


I don't know of any function to do it, but I think you can do it quite simply by splitting the hex string and passing each part through Convert.ToInt32():

int part1 = Convert.ToInt32("E9", 16)
int part2 = Convert.ToInt32("4C827CEB", 16) //the last 4 bytes
long result = part1 * 4294967296 + part2  //4294967296 being 2^32. Result = 1002011000043
like image 29
Weyland Yutani Avatar answered Sep 29 '22 00:09

Weyland Yutani