Hexadecimal -> file (binary)

本文介绍了一个简单的Java类,用于实现字节到十六进制字符串的转换以及从十六进制字符串到字节的转换。该类提供两个主要方法:`bytesToHex` 和 `hexToBytes`,分别用于进行这两种类型的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class HexCodec {
private static final char[] kDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
'b', 'c', 'd', 'e', 'f' };

public static char[] bytesToHex(byte[] raw) {
int length = raw.length;
char[] hex = new char[length * 2];
for (int i = 0; i < length; i++) {
int value = (raw[i] + 256) % 256;
int highIndex = value >> 4;
int lowIndex = value & 0x0f;
hex[i * 2 + 0] = kDigits[highIndex];
hex[i * 2 + 1] = kDigits[lowIndex];
}
return hex;
}

public static byte[] hexToBytes(char[] hex) {
int length = hex.length / 2;
byte[] raw = new byte[length];
for (int i = 0; i < length; i++) {
int high = Character.digit(hex[i * 2], 16);
int low = Character.digit(hex[i * 2 + 1], 16);
int value = (high << 4) | low;
if (value > 127)
value -= 256;
raw[i] = (byte) value;
}
return raw;
}

public static byte[] hexToBytes(String hex) {
return hexToBytes(hex.toCharArray());
}
}


