Java将十六进制转换为时间

发布于 2024-10-20 03:02:32 字数 342 浏览 2 评论 0原文

我真的不知道从哪里开始,我做了一些研究,但找不到任何东西。我知道我必须使用日期类,但我需要做的是提取下面突出显示的日期,但十六进制值是 AA 它从哪里获取日期值?

图片在这里,因为它不允许我上传图片: http ://www.facebook.com/photo.php?pid=2298915&l=e45630aead&id=1283154964

如果有人知道我将非常感激!

非常感谢

I really have no idea where to start on this, I have done some research but been unable to find anything. I know I have to use the date class but what i need to do is pull out the date as highlighted below but the hex value is AA where is it getting the date value from?

image is here as it wont let me upload an image: http://www.facebook.com/photo.php?pid=2298915&l=e45630aead&id=1283154964

If any one knows I would be most greatful!

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-10-27 03:02:32

Windows 在内部将 FileTime 存储为自 1.1.1601 UTC 以来的 100 纳秒数,作为 64 位字段。

您是否可以使用JNI并调用Windows API的FileTimeToSystemTime()?如果是的话请看这里:

http://msdn.microsoft.com/en -us/library/ms724280(VS.85).aspx

如果不是:

您知道这个 64 位字段位于您的数据结构中的位置吗?您指出了地址 03A0B00A(“AA”字节),但是我发现该字段不太可能位于此处,而不是 03A0B008 甚至 03A0B000 或 03A0B0C0。如果您不知道该字段的地址,您可以通过计算 100 纳秒格式的日期 (29.1.2011) 对其进行逆向工程;它不需要精确,您只需找到最高有效字节,这样您就知道 64 位字段所在的位置。我不知道 Java 是否允许您以相对于 1.1.1601 UTC 的纳秒为单位进行计算,但是正如我所说,它不需要精确:您可以使用 Java 计算自 1.1.1601 UTC 以来的天数,然后相应地相乘;您可以再次使用此估计来查找 64 位字段的地址,然后使用该地址来计算时间。

Windows stores FileTime internally as the number of 100-nanoseconds since 1.1.1601 UTC as a 64bit field.

Is it possible for you to use JNI and call Windows API's FileTimeToSystemTime()? If so have a look here:

http://msdn.microsoft.com/en-us/library/ms724280(VS.85).aspx

If not:

Do you know where this 64bit field is located in the datastructure you have there? You pointed out the address 03A0B00A (the 'AA' byte), however i find it unlikely that the field would be located there instead of maybe 03A0B008 or even 03A0B000 or 03A0B0C0. If you dont know the address of this field you might reverse-engineer it by calculating the date (29.1.2011) in the 100-nanosecond-format; it need not be precise, you just have to find the most significant bytes and thus you know where the 64bit field lies. I dont know if Java lets you calculate in nanoseconds relative to 1.1.1601 UTC, however as i said it need not be precise: you can use Java to calculate the days since 1.1.1601 UTC, and then multiply accordingly; you can again use this estimate to find the address of the 64bit field and from then on use that address to calculate the time.

维持三分热 2024-10-27 03:02:32

文件中的字节为:AA37 D608 DFBF CB01。

您可以在左侧列中看到,这被解释为 64 位整数 129407978957060010。如果将其转换回十六进制,您将看到该数字以“little-endian”格式存储:01CB BFDF 08D6 37AA。

所以你需要做的是:

    byte[] data = new byte[] { (byte) 0xAA, (byte) 0x37, (byte) 0xD6,
            (byte) 0x08, (byte) 0xDF, (byte) 0xBF, (byte) 0xCB, (byte) 0x01 };

    // convert bytes to long time
    long val = 0;
    for(int i=7;i>=0;i--) {
        val <<= 8;
        val += 0xff & data[i];
    }

    // convert 100 nanos to milliseconds
    val /= 10000;

    // convert to time offset from 1st Jan 1601 AD
    Calendar calend = Calendar.getInstance();
    calend.set(1601,0,01,00,00,00);
    calend.set(Calendar.MILLISECOND, 0);
    val += calend.getTimeInMillis();
    calend.setTimeInMillis(val);

    // display result
    DateFormat df = DateFormat.getDateTimeInstance();
    System.out.println(df.format(calend.getTime()));

The bytes in the file are: AA37 D608 DFBF CB01.

You can see in the left hand column that this is interpreted as the 64-bit integer 129407978957060010. If you convert that back to hex you will see that the number is stored in "little-endian" format: 01CB BFDF 08D6 37AA.

So what you need to do is:

    byte[] data = new byte[] { (byte) 0xAA, (byte) 0x37, (byte) 0xD6,
            (byte) 0x08, (byte) 0xDF, (byte) 0xBF, (byte) 0xCB, (byte) 0x01 };

    // convert bytes to long time
    long val = 0;
    for(int i=7;i>=0;i--) {
        val <<= 8;
        val += 0xff & data[i];
    }

    // convert 100 nanos to milliseconds
    val /= 10000;

    // convert to time offset from 1st Jan 1601 AD
    Calendar calend = Calendar.getInstance();
    calend.set(1601,0,01,00,00,00);
    calend.set(Calendar.MILLISECOND, 0);
    val += calend.getTimeInMillis();
    calend.setTimeInMillis(val);

    // display result
    DateFormat df = DateFormat.getDateTimeInstance();
    System.out.println(df.format(calend.getTime()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文