简体   繁体   English

如何实时组合十六进制值?

[英]How to combine hex values in real-time?

To give a context, I have an incoming stream of Hex values that is getting written to a CSV file which are in the format shown below.为了给出上下文,我有一个传入的 stream 十六进制值,它被写入一个 CSV 文件,格式如下所示。

20 5a 20 5e 20 7b 20 b1 20 64 20 f8 ...

I can not change the way the data is flowing in, but before it gets written to a CSV file I want it in this format below.我无法更改数据流入的方式,但在将其写入 CSV 文件之前,我希望它采用以下格式。

205a 205e 207b 20b1 2064 20f8 ...

As the data is coming, I need to process it and store it in the format shown above.随着数据的到来,我需要对其进行处理并以上面显示的格式存储。 One of the ways I tried was just bitshifting and doing logical OR which would store the result in a variable.我尝试过的方法之一就是移位并进行逻辑或运算,将结果存储在一个变量中。 But all I have here is a pointer pointing to a buffer where the data will be flowing into.但我这里只有一个指针,指向数据将流入的缓冲区。 I have something like this.我有这样的东西。

uint8_t *curr_ptr;
uint8_t* dec_buffer=(uint8_t*)calloc(4000,sizeof(uint8_t)*max_len);
init=dec_buffer;
curr_ptr=init+((count)*max_len);

for(int j=17;j<=145;j+=1){

        fprintf(f_write[file_count],"%02x ", *(curr_ptr+j));

if(j>0 && j%145==0){

       fprintf(f_write[file_count],"\n");

Effectively you want to remove every other space.实际上你想删除所有其他空间。 Why not something like this?为什么不这样呢?

for(int j=17;j<=145;j+=1){
    fprintf(f_write[file_count], j%2 ? "%02x " : "%02x", *(curr_ptr+j));

Not sure if you should be printing spaces after the odd values of j or the even ones, but you can sort that out.不确定你是否应该在j的奇数或偶数之后打印空格,但你可以解决这个问题。

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

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