-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrConversion.cpp
61 lines (49 loc) · 1.41 KB
/
StrConversion.cpp
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
/////////////////////////////////////////////////////////////////////////////
// StrConversion.cpp
//
// Copyright (C) 2013 Veria Kalantari
// veria.kalantary@gmail.com
// https://github.com/veriak
//
#include "stdafx.h"
#include "StrConversion.h"
using namespace StrConversion;
/////////////////////////////////////////////////////////////////////////////
// CStrConverter
CStrConverter::CStrConverter(void)
{
}
CStrConverter::~CStrConverter(void)
{
}
void CStrConverter::HexStrToByteArray(const char *lpHexStr, unsigned char **lppByteArray,
unsigned int *lpnBuffSize)
{
*lpnBuffSize = (unsigned int) strlen(lpHexStr) / 2;
unsigned char *lpByteArray = (unsigned char *) malloc(*lpnBuffSize);
*lppByteArray = lpByteArray;
char *lpSource = (char *) lpHexStr;
while (*lpSource != '\0')
{
lpByteArray[0] = BASE16_DECODELO(lpSource[0]);
lpByteArray[0] |= BASE16_DECODEHI(lpSource[1]);
lpByteArray++;
lpSource += 2;
}
}
void CStrConverter::ByteArrayToHexStr(const unsigned char *lpByteArray, unsigned int nBuffSize,
char **lppHexStr)
{
char *lpHexStr = (char *) malloc(nBuffSize * 2 + 1);
unsigned int nCount = 0;
*lppHexStr = lpHexStr;
while (nCount < nBuffSize)
{
lpHexStr[0] = BASE16_ENCODELO(*lpByteArray);
lpHexStr[1] = BASE16_ENCODEHI(*lpByteArray);
lpHexStr += 2;
lpByteArray++;
nCount++;
}
*lpHexStr = '\0';
}