C/C++日期解决方案/转换

发布于 2024-08-06 01:04:31 字数 739 浏览 5 评论 0原文

我需要想出一种方法将日期解压缩为可读格式。不幸的是,我不完全理解所使用的原始流程/代码。

根据转发给我的信息,日期是使用自定义 C/Python 代码打包的,如下所示;

  date = year << 20;
  date |= month << 16;
  date |= day << 11;
  date |= hour << 6;
  date |= minute;

例如,最近的打包日期是 2107224749,相当于 2009 年 9 月 22 日星期二上午 10:45

我理解......或者至少我很确定......<<正在移动位,但我不确定“|”是什么完成。

另外,为了解压代码,注释如下:

year = (date & 0xfff00000) >> 20;
month = (date & 0x000f0000) >> 16;
day = (date & 0x0000f800) >> 11;
hour = (date & 0x000007c0) >> 6;
minute = (date & 0x0000003f);

最终,我需要做的是使用 JavaScript 或 ASP 执行解包并转换为可读格式,但我需要更好地理解上述过程才能开发解决方案。

任何帮助、提示、技巧、指示、想法等将不胜感激。

I need to come up with a way to unpack a date into a readable format. unfortunately I don't completely understand the original process/code that was used.

Per information that was forwarded to me the date was packed using custom C/Python code as follows;

  date = year << 20;
  date |= month << 16;
  date |= day << 11;
  date |= hour << 6;
  date |= minute;

For example, a recent packed date is 2107224749 which equates to Tuesday Sept. 22 2009 10:45am

I understand....or at least I am pretty sure....the << is shifting the bits but I am not sure what the "|" accomplishes.

Also, in order to unpack the code the notes read as follows;

year = (date & 0xfff00000) >> 20;
month = (date & 0x000f0000) >> 16;
day = (date & 0x0000f800) >> 11;
hour = (date & 0x000007c0) >> 6;
minute = (date & 0x0000003f);

Ultimately, what I need to do is perform the unpack and convert to readable format using either JavaScript or ASP but I need to better understand the process above in order to develop a solution.

Any help, hints, tips, pointers, ideas, etc. would be greatly appreciated.

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

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

发布评论

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

