yy1717
2020-01-09 74bb2be0e23e9f2290ff8ecfb6506acf8a070339
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Created by YY on 2020/1/7.
//
 
#include "xconvert.h"
#include <cstdint>
#include <cstring>
 
void ConvertPhoneNum(uint8_t *dst, int length, const char *src)
{
    int p = length - 1;
    int q = strlen(src) - 1;
 
    memset(dst, 0, length);
 
    while (q >= 0) {
        dst[p] = src[q] - '0';
        q--;
        if (q >= 0) {
            dst[p] |= (src[q] - '0')<<4;
            p--;
            q--;
        }
    }
}
 
void ConvertHex2String(char *str, const uint8_t *hex, int length)
{
    const char HEX[] = "0123456789ABCDEF";
    int j = 0;
 
    for (int i = 0; i < length; ++i) {
        str[j++] = HEX[(hex[i]>>4)&0x0F];
        str[j++] = HEX[hex[i]&0x0F];
    }
    str[j++] = 0;
}
 
void ConvertString2Hex(uint8_t *hex, int length, const char *str)
{
    if (length < strlen(str)/2) return;
 
    for (int i = 0, j = 0; i < strlen(str); ++i, ++j) {
        if (str[i] >= '0' && str[i] <= '9') {
            hex[j] = str[i] - '0';
        } else if (str[i] >= 'A' && str[i] <= 'F') {
            hex[j] = 10 + str[i] - 'A';
        } else if (str[i] >= 'a' && str[i] <= 'f') {
            hex[j] = 10 + str[i] - 'a';
        }
        hex[j] <<= 4;
        i++;
        if (str[i] >= '0' && str[i] <= '9') {
            hex[j] |= str[i] - '0';
        } else if (str[i] >= 'A' && str[i] <= 'F') {
            hex[j] |= 10 + str[i] - 'A';
        } else if (str[i] >= 'a' && str[i] <= 'f') {
            hex[j] |= 10 + str[i] - 'a';
        }
    }
}