gpt4 book ai didi

c++ - 我的字节数组到十六进制字符串转换器有什么问题,或者为什么它在写入文件时在特定位置后剪切符号?

转载 作者:搜寻专家 更新时间:2023-10-31 01:14:50 26 4
gpt4 key购买 nike

这是我的:

std::string GetBytesAsHEX(const char *arr, int arr_size)
{
BYTE ch = 0x00;
char pseudo[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

std::string ret_val;

for (int i = 0; i < arr_size; i++)
{
ch = (BYTE) (arr[i] & 0xF0);
ch = (BYTE) (ch >> 4);
ch = (BYTE) (ch & 0x0F);
ret_val += pseudo[(int)ch];
ch = (BYTE) (arr[i] & 0x0F);
ret_val += pseudo[(int)ch];
ret_val += ' ';
}

return ret_val;
}

int __stdcall Hooked_send(SOCKET s, const char *buf, int len, int flags)
{
h_send.PreHook();
//--------------

int ret_val = send(s, buf, len, flags);

if (LogPackets)
{
FILE *fptr = fopen("packet_log_hex.txt", "a");
char header[128] = { 0 };
sprintf(header, "\nSENT %i bytes: ", ret_val);
fwrite(header, strlen(header), sizeof(char), fptr);
fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

fclose(fptr);

fptr = fopen("packet_log.txt", "ab");
fwrite(buf, ret_val, sizeof(char), fptr);
fclose(fptr);
}

//---------------
h_send.PostHook();

return ret_val;
}

int __stdcall Hooked_recv(SOCKET s, char *buf, int len, int flags)
{
h_recv.PreHook(); //restore original recv address

int ret_val = recv(s, buf, len, flags);

if (ret_val > 0 && LogPackets)
{
FILE *fptr = fopen("packet_log_hex.txt", "a");
char header[128] = { 0 };
sprintf(header, "\nRECV %i bytes: ", ret_val);
fwrite(header, strlen(header), sizeof(char), fptr);
fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

fclose(fptr);

fptr = fopen("packet_log.txt", "ab");
fwrite(buf, ret_val, sizeof(char), fptr);
fclose(fptr);
}

h_recv.PostHook(); //replace recv address with Hooked_recv

return ret_val;
}

这就是我在文件 packet_log_hex.txt 中得到的内容

SENT 16 bytes: 55 47 0C 00 00 0 //this is way not 16 bytes... and why so weird termination?
RECV 32 bytes: 55 47 1C 00 00 00 10 00 03 00 00
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 00 0
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 0B 0
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 10 F
RECV 16 bytes: 55 47 0C 00 00 0
SENT 16 bytes: 55 47 0C 00 C5 E

packet_log.txt(这个包含纯字节)(从十六进制编辑器复制)

55 47 0C 00 00 00 00 00 02 00 00 00 01 00 03 02
55 47 1C 00 00 00 10 00 03 00 00 00 2D 04 00 00 50 07 F3 17 1A 37 34 48 81 D2 5E 13 73 21 37 A3
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 30 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 0B 00 00 00 12 00 00 00 40 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 10 FB 00 00 12 00 00 00 50 00 00 00
55 47 0C 00 00 00 00 00 12 00 00 00 00 00 00 00
55 47 0C 00 C5 EE 00 00 12 00 00 00 60 00 00 00

呃,你看到了。

最佳答案

您在以下行中使用的 ret_val:

fwrite(GetBytesAsHEX(buf, ret_val).c_str(), ret_val, sizeof(char), fptr);

是您从 recv() 调用中收到的字节数。这不是在将接收到的字节转换为以空格分隔的十六进制字符串后需要写入文件的字节数。你的电话应该是:

std::string hex_str = GetBytesAsHEX(buf, ret_val);
fwrite(hex_str.c_str(), hex_str.length(), sizeof(char), fptr);

至于奇怪的终止,这是由同一件事引起的。你可以看到你在第一行中正好写了 16 个字节,通过简单地计算它们,但是它是转换后的字符串的 16 个字节:

          111111
0123456789012345

55 47 0C 00 00 0

关于c++ - 我的字节数组到十六进制字符串转换器有什么问题,或者为什么它在写入文件时在特定位置后剪切符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10834424/

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