简体   繁体   English

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

[英]How to Convert 64bit Long Data Type to 16bit Data Type

I would like to know how to convert 64 bit long Data Type to any of the 16 bit Data Types. 我想知道如何将64 bit long数据类型转换为16 bit数据类型中的任何一种。 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 . 只有2个字节16位 )可用于包含时间戳 But we are getting 64 bit long as the Time Stamp value from Win API . 但是, Win API的Time Stamp值的64 bit long64 bit long So a conversion from 64 bit data type to to 16 bit data type is essential. 因此,从64位数据类型到16位数据类型的转换至关重要。

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. 因此,如何量化或截断时间戳取决于您。 Eg 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. 在这种情况下,您将64位数字除以1000000000,然后剩下秒数。 Then it might fit into 16 bits or not (16 bits would only store up to 65535 seconds). 然后,它可能适合或不适合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. 然后找出您正在使用的Windows时间函数返回什么。 Then convert the Windows time unit into the-library-that-you-use time unit. 然后将Windows时间单位转换为您使用的库时间单位。

16 bits may or may not be enough, depending on what you need the timestamp for. 16位可能足够,也可能不足,这取决于您需要的时间戳。 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. 但是一些可能可行的示例是:超时,测量数据包的往返时间,总体测量时间间隔(对于向用户显示时间信息可能正常工作)等等。

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. 根据流中典型的数据包数量,您甚至可以减少一些位并将其用于其他目的,因为序列计数器可以更轻松地处理换行。

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秒可表示1秒到65536秒或约1000分钟的值。 1 millisecond per bit lets you go from 0.001 seconds up to 65.5 seconds 每位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);
}

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". 您的时间戳不必说“此数据包是5月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. 16位可为您提供65536个值。 Do you want those to represent seconds? 您想要那些代表秒吗? Then your timestamps will wrap around every 18 hours. 然后,您的时间戳将每18个小时结束一次。

Or they can be minutes. 也可以是几分钟。 Then it'll be 45 days before they wrap around. 然后需要45天的时间才能解决。 Or days, or microseconds, it all depends on what you need. 或几天或几微秒,都取决于您的需求。

But the only way you can convert a 64-bit value into a 16-bit one is to remove 48 bits of data. 但是,将64位值转换为16位值的唯一方法是删除48位数据。 You choose which ones 您选择哪个

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

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