评论(5

最丧也最甜 2024-08-13 01:04:32

管道(|)是按位或,它是用于将位组合成单个值。

提取看起来很简单,但我建议先移动,然后屏蔽。这使得用于掩码的常数尽可能小,这更容易管理(并且可能更高效,尽管对于这种情况来说这并不重要)。

查看以二进制形式编写的掩码,可以了解每个字段使用了多少位:

  • 0xfff00000 设置了 12 位,因此 12 位用于年份
  • 0x000f0000 设置了 4 位,表示月份
  • 0x0000f800 设置了 5 位,表示日期
  • 0x000007c0 设置了 5 位,表示小时
  • 0x0000003f 设置了 6 位,表示分钟

The pipe (|) is bitwise or, it is used to combine the bits into a single value.

The extraction looks straight-forward, except I would recommend shifting first, and masking then. This keeps the constant used for the mask as small as possible, which is easier to manage (and can possibly be a tad more efficient, although for this case that hardly matters).

Looking at the masks used written in binary reveals how many bits are used for each field:

  • 0xfff00000 has 12 bits set, so 12 bits are used for the year
  • 0x000f0000 has 4 bits set, for the month
  • 0x0000f800 has 5 bits set, for the day
  • 0x000007c0 has 5 bits set, for the hour
  • 0x0000003f has 6 bits set, for the minute
望她远 2024-08-13 01:04:32

这个想法正是你所说的。表演“<<”只是将这些位向左移动。
什么是| (按位或)基本上是在数字中添加更多位,但不会覆盖已经存在的内容。

演示这一原则可能会有所帮助。

假设我们有一个字节(8 位),并且有两个数字,每个数字都是 4 位,我们希望将它们“放在一起”形成一个字节。假设这些数字是二进制的 1010 和 1011。所以我们希望最终得到字节:10101011。

现在,我们该怎么做呢?假设我们有一个字节 b,它初始化为 0。

如果我们取出要添加的第一个数字 1010,并将其移位 4 位,我们将得到数字 10100000(移位会在数字右侧添加字节) 。

如果我们这样做:b = (1010 << 4),b 的值为 10100000。

但是现在,我们想要添加 4 个位 (0011),而不触及前面的位。为此,我们可以使用|。这是因为 |运算符“忽略”我们数字中为零的任何内容。因此,当我们这样做时:

10100000 (b's current value)
|
00001011 (the number we want to add)
We get:
10101011 (the first four numbers are copied from the first number, 
          the other four numbers copied from the second number).

注意:这个答案有点长,我正在维基百科,所以,如果这里有人有更好的想法如何解释它,我将感谢您的帮助。

The idea is exactly what you said. Performing "<<" just shifts the bits to the left.
What the | (bitwise or) is accomplishing is basically adding more bits to the number, but without overwriting what was already there.

A demonstration of this principle might help.

Let's say we have a byte (8 bits), and we have two numbers that are each 4 bits, which we want to "put together" to make a byte. Assume the numbers are, in binary, 1010, and 1011. So we want to end up with the byte: 10101011.

Now, how do we do this? Assume we have a byte b, which is initialized to 0.

If we take the first number we want to add, 1010, and shift it by 4 bits, we get the number 10100000 (the shift adds bytes to the right of the number).

If we do: b = (1010 << 4), b will have the value 10100000.

But now, we want to add the 4 more bits (0011), without touching the previous bits. To do this, we can use |. This is because the | operator "ignores" anything in our number which is zero. So when we do:

10100000 (b's current value)
|
00001011 (the number we want to add)
We get:
10101011 (the first four numbers are copied from the first number, 
          the other four numbers copied from the second number).

Note: This answer came out a little long, I'm wikiing this, so, if anyone here has a better idea how to explain it, I'd appreciate your help.

吃→可爱长大的 2024-08-13 01:04:32

在解码部分&是按位的,0xfff00000 是十六进制位掩码。基本上位掩码中的每个字符代表数字的 4 位。 0 是二进制的 0000,f 是 1111,所以如果你看一下二进制的操作,你会得到 1111 1111 1111 0000 0000 ...无论是什么日期,所以基本上你得到了上三个半字节(半字节)并移动它们向下,这样 00A00000 就可以得到该年份的 10(十六进制 A)。

另请注意,|= 与 += 类似,它是按位或然后赋值为 1。

In the decode section & is bit wise and the 0xfff00000 is a hexadecimal bit mask. Basically each character in the bit mask represents 4 bits of the number. 0 being 0000 in binary and f being 1111 so if you look at the operation in binary you are anding 1111 1111 1111 0000 0000 ... with whatever is in date so basically you are getting the upper three nibbles(half bytes) and shifting them down so that 00A00000 gives you 10(A in hex) for the year.

Also note that |= is like += it is bit wise or then assignment rolled in to one.

假情假意假温柔 2024-08-13 01:04:32

只是添加一些实用技巧:

minute = value & ((1 << 6)-1);
hour = (value >> 6) & ((1<<5)-1);  // 5 == 11-6 == bits reserved for hour
...

1 << 5 在位置 5 创建一个位(即 32=00100000b),
(1<<5)-1 创建一个位掩码,其中设置了 5 个最低位(即 31 == 00011111b)

x & ((1<<5)-1) 执行按位“与”,仅保留最低五位中设置的位,提取原始小时值。

Just to add some practical tips:

minute = value & ((1 << 6)-1);
hour = (value >> 6) & ((1<<5)-1);  // 5 == 11-6 == bits reserved for hour
...

1 << 5 creates a bit at position 5 (i.e. 32=00100000b),
(1<<5)-1 cretaes a bit mask where the 5 lowest bits are set (i.e. 31 == 00011111b)

x & ((1<<5)-1) does a bitwise 'and' preserving only the bits set in the lowest five bits, extracting the original hour value.

看春风乍起 2024-08-13 01:04:32

是的<<移位位和 |是按位或运算符。

Yes the << shifts bits and the | is the bitwise OR operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文