在线转换工具: http://tomeko.net/online_tools/hex_to_file.php?lang=en
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include "aip_common.h" #include <string.h> #include <stdlib.h> #define ZERO (0) #define MAX_LINE_LENGTH (256) #define ADD_VALUE (0xF) #define FALSE (0) #define WRONG (0) #define SUCCESS (1) #define TRUE (1) #define STORESIZE (9) #define ENDOFSTRING (8) #define FOUR (4) #define ONE (1) #define SIXTEEN (16) /*===================================================================================================================================*/ /* Function Name: vd_s_initialize_array */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Initializes an array by setting all elements to ZERO. */ /* Arguments: U1* u1p_a_initialize_array : Pointer to the array to be initialized. */ /* size_t stp_a_initialize_size : Size of the array to initialize. */ /* Return: None */ /*===================================================================================================================================*/ static void vd_s_initialize_array(U1* u1p_a_initialize_array, size_t stp_a_initialize_size) { size_t stp_t_initialize_array_i; for (stp_t_initialize_array_i = (U4)ZERO; stp_t_initialize_array_i < stp_a_initialize_size; stp_t_initialize_array_i++) { u1p_a_initialize_array[stp_t_initialize_array_i] = (U1)ZERO; /* Fill each element with ZERO */ } } /*===================================================================================================================================*/ /* Function Name: vd_s_remove_trailing_newline */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Removes the trailing newline character (`\n`) in the given buffer, if present. */ /* Arguments: U1* u1p_a_remove_buffer : Pointer to the buffer to remove trailing newline character from. */ /* U4 u4_a_remove_length : Length of the buffer. */ /* Return: None */ /*===================================================================================================================================*/ static void vd_s_remove_trailing_newline(U1* u1p_a_remove_buffer, U4 u4_a_remove_length) { if ((u4_a_remove_length > (U4)ZERO) && ('\n' == u1p_a_remove_buffer[u4_a_remove_length - (U4)ONE])) { u1p_a_remove_buffer[u4_a_remove_length - (U4)ONE] = '\0'; /* Replace the trailing newline with a null character */ } } /*===================================================================================================================================*/ /* Function Name: stp_s_open_input_file */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Opens the input file "DA_510B_IMG_1.mhx" in binary mode for reading. */ /* Arguments: None */ /* Return: FILE* : Pointer to the opened input file. Returns NULL if opening fails. */ /*===================================================================================================================================*/ static FILE* stp_s_open_input_file(void) { FILE* stp_t_open_fp; stp_t_open_fp = fopen("DA_510B_IMG_1.mhx", "rb"); /* Open the input file in binary mode */ return stp_t_open_fp; } /*===================================================================================================================================*/ /* Function Name: stp_s_create_output_file */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Creates or opens the output file "WRITE_ADDRESS.TXT" in write mode for writing. */ /* Arguments: None */ /* Return: FILE* : Pointer to the opened output file. Returns NULL if creation fails. */ /*===================================================================================================================================*/ static FILE* stp_s_create_output_file(void) { FILE* stp_t_create_output_fp; stp_t_create_output_fp = fopen("WRITE_ADDRESS.TXT", "w"); /* Open the output file for writing */ return stp_t_create_output_fp; } /*===================================================================================================================================*/ /* Function Name: u4_s_process_s315_line */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Searches for a given prefix string in the line, extracts the hexadecimal address, */ /* modifies the address by adding a predefined value, and updates the output buffer. */ /* Arguments: U1* u1p_a_process_line : Pointer to the line being processed. */ /* U1* u1p_a_process_new_hex_addr : Pointer to the buffer to store processed hexadecimal address. */ /* U1* u1p_a_search_str : Pointer to the prefix string to search for. */ /* Return: U4 : TRUE if prefix is found and processed successfully, otherwise FALSE. */ /*===================================================================================================================================*/ static U4 u4_s_process_s315_line(U1* u1p_a_process_line, U1* u1p_a_process_new_hex_addr, U1* u1p_a_search_str) { U4 u4_t_process_result; U1* u1p_t_process_search_pos; U1* u1p_t_process_addr_start; size_t stp_t_len_after; U1 u1_tp_store_string[(U4)STORESIZE]; U4 u4_t_address_value; U4 u4_t_new_address; /* Initialize result as FALSE */ u4_t_process_result = (U4)FALSE; /* Search for the user-defined prefix string in the line */ u1p_t_process_search_pos = strstr(u1p_a_process_line, u1p_a_search_str); /* Continue processing only if the prefix is found */ if (NULL != u1p_t_process_search_pos) { u1p_t_process_addr_start = u1p_t_process_search_pos + (U4)FOUR; /* Skip prefix and get address part */ stp_t_len_after = strlen(u1p_t_process_addr_start); /* Length after the prefix */ /* Ensure sufficient length for processing */ if (stp_t_len_after >= (U4)ENDOFSTRING) { /* Initialize storage buffer */ vd_s_initialize_array(u1_tp_store_string, STORESIZE); /* Extract the address string and copy it into the buffer */ strncpy(u1_tp_store_string, u1p_t_process_addr_start, (U4)ENDOFSTRING); u1_tp_store_string[(U4)ENDOFSTRING] = '\0'; /* Terminate the string with a null character */ /* Convert hexadecimal string to integer */ u4_t_address_value = strtoul(u1_tp_store_string, NULL, (U4)SIXTEEN); u4_t_new_address = u4_t_address_value + (U4)ADD_VALUE; /* Modify the address */ /* Format and store the new hexadecimal address */ snprintf(u1p_a_process_new_hex_addr, (U4)STORESIZE, "%08lX", u4_t_new_address); /* Update the result as TRUE */ u4_t_process_result = (U4)TRUE; } } return u4_t_process_result; } /*===================================================================================================================================*/ /* Function Name: write_address_pair */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Writes the processed hexadecimal address and additional default settings into the output file. */ /* Arguments: FILE* stp_a_write_address_fp : Pointer to the output file. */ /* U1* u1p_a_write_address_hex_addr : Pointer to the new hexadecimal address string. */ /* Return: None */ /*===================================================================================================================================*/ static void vd_s_address_pair(FILE* stp_a_write_address_fp, const U1* u1p_a_write_address_hex_addr) { if (NULL != stp_a_write_address_fp) { fprintf(stp_a_write_address_fp, "0x40000000,0x%s\n", u1p_a_write_address_hex_addr); fprintf(stp_a_write_address_fp, "0x43FFFFF0,0x43FFFFFF\n"); /* Default settings */ } } /*===================================================================================================================================*/ /* Function Name: write_default_address */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Writes predefined default address mappings into the output file. */ /* Arguments: FILE* stp_a_write_default_fp : Pointer to the output file. */ /* Return: None */ /*===================================================================================================================================*/ static void vd_s_write_default_address(FILE* stp_a_write_default_fp) { if (NULL != stp_a_write_default_fp) { fprintf(stp_a_write_default_fp, "0x40000000,0x00000000\n"); fprintf(stp_a_write_default_fp, "0x43FFFFF0,0x43FFFFFF\n"); } } /*===================================================================================================================================*/ /* Function Name: u4_s_main_processing_task */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Reads the input file, searches for the user-defined prefix in each line, */ /* extracts and modifies the hexadecimal addresses, and writes the results into the output file. */ /* Arguments: U1* u1p_a_search_string : Pointer to the search prefix string. */ /* Return: U4 : SUCCESS if processing completes successfully, otherwise WRONG. */ /*===================================================================================================================================*/ static U4 u4_s_main_processing_task(U1* u1p_a_search_string) { U4 u4_t_main_processing_result; FILE* stp_t_input_fp; FILE* stp_t_output_fp; U1 u1_tp_line_buffer[(U4)MAX_LINE_LENGTH]; U4 u4_t_target_found; U1 u1_tp_new_hex_addr[(U4)STORESIZE]; size_t stp_t_main_processing_len; U4 u4_t_main_processing_line; /* Initialize result as WRONG */ u4_t_main_processing_result = (U4)WRONG; stp_t_input_fp = stp_s_open_input_file(); /* Open the input file */ if (NULL != stp_t_input_fp) { stp_t_output_fp = stp_s_create_output_file(); /* Create the output file */ if (NULL != stp_t_output_fp) { u4_t_target_found = (U4)FALSE; /* Default: target not found */ vd_s_initialize_array(u1_tp_new_hex_addr, STORESIZE); /* Initialize buffer */ while (NULL != fgets(u1_tp_line_buffer, (U4)MAX_LINE_LENGTH, stp_t_input_fp)) { stp_t_main_processing_len = strlen(u1_tp_line_buffer); vd_s_remove_trailing_newline(u1_tp_line_buffer, stp_t_main_processing_len); /* Remove trailing newline */ u4_t_main_processing_line = u4_s_process_s315_line( u1_tp_line_buffer, u1_tp_new_hex_addr, u1p_a_search_string); if ((U4)TRUE == u4_t_main_processing_line) { vd_s_address_pair(stp_t_output_fp, u1_tp_new_hex_addr); u4_t_target_found = (U4)TRUE; /* Mark as found */ u4_t_main_processing_result = (U4)SUCCESS; break; } } if ((U4)FALSE == u4_t_target_found) { vd_s_write_default_address(stp_t_output_fp); /* Write default addresses */ printf("Line %s not found\n", u1p_a_search_string); } fclose(stp_t_output_fp); } fclose(stp_t_input_fp); } return u4_t_main_processing_result; } /*===================================================================================================================================*/ /* Function Name: main */ /* --------------------------------------------------------------------------------------------------------------------------------- */ /* Description: Entry point for the program. Prompts the user to input a search prefix, */ /* executes the processing task, and displays the results on the console. */ /* Allows the user to retry if the processing fails. */ /* Arguments: None */ /* Return: U4 : Returns SUCCESS if processing completes successfully; otherwise returns WRONG. */ /*===================================================================================================================================*/ U4 main(void) { U4 u4_t_return_code; /* Holds the return code indicating success or failure. */ U4 u4_t_test_status; /* Holds the status of the processing task. */ U1 u1_tp_search_string[(U4)MAX_LINE_LENGTH]; /* Buffer to store user input for the search prefix string. */ U1 u1_tp_choice[(U4)FOUR]; /* Buffer to store user's choice for retry. */ U4 u4_t_retry = (U4)TRUE; /* Flag indicating whether the user wants to retry or exit. */ u4_t_return_code = (U4)WRONG; /* Initialize the return code to WRONG (processing failure by default). */ while (u4_t_retry) /* Loop until the user decides not to retry. */ { printf("Enter the search prefix string: "); /* Prompt user to enter the search prefix string. */ if (NULL == fgets(u1_tp_search_string, (U4)MAX_LINE_LENGTH, stdin)) /* Read user input. */ { printf("Error: Failed to read input. Exiting.\n"); /* Display error message for invalid input. */ system("pause"); return (U4)WRONG; /* Exit with WRONG if input reading fails. */ } vd_s_remove_trailing_newline(u1_tp_search_string, strlen(u1_tp_search_string)); /* Remove trailing newline if present. */ /* Check if the user has entered an empty string. */ if ((U4)ZERO == strlen(u1_tp_search_string)) { printf("Error: Search prefix string cannot be empty.\n"); /* Display error message for empty input. */ continue; /* Restart loop and prompt user again. */ } printf("Processing with search string: %s\n", u1_tp_search_string); /* Display the current search string. */ u4_t_test_status = u4_s_main_processing_task(u1_tp_search_string); /* Execute processing task with user input. */ /* Check processing status. */ if ((U4)SUCCESS == u4_t_test_status) { printf("SUCCESS! The search string: %s was processed successfully.\n", u1_tp_search_string); /* Display success message. */ u4_t_return_code = (U4)ZERO; /* Update return code to SUCCESS. */ break; /* Exit loop as processing succeeded. */ } else { printf("FAILED: No matching line was found for the provided search string.\n"); /* Display failure message. */ /* Ask user whether to retry. */ printf("Would you like to try again? (y/n): "); /* Prompt user for retry decision. */ if (NULL == fgets(u1_tp_choice, sizeof(u1_tp_choice), stdin)) /* Read user's decision. */ { printf("Error: Failed to read input. Exiting.\n"); /* Display error message for invalid input. */ break; /* Exit loop if input reading fails. */ } vd_s_remove_trailing_newline(u1_tp_choice, strlen(u1_tp_choice)); /* Remove trailing newline if present. */ /* Check user's decision. */ if (u1_tp_choice[(U4)ZERO] != 'y' && u1_tp_choice[0] != 'Y') /* User chooses not to retry. */ { u4_t_retry = (U4)FALSE; /* Update retry flag to FALSE. */ } } } system("pause"); /* Pause system to allow user to view the results. */ return u4_t_return_code; /* Return the result indicating success or failure. */ }我要这个代码的流程图
07-31
┌──(kali㉿kali)-[~] └─$ binwalk Binwalk v2.4.3 Original author: Craig Heffner, ReFirmLabs https://github.com/OSPG/binwalk Usage: binwalk [OPTIONS] [FILE1] [FILE2] [FILE3] ... Disassembly Scan Options: -Y, --disasm Identify the CPU architecture of a file using the capstone disassembler -T, --minsn=<int> Minimum number of consecutive instructions to be considered valid (default: 500) -k, --continue Don't stop at the first match Signature Scan Options: -B, --signature Scan target file(s) for common file signatures -R, --raw=<str> Scan target file(s) for the specified sequence of bytes -A, --opcodes Scan target file(s) for common executable opcode signatures -m, --magic=<file> Specify a custom magic file to use -b, --dumb Disable smart signature keywords -I, --invalid Show results marked as invalid -x, --exclude=<str> Exclude results that match <str> -y, --include=<str> Only show results that match <str> Extraction Options: -e, --extract Automatically extract known file types -D, --dd=<type[:ext[:cmd]]> Extract <type> signatures (regular expression), give the files an extension of <ext>, and execute <cmd> -M, --matryoshka Recursively scan extracted files -d, --depth=<int> Limit matryoshka recursion depth (default: 8 levels deep) -C, --directory=<str> Extract files/folders to a custom directory (default: current working directory) -j, --size=<int> Limit the size of each extracted file -n, --count=<int> Limit the number of extracted files -0, --run-as=<str> Execute external extraction utilities with the specified user's privileges -1, --preserve-symlinks Do not sanitize extracted symlinks that point outside the extraction directory (dangerous) -r, --rm Delete carved files after extraction -z, --carve Carve data from files, but don't execute extraction utilities -V, --subdirs Extract into sub-directories named by the offset Entropy Options: -E, --entropy Calculate file entropy -F, --fast Use faster, but less detailed, entropy analysis -J, --save Save plot as a PNG -Q, --nlegend Omit the legend from the entropy plot graph -N, --nplot Do not generate an entropy plot graph -H, --high=<float> Set the rising edge entropy trigger threshold (default: 0.95) -L, --low=<float> Set the falling edge entropy trigger threshold (default: 0.85) Binary Diffing Options: -W, --hexdump Perform a hexdump / diff of a file or files -G, --green Only show lines containing bytes that are the same among all files -i, --red Only show lines containing bytes that are different among all files -U, --blue Only show lines containing bytes that are different among some files -u, --similar Only display lines that are the same between all files -w, --terse Diff all files, but only display a hex dump of the first file Raw Compression Options: -X, --deflate Scan for raw deflate compression streams -Z, --lzma Scan for raw LZMA compression streams -P, --partial Perform a superficial, but faster, scan -S, --stop Stop after the first result General Options: -l, --length=<int> Number of bytes to scan -o, --offset=<int> Start scan at this file offset -O, --base=<int> Add a base address to all printed offsets -K, --block=<int> Set file block size -g, --swap=<int> Reverse every n bytes before scanning -f, --log=<file> Log results to file -c, --csv Log results to file in CSV format -t, --term Format output to fit the terminal window -q, --quiet Suppress output to stdout -v, --verbose Enable verbose output -h, --help Show help output -a, --finclude=<str> Only scan files whose names match this regex -p, --fexclude=<str> Do not scan files whose names match this regex -s, --status=<int> Enable the status server on the specified port [NOTICE] Binwalk v2.x will reach EOL in 12/12/2025. Please migrate to binwalk v3.x 怎样使用binwalk
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值