How to convert this hex string into a long?
Asked Answered
A

4

17

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

Antlia answered 15/5, 2013 at 16:6 Comment(1)
took a few moments to find it msdn.microsoft.com/en-us/library/ee435453.aspxInsignificant
S
17

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
Secrest answered 15/5, 2013 at 16:26 Comment(0)
B
40

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

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

see: MSDN Documentation

Brazil answered 14/10, 2015 at 9:4 Comment(1)
Works for .NET Core 3.1.Borden
S
17

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
Secrest answered 15/5, 2013 at 16:26 Comment(0)
N
4

Kick it old-school and roll your your own. This is not exactly rocket science here:

public ulong HexLiteral2Unsigned( string hex )
{
    if ( string.IsNullOrEmpty(hex) ) throw new ArgumentException("hex") ;

    int i = hex.Length > 1 && hex[0]=='0' && (hex[1]=='x'||hex[1]=='X') ? 2 : 0 ;
    ulong value = 0 ;

    while ( i < hex.Length )
    {
        uint x = hex[i++] ;

        if      ( x >= '0' && x <= '9' ) x =   x - '0' ;
        else if ( x >= 'A' && x <= 'F' ) x = ( x - 'A' ) + 10 ;
        else if ( x >= 'a' && x <= 'f' ) x = ( x - 'a' ) + 10 ;
        else throw new ArgumentOutOfRangeException("hex") ;

        value = 16*value + x ;

    }

    return value ;
}
Naarah answered 15/5, 2013 at 17:27 Comment(0)
A
1

using the bit shift operators can clean up some of this code --

  static long HexToLong(string hexString)
    {
        if (!string.IsNullOrWhiteSpace(hexString))
        {
            hexString = hexString.Trim();
            if (hexString.StartsWith("0x"))
                hexString = hexString.Substring(2);
            if (hexString.Length > 16)
                throw new FormatException("Input string is too long");
            hexString = hexString.PadLeft(16, '0'); // ensure proper length -- pad the leading zeros

            int part1 = Convert.ToInt32(hexString.Substring(0, 8), 16); // first 4 bytes
            int part2 = Convert.ToInt32(hexString.Substring(8, 8), 16);//the last 4 bytes
            return (part1 << 8 * 4) + part2;  // slide it on over -- 8 bits per byte
        }
        return 0;
    }

There you go

Aut answered 30/11, 2020 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.