简体   繁体   English

以十六进制表示的4字节日期时间转换为可读

[英]4 bytes datetime in hex convert to readable

I have a response from a server that was compressed and converted it to base64 string. 我收到来自服务器的响应,该响应已被压缩并将其转换为base64字符串。 I've decoded this response and found it to be something that looks like a soap document. 我已经解码了此响应,发现它看起来像是肥皂文档。 Most values are in hex format. 大多数值都是十六进制格式。 So I have a parameter datetime which stores value "\\xf4\\xdd|\\xad\\x08". 因此,我有一个参数datetime,它存储值“ \\ xf4 \\ xdd | \\ xad \\ x08”。 I know that it is a representation of the 24.06.1982 date. 我知道这是1982年6月24日的代表。

How I could convert hex value to the date format? 如何将十六进制值转换为日期格式? Prefer c/c++ language. 首选c / c ++语言。

To convert the encoded number to an integer, you need to decide if it's big-endian or little-endian. 要将编码的数字转换为整数,您需要确定它是big-endian还是little-endian。

Since the data you have is not a string, ie the number is not in text but directly in binary, you cannot sanely use sscanf() which is a string-parsing function. 由于您拥有的数据不是字符串,即数字不是文本形式,而是直接以二进制形式,因此您不能明智地使用sscanf()这是一个字符串解析函数。

Instead, so something like this: 而是这样的:

const char *buffer = "\xf4\xdd|\xad\x08";
unsigned int stamp = (((unsigned char) buffer[0]) << 24) |
                     (((unsigned char) buffer[1]) << 16) |
                     (((unsigned char) buffer[3]) << 8) |
                     ((unsigned char) buffer[4]);

If it's little-endian, reverse the indexing sequence (so that buffer[0] becomes buffer[4] and so on). 如果是little-endian,则反转索引顺序(以便buffer[0]成为buffer[4] ,依此类推)。

to convert that hex format to a number, you can use scanf() 要将十六进制格式转换为数字,可以使用scanf()

int a,b,c,d;
sscanf(input,"\\x%x\\x%x|\\x%x\\x%x",&a,&b,&c,&d);
long result=a<<24 | b<<16 | c<<8 | d;

That will give you 4108168456 for the value, you provided. 您所提供的价值将为您提供4108168456。

What further information about the format do you have? 关于格式,您还有什么进一步的信息?

int a,b,c,d;
sscanf(input,"\\x%x\\x%x|\\x%x\\x%x",&a,&b,&c,&d);
long field1=a<<8 | b;
long field2=c<<8 | d;

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

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