如何将64位长数据类型转换为16位数据类型

时间:2022-06-25 19:19:11

I would like to know how to convert 64 bit long Data Type to any of the 16 bit Data Types. This feature is required in the Ethernet Application to include the Time Stamp. Only 2 Bytes ( 16 bits ) are available to include the Time Stamp. But we are getting 64 bit long as the Time Stamp value from Win API. So a conversion from 64 bit data type to to 16 bit data type is essential.

我想知道如何将64位长数据类型转换为任何16位数据类型。以太网应用程序中需要此功能才能包含时间戳。只有2个字节(16位)可用于包含时间戳。但是我们从Win API获得64位长的时间戳值。因此,从64位数据类型到16位数据类型的转换是必不可少的。

4 个解决方案

#1


Well, you can't fit 64 bits of information into 16 bits of storage without losing some of the information.

好吧,你不能将64位信息放入16位存储而不会丢失一些信息。

So it's up to you how to quantize or truncate the timestamp. E.g. suppose you get the timestamp in nanosecond precision, but you only need to store it at seconds precision. In that case you divide the 64 bit number by 1000000000 and are left with the seconds. Then it might fit into 16 bits or not (16 bits would only store up to 65535 seconds).

因此,您可以自行决定如何量化或截断时间戳。例如。假设您获得的时间戳精确到纳秒级,但您只需要以秒精度存储它。在这种情况下,您将64位数除以1000000000,并留下秒。然后它可能适合16位或不适合16位(16位只能存储65535秒)。

If it won't fit, then you'll have the timestamp wrapping around periodically. Which, again, might be a problem in your case or it might be not a problem.

如果它不适合,那么你将定期包裹时间戳。在您的情况下,这可能是一个问题,也可能不是问题。

In any case, if you need to interface an existing library that requires timestamps - figure out what it needs in that timestamp (clock ticks? seconds? years?). Then figure out what the Windows times function that you're using returns. Then convert the Windows time unit into the-library-that-you-use time unit.

在任何情况下,如果您需要连接需要时间戳的现有库 - 找出它在该时间戳中需要什么(时钟滴答?秒?年?)。然后找出你正在使用的Windows时间函数返回的内容。然后将Windows时间单位转换为您使用的库时间单位。

#2


16 bits may or may not be enough, depending on what you need the timestamp for. For most purposes it's way too small or at least inconvenient. But some examples where this might work could be: timeouts, measuring round-trip time for packets, grossly measuring time intervals (which might work alright for displaying time information to users) and so on.

16位可能是也可能不够,具体取决于您需要的时间戳。在大多数情况下,它太小或至少不方便。但是这可能有用的一些例子可能是:超时,测量数据包的往返时间,大量测量时间间隔(这可能对于向用户显示时间信息很有效)等等。

On the other hand, it's probably useless for reordering packets. If this is the case, I'd suggest you replaced the timestamp with a sequence counter. Depending on the typical number of packets in the stream, you might even be able to cut down a few bits and use them for other purposes, since sequence counters can handle wrapping more easily.

另一方面,重新排序数据包可能毫无用处。如果是这种情况,我建议你用序列计数器替换时间戳。根据流中典型的数据包数量,您甚至可以减少几个比特并将其用于其他目的,因为序列计数器可以更轻松地处理包装。

#3


As the others said, the first problem is deciding on the correct scaling. You've got to balance your resolution with you desired maximum range. One way to think about it is deciding how many seconds per bit you want. With 1 second per bit you can express values from 1 second up to 65536 seconds or ~1000 minutes. 1 millisecond per bit lets you go from 0.001 seconds up to 65.5 seconds

正如其他人所说,第一个问题是决定正确的缩放。你必须平衡你的分辨率和你想要的最大范围。考虑它的一种方法是决定你想要的每位数秒。每位1秒,您可以表示从1秒到65536秒或~1000分钟的值。每位1毫秒可让您从0.001秒到65.5秒

Here's one way to do the conversion.

这是进行转换的一种方法。

#define seconds_per_bit   .0001  <--YOUR VALUE HERE.
#define bits_per_second   (1/seconds_per_bit);  
int16 timestamp()
{
  Int64 counts_per_second,counts;

  QueryPerformanceFrequency(&counts_per_sec);
  QueryPerformanceCounter(&counts);  
  return (UInt16)(counts * bits_per_second / counts_per_second);
}

#4


It depends entirely on what you use the timestamp for. You mention ethernet, so one obvious use I could imagine is for ordering packets. And in that case all you really need is a counter. Instead of your timestamp saying "this packet was sent on may 14th at 14:35PM", it can simply say "this is the 4023th packet".

它完全取决于您使用时间戳的内容。你提到以太网,所以我能想象的一个明显的用途是订购数据包。在这种情况下,你真正需要的只是一个柜台。而不是你的时间戳说“这个数据包是在14日下午14:35发送的”,它可以简单地说“这是第4023个数据包”。

If you need it to record actual clock time, you just have to pick which parts of it is relevant. 16 bits give you 65536 values to play with. Do you want those to represent seconds? Then your timestamps will wrap around every 18 hours.

如果您需要它来记录实际的时钟时间,您只需选择它的哪些部分是相关的。 16位为您提供65536个值。你想要那些代表秒吗?然后你的时间戳将每18个小时回绕一次。

