gpt4 book ai didi

c - 无符号字符比较

转载 作者:行者123 更新时间:2023-11-30 18:07:38 25 4
gpt4 key购买 nike

我对所附代码有疑问。我希望用偏移量(它们之间的字节数)替换一些分隔符。我只有 1 个字节,如果偏移量大于 254,我想用 255 替换它。在这种情况下,不应替换下一个找到的偏移量。替换部分工作正常...无论如何,我无法识别最后一个分隔符是否上次被替换为 255,它被跳过或转换,我不知道为什么/我做错了什么。也许任何人都可以给我任何线索。这里是代码,最后是输出。我已经标记(用 **)分隔符,不应替换它。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "zlib.h"


unsigned char *process(unsigned char *data, unsigned long size )
{
int i;
unsigned char *lastdel = data+1;
for (i=2;i<size; i++)
{
// if((unsigned char)*lastdel >254 )printf(" %c <#######-------\n", *lastdel);
//if(*lastdel == (unsigned char) 255 )printf(" %c <#######-------\n", *lastdel);
//if (!memcmp(lastdel,(unsigned char) 255, 1))
//printf(" %c <#######-------\n", *lastdel);
//if(*lastdel == (unsigned char) 255 )printf(" %c <#######-------\n", *lastdel);
//if(data[i] !='\x03' && data[i] !='\x1D' && data[i] !='\x1E'&& data[i] !='\x1C')continue;
//if(*lastdel >(unsigned char) 254 )printf(" %c <#######-------\n", *lastdel);
if(*lastdel >(unsigned char) 254 )printf(" %c <++++~~~++++++++#######-------\n", *lastdel);
if(data[i] =='\x03')
{
*lastdel = (&data[i] - lastdel-1 >255) ?(unsigned char) 255 : &data[i] - lastdel-1;
i+=2;
lastdel = &data[i];
continue;
}
if(data[i] =='\x1D')
{
*lastdel = (&data[i] - lastdel-1 >255) ?(unsigned char) 255 : &data[i] - lastdel-1;
i++;
lastdel = &data[i];
continue;
}
if(data[i] =='\x1E'|| data[i] =='\x1C')
{
if(*lastdel >(unsigned char) 254 )printf(" %c <++++~~~++++++++#######-------\n", *lastdel);
*lastdel = (&data[i] - lastdel-1 >255) ? (unsigned char) 255 : &data[i] - lastdel-1;
lastdel = &data[i];
if(*lastdel >(unsigned char) 254 )printf(" %c <++++~~~++++++++#######-------\n", *lastdel);
continue;
}
}
}
int main(void)
{
//readFile("/root/20101202.174715.std");
unsigned long s=290;
printf("hex: %x ", s<<24);
printf(" %x ", s<<16);
printf(" %x ", s<<8);
printf(" %x \n", s);
//exit(1);
int length ;
//unsigned char *bytes =readFile("/root/20101202.174715.std");
unsigned char bytes[] =
"\x31\x02\x00\x00\x67\x10\x00\x00\x00\x53\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x44\x45\x30\x30\x30\x4C\x53\x38\x30\x35\x38\x36\x2E\x4C\x55\x53\x1E\x35\x1C\x38\x2E\x1D\x1E\x32\x38\x1C\x30\x2E\x36\x32\x30\x30\x1E\x33\x30\x1C\x30\x2E\x35\x39\x30\x30\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x03\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x44\x45\x30\x30\x30\x53\x46\x44\x30\x5A\x32\x36\x2E\x4C\x55\x53\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x1E\x35\x1C\x38\x2E\x1D\x1E\x32\x38\x1C\x31\x30\x2E\x30\x35\x38\x30\x1E\x33\x30\x1C\x31\x30\x2E\x30\x32\x38\x30\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x03\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x45\x55\x52\x4E\x4F\x4B\x2E\x46\x58\x56\x57\x44\x1E\x35\x1C\x31\x30\x2E\x1D\x1E\x33\x30\x1C\x37\x2E\x39\x35\x37\x31\x1E\x32\x38\x1C\x37\x2E\x39\x36\x32\x36\x1E\x38\x30\x1C\x37\x2E\x39\x35\x39\x38\x35\x1E\x37\x35\x32\x1C\x5A\x4B\x42\x5A\x1E\x32\x38\x31\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x1E\x36\x34\x1C\x2D\x30\x2E\x30\x33\x39\x32\x1E\x31\x36\x34\x1C\x2D\x2E\x34\x38\x39\x34\x03\x45\x49\x00\x00\x00\x68\x10\x00\x00\x00\x53\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x44\x45\x30\x30\x30\x39\x37\x36\x39\x37\x36\x30\x2E\x4C\x55\x53\x1E\x35\x1C\x31\x2E\x1D\x1E\x32\x38\x1C\x31\x30\x30\x2E\x38\x39\x30\x30\x1E\x33\x30\x1C\x31\x30\x30\x2E\x30\x39\x30\x30\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x03\x45"
;
/* Speicher fuer String mit 4294967296 Zeichen reservieren */
unsigned char *data;
if (( data = ( unsigned char *) malloc(1000000000 * sizeof(unsigned char))) == NULL)
{
printf( "Nicht genug Speicher, um den Puffer zu allokieren\n");
exit(1);
}
printf( "Speicher fuer String wurde reserviert!\n" );
int strsize =((sizeof(bytes)/sizeof(*bytes))-1);
int pos =0;
int loop=1;
while(bytes[pos+9]=='S')
{
printf("----------------------------\n");
printf("| LOOP %d, POSITION %d |\n", loop, pos);
printf("----------------------------\n\n");
printf("hex: %x ", bytes[pos+0]<<24);
printf(" %x ", bytes[pos+1]<<16);
printf(" %x ", bytes[pos+2]<<8);
printf(" %x \n", bytes[pos+3]);
unsigned long size= ntohl(bytes[pos+0]<<24| bytes[pos+1]<<16| bytes[pos+2]<<8| bytes[pos+3]);
printf("=> RECOGNIZED SIZE: %lu \n", size );
printf("\n");
int seq = ntohl(bytes[pos+4]<<24| bytes[pos+5]<<16| bytes[pos+6]<<8| bytes[pos+7]);
printf("seq number: %d \n", seq );
printf("compressed: %d \n", bytes[pos+8]);
printf("start in hex: %x, in char: %c \n", bytes[pos+9], bytes[pos+9] );
//printf("HeaderFieldIdentifier: %c %c %c \n", bytes[pos+10], bytes[pos+11], bytes[pos+12]);
printf("stop in hex: %x, in char: %c \n\n", bytes[pos+size+10], bytes[pos+size+10] );
/* Array kreieren das nur auf die komprimierten Daten zeigt */
unsigned char *ptr;
ptr = &bytes[pos+10]; // Position des 1. Bytes
bytes[pos+size+10]='\0'; // Ende des Strings festlegen
/* Jetzt entkomprimieren wir die Daten */
int ret=0;
unsigned long size_uncompress = size;//1000000000 * sizeof(unsigned char);
if( bytes[pos+8]=='\x01')
{
size_uncompress = 1000000000 * sizeof(unsigned char);
ret=uncompress(data, &size_uncompress, ptr, size);
data[size_uncompress]='\0';
}
else
data = ptr;
//data[size_uncompress]='\0';
printf("%d|%lu|%lu|%x|%x|%x|%x\n",ret,size,size_uncompress,ptr[0],ptr[1],bytes[pos+10],bytes[pos+11]);
printf("\n");
printf("----------------------------\n");
printf("| Message as String: |\n");
printf("----------------------------\n");
printf("%s\n", data);
int i =0;
printf("\n");
printf("----------------------------\n");
printf("| Message as hexadecimal |\n");
printf("----------------------------\n");
for (i=0;i<size_uncompress ;i++)
{
printf("%x ",data[i] );

}
printf("\n\n");
printf("----------------------------\n");
printf("| Edited message |\n");
printf("----------------------------\n");
process(data, size_uncompress);
for (i=0;i<size_uncompress ;i++)
{
printf("%x ",data[i] );
}
printf("\n");
pos+=(size+11);
loop++;
}
//free(data);
}

