十六进制解码_快速十六进制编码和解码

十六进制解码

In

Easy String Encryption Using CryptoAPI in C++ I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may contain embedded NULLs or other characters that can cause processing problems. 在C ++中使用CryptoAPI进行简单的字符串加密我描述了如何加密文本,并建议将加密的文本存储为一系列十六进制数字-因为密文可能包含嵌入的NULL或其他可能导致处理问题的字符。

Since then I've worked on an application in which large blocks of binary data must be encoded into hexadecimal rapidly, and when needed, decoded back to binary just as rapidly.  In the earlier article, I provided a sort of "throw-away" hex encoder/decoder, but I felt that I needed something that was more efficient for production work.

从那时起,我就致力于一个应用程序,在该应用程序中,必须将大型二进制数据块快速编码为十六进制,并在需要时将其解码为二进制。 在较早的文章中,我提供了一种“扔掉”的十六进制编码器/解码器,但我觉得我需要对生产工作更加有效的东西。

In this article, I'll explore some hex encode/decode options, including the one supported by the CryptoAPI, and at the end of the article, I'll present the ultra-fast functions that ended up in my production code.

在本文中,我将探讨一些十六进制编码/解码选项,包括。在本文的结尾,我将介绍最终在生产代码中使用的超快速功能。

Observe the ordering of characters in an ASCII Table The only real issue with hex encoding is that there is a gap between the 9 and the A.  Most of the complications come when dealing with that.

观察ASCII表中字符的顺序十六进制编码的唯一真正问题是9A之间存在间隙。 大多数并发症是在处理这些问题时出现的。

How to Encode to Hexadecimal

如何编码为十六进制

Your binary source byte is 8 bits.  Take the data 4 bits at a time (values 0-15).  If that value is 0-9, then just add '0' (0x30).  If the value is 10-15, then add 0x37 instead.  That gives you a hex digit ('0', '1',...,'9','A',..., 'F').  Output the resulting high hex digit followed by the low hex digit.

您的二进制源字节为8位。 一次取4位数据(值0-15)。 如果该值为0-9,则只需添加“ 0”(0x30)。 如果值为10-15,则添加0x37。 这会给您一个十六进制数字(“ 0”,“ 1”,...,“ 9”,“ A”,...,“ F”)。 输出结果高位十六进制数字,然后输出低位十六进制数字。

void ToHexManual( BYTE* pSrc, int nSrcLen, CString& sDest )
{
   
	sDest="";
	for ( int j=0; j<nSrcLen; j++ ) {
   
		BYTE b1= *pSrc >> 4;   // hi nybble
		BYTE b2= *pSrc & 0x0f; // lo nybble
		b1+='0'; if (b1>'9') b1 += 7;  // gap between '9' and 'A'
		b2+='0'; if (b2>'9') b2 += 7;  
		sDest += b1; 
		sDest += b2; 
		pSrc++;
	}
}
void ToHexSlow( BYTE* pSrc, int nSrcLen, CString& sDest )
{
   
	sDest="";
	for ( int j=0; j<nSrcLen; j++ ) {
   
		sDest.AppendFormat( "%02x", int(*pSrc) );
		pSrc++;
	}
}
significant problem when the output is large.  Let me explain...

Significant Problem with Long Strings

长字符串的重大问题

Each time you concatenate to an existing CString (or an STL string object), the code must check to see if that current allocation is large enough.  If not, it must allocate a new chunk that is larger, and copy the existing contents to that location before appending the new text.  Imagine how clumsy that gets when the string is about 1MB long!

每次连接到

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值