Or they can be minutes. Then it'll be 45 days before they wrap around. Or days, or microseconds, it all depends on what you need.

或者他们可以是几分钟。然后它们会在它们环绕之前45天。或者天或微秒,这一切都取决于你需要什么。

But the only way you can convert a 64-bit value into a 16-bit one is to remove 48 bits of data. You choose which ones

但是,将64位值转换为16位值的唯一方法是删除48位数据。你选择哪一个

#1


Well, you can't fit 64 bits of information into 16 bits of storage without losing some of the information.

好吧,你不能将64位信息放入16位存储而不会丢失一些信息。

So it's up to you how to quantize or truncate the timestamp. E.g. suppose you get the timestamp in nanosecond precision, but you only need to store it at seconds precision. In that case you divide the 64 bit number by 1000000000 and are left with the seconds. Then it might fit into 16 bits or not (16 bits would only store up to 65535 seconds).

因此,您可以自行决定如何量化或截断时间戳。例如。假设您获得的时间戳精确到纳秒级,但您只需要以秒精度存储它。在这种情况下,您将64位数除以1000000000,并留下秒。然后它可能适合16位或不适合16位(16位只能存储65535秒)。

If it won't fit, then you'll have the timestamp wrapping around periodically. Which, again, might be a problem in your case or it might be not a problem.

如果它不适合,那么你将定期包裹时间戳。在您的情况下,这可能是一个问题,也可能不是问题。

In any case, if you need to interface an existing library that requires timestamps - figure out what it needs in that timestamp (clock ticks? seconds? years?). Then figure out what the Windows times function that you're using returns. Then convert the Windows time unit into the-library-that-you-use time unit.

在任何情况下,如果您需要连接需要时间戳的现有库 - 找出它在该时间戳中需要什么(时钟滴答?秒?年?)。然后找出你正在使用的Windows时间函数返回的内容。然后将Windows时间单位转换为您使用的库时间单位。

#2


16 bits may or may not be enough, depending on what you need the timestamp for. For most purposes it's way too small or at least inconvenient. But some examples where this might work could be: timeouts, measuring round-trip time for packets, grossly measuring time intervals (which might work alright for displaying time information to users) and so on.

16位可能是也可能不够,具体取决于您需要的时间戳。在大多数情况下,它太小或至少不方便。但是这可能有用的一些例子可能是:超时,测量数据包的往返时间,大量测量时间间隔(这可能对于向用户显示时间信息很有效)等等。

On the other hand, it's probably useless for reordering packets. If this is the case, I'd suggest you replaced the timestamp with a sequence counter. Depending on the typical number of packets in the stream, you might even be able to cut down a few bits and use them for other purposes, since sequence counters can handle wrapping more easily.

另一方面,重新排序数据包可能毫无用处。如果是这种情况,我建议你用序列计数器替换时间戳。根据流中典型的数据包数量,您甚至可以减少几个比特并将其用于其他目的,因为序列计数器可以更轻松地处理包装。

#3


As the others said, the first problem is deciding on the correct scaling. You've got to balance your resolution with you desired maximum range. One way to think about it is deciding how many seconds per bit you want. With 1 second per bit you can express values from 1 second up to 65536 seconds or ~1000 minutes. 1 millisecond per bit lets you go from 0.001 seconds up to 65.5 seconds

正如其他人所说,第一个问题是决定正确的缩放。你必须平衡你的分辨率和你想要的最大范围。考虑它的一种方法是决定你想要的每位数秒。每位1秒,您可以表示从1秒到65536秒或~1000分钟的值。每位1毫秒可让您从0.001秒到65.5秒

Here's one way to do the conversion.

这是进行转换的一种方法。

#define seconds_per_bit   .0001  <--YOUR VALUE HERE.
#define bits_per_second   (1/seconds_per_bit);  
int16 timestamp()
{
  Int64 counts_per_second,counts;

  QueryPerformanceFrequency(&counts_per_sec);
  QueryPerformanceCounter(&counts);  
  return (UInt16)(counts * bits_per_second / counts_per_second);
}

#4


It depends entirely on what you use the timestamp for. You mention ethernet, so one obvious use I could imagine is for ordering packets. And in that case all you really need is a counter. Instead of your timestamp saying "this packet was sent on may 14th at 14:35PM", it can simply say "this is the 4023th packet".

它完全取决于您使用时间戳的内容。你提到以太网,所以我能想象的一个明显的用途是订购数据包。在这种情况下,你真正需要的只是一个柜台。而不是你的时间戳说“这个数据包是在14日下午14:35发送的”,它可以简单地说“这是第4023个数据包”。

If you need it to record actual clock time, you just have to pick which parts of it is relevant. 16 bits give you 65536 values to play with. Do you want those to represent seconds? Then your timestamps will wrap around every 18 hours.

如果您需要它来记录实际的时钟时间,您只需选择它的哪些部分是相关的。 16位为您提供65536个值。你想要那些代表秒吗?然后你的时间戳将每18个小时回绕一次。

Or they can be minutes. Then it'll be 45 days before they wrap around. Or days, or microseconds, it all depends on what you need.

或者他们可以是几分钟。然后它们会在它们环绕之前45天。或者天或微秒,这一切都取决于你需要什么。

But the only way you can convert a 64-bit value into a 16-bit one is to remove 48 bits of data. You choose which ones

但是,将64位值转换为16位值的唯一方法是删除48位数据。你选择哪一个