zoukankan      html  css  js  c++  java
  • [Windows] [VS] [C] [取得指针所指内存的二进制形式字符]

    // 取得指针所指内存的十六进制形式字符串,size指定字节长度
    #define Mem_toString(address, size) _Mem_toString((PBYTE)address, size)
    ////
    // 取得指针所指内存的十六进制形式字符串,size指定字节长度
    PSTR _Mem_toString(PBYTE address, size_t size);

    在上述接口已实现的前提下(http://www.cnblogs.com/develon/p/7834495.html),我们定义一个接口:

     1 #ifndef HEX2BIN_H_INC
     2 #define HEX2BIN_H_INC
     3 ////
     4 #include <Windows.h>
     5 ////
     6 ////
     7 // 将_Mem_toString(PBYTE, size_t)返回的字符处理为二进制形式
     8 PSTR Hex2Bin(PSTR pszHex);
     9 ////
    10 #endif // !HEX2BIN_H_INC
    Hex2Bin.h

    然后实现它:

     1 #include "Hex2Bin.h"
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <string.h>
     5 ////
     6 
     7 ////
     8 static PCSTR d[][2] = 
     9 {
    10     "0", "0000",
    11     "1", "0001",
    12     "2", "0010",
    13     "3", "0011",
    14     "4", "0100",
    15     "5", "0101",
    16     "6", "0110",
    17     "7", "0111",
    18     "8", "1000",
    19     "9", "1001",
    20     "A", "1010",
    21     "B", "1011",
    22     "C", "1100",
    23     "D", "1101",
    24     "E", "1110",
    25     "F", "1111",
    26 };
    27 ////
    28 // 将_Mem_toString(PBYTE, size_t)返回的字符处理为二进制形式
    29 PSTR Hex2Bin(PSTR pszHex){
    30     PSTR $pszBin = NULL;
    31     int $pszHexLength = strlen(pszHex);
    32     int $length = $pszHexLength/ 3 * 9 + 8 + 1; // 需要的字节长度
    33     int $i = 0; // 当前正在处理的pszHex下标
    34     int $location = 0; // $pszBin读写进度
    35 
    36     $pszBin = (PSTR) malloc($length);
    37     for( ; $i< $pszHexLength; $i++ ){
    38         int $j = 0;
    39         BOOL $flag = FALSE; // 当前字符已匹配?
    40         for( ; $j< 16; $j++ ){
    41             if(pszHex[$i] == *d[$j][0] ){
    42                 sprintf(&$pszBin[$location], "%s", d[$j][1]);
    43                 $location += 4;
    44                 $flag = TRUE;
    45             }
    46         }
    47         if(!$flag){ // 字符不在范围内0-F,可能是空格,也可能是其它字符
    48             if(pszHex[$i] == ' ' ){
    49                 sprintf(&$pszBin[$location],  " ");
    50                 $location += 1;
    51             }else{
    52                 sprintf(&$pszBin[$location],  "?");
    53                 $location += 1;
    54             }
    55         }
    56     }
    57     $pszBin[$length-1] = '';
    58     return $pszBin;
    59 }
    60 ////
    Hex2Bin.c

    测试:

     

  • 相关阅读:
    Go 语言简介(下)— 特性
    Array.length vs Array.prototype.length
    【转】javascript Object使用Array的方法
    【转】大话程序猿眼里的高并发架构
    【转】The magic behind array length property
    【转】Build Your own Simplified AngularJS in 200 Lines of JavaScript
    【转】在 2016 年做 PHP 开发是一种什么样的体验?(一)
    【转】大话程序猿眼里的高并发
    php通过token验证表单重复提交
    windows 杀进程软件
  • 原文地址:https://www.cnblogs.com/develon/p/7839075.html
Copyright © 2011-2022 走看看