简体   繁体   English

如何将此十六进制字符串转换为长字符串?

[英]How to convert this hex string into a long?

I have: "0xE94C827CEB" in hex but as a string.我有:“0xE94C827CEB”在十六进制但作为一个字符串。

Which is: 1002011000043 (dd mm yyyy HH mm ss)即: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.)不幸的是,如果我只有字符串格式,我不知道如何进行转换,而且我没有 Convert.ToLong("0xE94C827CEB", 16) 函数,因为我使用的是 .NET Micro Framework(也,没有可用的 NumberStyles 命名空间。)

Is there a function out there that will convert this for me?有没有一个函数可以为我转换这个?

Thanks谢谢

For those of you looking for the answer using the full .NET framework for pc.对于那些使用适用于 pc 的完整 .NET 框架寻找答案的人。

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

see: MSDN Documentation请参阅: MSDN 文档

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():我不知道有什么函数可以做到这一点,但我认为您可以通过拆分十六进制字符串并通过 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

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 ;
}

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你去吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM