#include <stdio.h>
#include <stdlib.h>
unsigned char* readFile(const char* filename, size_t* size);
void binaryToHex(const unsigned char* data, size_t size, char* output);
int main() {
const char* filename = "file:///H:/BaiduNetdiskDownload/ceshi.mp3";
size_t size;
unsigned char* data = readFile(filename, &size);
if (!data) {
return 1;
}
char* hexString = (char*)malloc(size * 2 + 1);
if (!hexString) {
perror("Failed to allocate memory for hex string");
free(data);
return 1;
}
binaryToHex(data, size, hexString);
printf("Hexadecimal representation:\n%s\n", hexString);
free(data);
free(hexString);
return 0;
}
unsigned char* readFile(const char* filename, size_t* size) {
FILE* file = fopen(filename, "rb");
if (!file) {
perror("Failed to open file");
return NULL;
}
fseek(file, 0, SEEK_END);
*size = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char* buffer = (unsigned char*)malloc(*size);
if (!buffer) {
perror("Failed to allocate memory");
fclose(file);
return NULL;
}
fread(buffer, 1, *size, file);
fclose(file);
return buffer;
}
void binaryToHex(const unsigned char* data, size_t size, char* output) {
for (size_t i = 0; i < size; ++i) {
sprintf(output + i * 2, "%02X", data[i]);
}
output[size * 2] = '\0';
}