gpt4 book ai didi

c - 删除转储中的字节或 c 中的 utf-8

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:45 26 4
gpt4 key购买 nike

我的消防站中有一个“C”程序,可以将传入的数据包捕获到工作站打印机。然后该程序会扫描数据包并发送声音警报,提示哪些设备将在调用中到期。该县最近开始使用UTF-8数据包,c程序无法处理数据流中所有多余的“00”。我需要忽略 00 或将程序设置为处理 UTF-8。我已经找了好几天了,但对于如何处理像我这样的新手可以处理的 utf-8,没有任何具体的内容。下面是程序的解释部分。

72 00 65 00 61 00 74 00 68 00 69 00 6e 00 67 00 稍后在数据包中

43 4f 44 45 53 45 54 3d 55 54 46 38 0a 40 50 4a 数据包开始

***void compressUtf16 (char *buff, size_t count) {
int i;
for (i = 0; i < count; i++)
buff[i] = buff[i*2]; // for xx 00 xx 00 xx 00 ...

*{ u_int i=0; 字符*搜索器= 0; 字符 c; 诠释j; int 本地标志; static int locationtripped = 0;

    static char currentline[256]; 
static int currentlinepos = 0;
static char lastdispatched[256];
static char dispatchstring[256];

char betastring[256];

static int a = 0;
static int e = 0;
static int pe = 0;
static int md = 0;

static int pulse = 0;

static char location[128];
static char type[16];
static char station[16];

static FILE *fp;
static int printoutscanning = 0;
static char printoutID[20];
static char printoutfileID[32];

static FILE *dbg;

if(pulse) {
if(pulse == 80) {
sprintf(betastring, "beta a a a");
printf("betastring: \"%s\"\n", betastring);
system(betastring);
pulse = 0;
} else
pulse++;
}

if(header->len > 96) {
for(i=55; (i < header->caplen + 1 ) ; i++) {
c = pkt_data[i-1];

if(c == 13 || c == 10) {
currentline[currentlinepos] = 0;
currentlinepos = 0;
j = strlen(currentline);
if(j && (j > 1)) {
if(strlen(printoutfileID) && printoutscanning) {
dbg = fopen(printoutfileID, "a");
fprintf(dbg, "%s\n", currentline);
fclose(dbg);
}

if(!printoutscanning) {
searcher = 0;
searcher = strstr(currentline, "INCIDENT HISTORY DETAIL:");
if(searcher) {
searcher = searcher + 26;
strncpy(printoutID, searcher, 9);
printoutID[9] = 0;
printoutscanning = 1;
a = 0;
e = 0;
pe = 0;
md = 0;
for(j = 0; j < 128; j++)
location[j] = 0;
for(j = 0; j < 16; j++) {
type[j] = 0;
station[j] = 0;
}
sprintf(printoutfileID, "calls/%s %.6d.txt", printoutID, header-> ts.tv_usec);
dbg = fopen(printoutfileID, "a");
fprintf(dbg, "%s\n", currentline);
fclose(dbg);
}

最佳答案

UTF-8,除了零代码点本身,不会有任何零字节。所有多字节编码(非 ASCII 代码点)的第一个字节始终以 11 位模式开头,后续字节始终以 10 位模式开头。

从下表中可以看出,U+0000 是 UTF-8 中唯一可以为您提供零字节的代码点。

+----------------+----------+----------+----------+----------+
| Unicode | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
+----------------+----------+----------+----------+----------+
| U+0000-007F | 0xxxxxxx | | | |
| U+0080-07FF | 110yyyxx | 10xxxxxx | | |
| U+0800-FFFF | 1110yyyy | 10yyyyxx | 10xxxxxx | |
| U+10000-10FFFF | 11110zzz | 10zzyyyy | 10yyyyxx | 10xxxxxx |
+----------------+----------+----------+----------+----------+

UTF-16 将在您的其他 ASCII 字节之间散布零字节,但这是一个简单的问题,即每隔一个字节就丢弃一次。是 0, 2, 4, ... 还是 1, 3, 5, ... 取决于您的 UTF-16 编码是 big-endian 还是 little-字节序。


我从您的示例中看到您的数据流确实指示 UTF-8(43 4f 44 45 53 45 54 3d 55 54 46 38 转换为文本 CODESET=UTF8) 但我向你保证它是在撒谎 :-)

72 00 65 00 61 00 74 00 68 00 69 00 6e 00 67 00reathing 的 UTF-16,大概是一个词段,因为我不熟悉那个词(无论如何是英文的)。

我建议您向生成该数据的人澄清,因为它显然是错误的。至于您如何处理 UTF-16,我已经在上面介绍过了。如果其中包含 ASCII 数据(备用字节始终为零),您可以使用类似以下内容的方式丢弃这些备用字节:

// Process a UTF16 buffer containing ASCII-only characters.
// buff is the buffer, count is the quantity of UTF-16 chars.
// Will change buffer.

void compressUtf16 (char *buff, size_t count) {
int i;
for (i = 0; i < count; i++)
buff[i] = buff[i*2]; // for xx 00 xx 00 xx 00 ...
}

而且,如果您使用的是 other endian UTF-16,只需更改:

buff[i] = buff[i*2];     // for xx 00 xx 00 xx 00 ...

进入:

buff[i] = buff[i*2+1];   // for 00 xx 00 xx 00 xx ...

关于c - 删除转储中的字节或 c 中的 utf-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6658936/

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