简体   繁体   English

如何将16位值返回为64位?

[英]How to return a 16 bit value as 64 bit?

// Returns 64 bit mac timer count value
uint64_t get_timestamp()
{
    uint16_t cnt;
    cnt =   read_counter(); 
    printk ("counter value is 0x%x\n", cnt);
    return cnt;
}

within caller: 在呼叫者内:

uint64_t ts;        
ts =    get_timestamp();
printk ( "returned timestamp is 0x%x \n", ts );

I got the following on screen, What is wrong above? 屏幕上显示以下内容,上面有什么问题?

counter value is 0x000045a5 returned timestamp is 0x00000000 计数器值为0x000045a5返回的时间戳为0x00000000

counter value is 0x0000698f returned timestamp is 0x00000000 计数器值为0x0000698f返回的时间戳为0x00000000

You are seeing this issue becasuse you print out the values incorrectly. 您看到此问题是因为您错误地打印了这些值。 there are dedicated printf format macros for the uint*_t types. uint*_t类型有专用的printf格式宏。

from the manpage of <inttypes.h> : <inttypes.h>的联机帮助页中:

   The fprintf() macros for signed integers are:


          PRIdN        PRIdLEASTN   PRIdFASTN    PRIdMAX      PRIdPTR
          PRIiN        PRIiLEASTN   PRIiFASTN    PRIiMAX      PRIiPTR

   The fprintf() macros for unsigned integers are:


          PRIoN        PRIoLEASTN   PRIoFASTN    PRIoMAX      PRIoPTR
          PRIuN        PRIuLEASTN   PRIuFASTN    PRIuMAX      PRIuPTR
          PRIxN        PRIxLEASTN   PRIxFASTN    PRIxMAX      PRIxPTR
          PRIXN        PRIXLEASTN   PRIXFASTN    PRIXMAX      PRIXPTR

so for example, to print a uint16_t and a uint64_t you could write (untested): 因此,例如,要打印uint16_tuint64_t您可以编写(未经测试):

int main (void)
{
  uint16_t a = 13;
  uint64_t b = 37;

  printf("uint16_t: %" PRIx16 ", uint64_t: %" PRIx64 "\n", a, b);

  return 0;
}

you should read man stdint.h and man inttypes.h if you are interested in the details, and what the semantics of the LEAST and FAST types are. 如果您对详细信息以及LEASTFAST类型的语义感兴趣,则应该阅读man stdint.hman inttypes.h very cool stuff. 很酷的东西。

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

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