十六进制字符串

十六进制字符串1//Copyright(c)2//Allrightsreserved.3//4//Describe:Hexstring5//Author:Qing6//Date:2014-08-1478#ifndef__HEX_STRING_…

大家好,欢迎来到IT知识分享网。十六进制字符串"

 1 // Copyright (c) 
 2 // All rights reserved.
 3 //
 4 //Describe : Hex string
 5 //Author   : Qing
 6 //Date     : 2014-08-14
 7 
 8 #ifndef __HEX_STRING_H__
 9 #define __HEX_STRING_H__
10 
11 #include <string>
12 
13 namespace Qing
14 {
15     class HexString
16     {
17     public:
18 
19         HexString(void);
20         ~HexString(void);
21 
22         int HexCharToInt(char ch) const;
23         std::string AddSpace(const std::string &SourcesString, int Interval) const;
24 
25         void HexToString(const unsigned char* HexCharArray, int ArraySize, unsigned char* TargetBuffer, int BufferSize) const;
26         void StringToHex(std::string &MyString, const unsigned char* SourcesCharArray, int ArraySize, bool IsLowerCase) const;
27     };
28 }
29 
30 #endif

 

 1 #include "StdAfx.h"
 2 #include "HexString.h"
 3 
 4 namespace Qing
 5 {
 6     HexString::HexString(void)
 7     {
 8     }
 9 
10     HexString::~HexString(void)
11     {
12     }
13 
14     int HexString::HexCharToInt(char Ch) const
15     {
16         if(Ch >= '0' && Ch <= '9') return (Ch - '0');
17         if(Ch >= 'A' && Ch <= 'F') return (Ch - 'A' + 10);
18         if(Ch >= 'a' && Ch <= 'f') return (Ch - 'a' + 10);
19 
20         return 0;
21     }
22 
23     std::string HexString::AddSpace(const std::string &SourcesString, int Interval) const
24     {
25         std::string Space(" ");
26         std::string TempString = SourcesString;
27 
28         for(std::string::size_type Index = Interval; Index < TempString.size(); Index += Interval)
29         {
30             TempString.insert(Index, Space);
31             ++Index;
32         }
33 
34         return TempString;
35     }
36 
37     void HexString::HexToString(const unsigned char* HexCharArray, int ArraySize, unsigned char* TargetBuffer, int BufferSize) const
38     {
39         memset(TargetBuffer, 0, BufferSize);
40 
41         for(int Index = 0; Index < ArraySize; Index+=2)
42         {
43             TargetBuffer[Index/2] = static_cast<char>(((HexCharToInt(HexCharArray[Index]) << 4) | HexCharToInt(HexCharArray[Index+1])));
44         }
45     }
46 
47     void HexString::StringToHex(std::string &MyString, const unsigned char* SourcesCharArray, int ArraySize, bool IsLowerCase) const
48     {
49         MyString.clear();
50         std::string FlagString(IsLowerCase ? "0123456789abcdef" : "0123456789ABCDEF");
51 
52         int b = 0;
53         for(int i = 0; i < ArraySize; i++)
54         {
55             b = 0x0f & (SourcesCharArray[i] >> 4);
56             MyString.append(1, FlagString.at(b));
57 
58             b = 0x0f & SourcesCharArray[i];
59             MyString.append(1, FlagString.at(b));
60         }
61     }
62 }

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32999.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信