输出原始。留言

02 01 31 02 36 33 02 37 34 01 55 01 38 10 44 45 30 30 30 4c 53 38 30 35 38 36 2e 4c 55 53 01 35 02 38 2e 1d 02 32 38 06 30 2e 36 32 30 30 02 33 30 06 30 2e 35 39 30 30 02 32 36 08 31 35 3a 30 33 3a 30 34 03 02 01 31 02 36 33 02 37 34 01 55 01 38 ff 44 45 30 30 30 53 46 44 30 5a 32 36 2e 4c 55 53 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 **01** 35 02 38 2e 1d 02 32 38 07 31 30 2e 30 35 38 30 02 33 30 07 31 30 2e 30 32 38 30 02 32 36 08 31 35 3a 30 33 3a 30 34 03 02 01 31 02 36 33 02 37 34 01 55 01 38 c 45 55 52 4e 4f 4b 2e 46 58 56 57 44 01 35 03 31 30 2e 1d 02 33 30 06 37 2e 39 35 37 31 02 32 38 06 37 2e 39 36 32 36 02 38 30 07 37 2e 39 35 39 38 35 03 37 35 32 4 5a 4b 42 5a 03 32 38 31 8 31 35 3a 30 33 3a 30 34 02 32 36 8 31 35 3a 30 33 3a 30 34 02 36 34 07 2d 30 2e 30 33 39 32 03 31 36 34 06 2d 2e 34 38 39 34 3                                                                                                                                                                                     
Replaced message
02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 44 45 30 30 30 4C 53 38 30 35 38 36 2E 4C 55 53 1E 35 1C 38 2E 1D 1E 32 38 1C 30 2E 36 32 30 30 1E 33 30 1C 30 2E 35 39 30 30 1E 32 36 1C 31 35 3A 30 33 3A 30 34 03 02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 44 45 30 30 30 53 46 44 30 5A 32 36 2E 4C 55 53 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 **1E** 35 1C 38 2E 1D 1E 32 38 1C 31 30 2E 30 35 38 30 1E 33 30 1C 31 30 2E 30 32 38 30 1E 32 36 1C 31 35 3A 30 33 3A 30 34 03 02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 45 55 52 4E 4F 4B 2E 46 58 56 57 44 1E 35 1C 31 30 2E 1D 1E 33 30 1C 37 2E 39 35 37 31 1E 32 38 1C 37 2E 39 36 32 36 1E 38 30 1C 37 2E 39 35 39 38 35 1E 37 35 32 1C 5A 4B 42 5A 1E 32 38 31 1C 31 35 3A 30 33 3A 30 34 1E 32 36 1C 31 35 3A 30 33 3A 30 34 1E 36 34 1C 2D 30 2E 30 33 39 32 1E 31 36 34 1C 2D 2E 34 38 39 34 03 45 49 00 00 00 68 10 00 00 00 53 02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 44 45 30 30 30 39 37 36 39 37 36 30 2E 4C 55 53 1E 35 1C 31 2E 1D 1E 32 38 1C 31 30 30 2E 38 39 30 30 1E 33 30 1C 31 30 30 2E 30 39 30 30 1E 32 36 1C 31 35 3A 30 33 3A 30 34

最佳答案

首先检查尚未替换的前一个分隔符(*lastdel)是否为255,因为它尚未被替换,难怪这种情况永远不会发生。然后将*lastdel替换为255,替换后将lastdel指针的值更改为下一个位置(尚未更改),然后检查该位置是否已替换并包含255...

难怪这种情况从未发生。只要将测试放在正确的位置,它就应该可以正常工作。

关于c - 无符号字符比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4408637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com