base32在C+中的转换+; 有人知道C++常用的库,它提供了从基10到基32和VeVeVaS.编码和解码的方法吗?
谢谢,base32在C+中的转换+; 有人知道C++常用的库,它提供了从基10到基32和VeVeVaS.编码和解码的方法吗?,c++,base32,C++,Base32,谢谢, 斯蒂法诺< /P> < P>我不知道任何常用于BASE32编码的库,但是Cuffo++包含a,< P>>强> [更新] < /强>显然,C++ IO操作器和正常 > I/Cord> IO运算符只处理基8, 10和16,因此对于处理基32无效。 所以要解决你的转换问题 以10/32为基数的字符串表示从程序中的某些输入读取到整数的数字 将程序中的整数转换为以10/32为基数表示的字符串以供输出 您将需要求助于其他功能 要将包含基2..36表示形式的C样式字符串转换为整数,可以使用#in
斯蒂法诺< /P> < P>我不知道任何常用于BASE32编码的库,但是Cuffo++包含a,< P>>强> [更新] < /强>显然,C++ IO操作器和正常<代码> <代码> > I/Cord> IO运算符只处理基8, 10和16,因此对于处理基32无效。 所以要解决你的转换问题
- 以10/32为基数的字符串表示从程序中的某些输入读取到整数的数字
- 将程序中的整数转换为以10/32为基数表示的字符串以供输出
#include
和strtol(3)
&Co.函数集
至于将整数转换为具有任意基的字符串。。。我找不到一个简单的答案printf(3)
样式格式字符串仅处理基数为8、10、16的AFAIC,就像std::setbase
一样。有人吗?你的意思是“基数10到基数32”,而不是整数到基数32?后者似乎更有可能更有用;默认情况下,标准格式的I/O函数在处理整数时生成基10字符串格式
对于从基32到整数的转换,标准库strtol()函数将执行此操作。相反,你不需要一个库来实现你自己可以轻松实现的东西(不是所有的东西都是乐高积木)
这里有一个例子,不一定是最有效的,但很简单
#include <cstring>
#include <string>
long b32tol( std::string b32 )
{
return strtol( b32.c_str(), 0, 32 ) ;
}
std::string itob32( long i )
{
unsigned long u = *(reinterpret_cast<unsigned long*>)( &i ) ;
std::string b32 ;
do
{
int d = u % 32 ;
if( d < 10 )
{
b32.insert( 0, 1, '0' + d ) ;
}
else
{
b32.insert( 0, 1, 'a' + d - 10 ) ;
}
u /= 32 ;
} while( u > 0 );
return b32 ;
}
#include <iostream>
int main()
{
long i = 32*32*11 + 32*20 + 5 ; // BK5 in base 32
std::string b32 = itob32( i ) ;
long ii = b32tol( b32 ) ;
std::cout << i << std::endl ; // Original
std::cout << b32 << std::endl ; // Converted to b32
std::cout << ii << std::endl ; // Converted back
return 0 ;
}
#包括
#包括
长b32tol(标准::字符串b32)
{
返回strtol(b32.c_str(),0,32);
}
标准::字符串itob32(长i)
{
无符号长u=*(重新解释_-cast)(&i);
std::字符串b32;
做
{
int d=u%32;
如果(d<10)
{
b32.插入(0,1,'0'+d);
}
其他的
{
b32.插入(0,1,'a'+d-10);
}
u/=32;
}而(u>0);
返回b32;
}
#包括
int main()
{
长i=32*32*11+32*20+5;//基32中的BK5
std::字符串b32=itob32(i);
长ii=b32tol(b32);
STD::CUT< P>直接回答原始(现在是老)问题,我不知道在BASE32中有什么通用的字节数组编码,或者是在以后解码它们。但是,上周我提出了一个需要解码BASE32中表示的Sh1哈希值到它们的原始字节数组的方法。这里有一些C++代码。(有一些值得注意的窗口/小endian工件)我写这篇文章就是为了验证结果
请注意,与上面Clifford的代码相反,如果我没有弄错的话,它假定RFC4648中提到的“base32hex”字母表,我的代码假定为“base32”字母表(“A-Z”和“2-7”)
//此程序说明了如何对base32编码形式的SHA1哈希值进行解码
//然后在base16中重新编码。
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用名称空间std;
无符号字符Base16EncodeNibble(无符号字符值)
{
如果(value>=0&&value=10&&value),我很确定从基数10到基数32的数字转换与base32编码完全不同。std::setbase()
只接受8、10和16的值。谢谢,请注意,代码中的转换似乎只适用于正整数。我正在处理它。进行了一次更正,但仍然不正确。请自己动手。
// This program illustrates how SHA1 hash values in base32 encoded form can be decoded
// and then re-encoded in base16.
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <cassert>
using namespace std;
unsigned char Base16EncodeNibble( unsigned char value )
{
if( value >= 0 && value <= 9 )
return value + 48;
else if( value >= 10 && value <= 15 )
return (value-10) + 65;
else //assert(false);
{
cout << "Error: trying to convert value: " << value << endl;
}
return 42; // sentinal for error condition
}
void Base32DecodeBase16Encode(const string & input, string & output)
{
// Here's the base32 decoding:
// The "Base 32 Encoding" section of http://tools.ietf.org/html/rfc4648#page-8
// shows that every 8 bytes of base32 encoded data must be translated back into 5 bytes
// of original data during a decoding process. The following code does this.
int input_len = input.length();
assert( input_len == 32 );
const char * input_str = input.c_str();
int output_len = (input_len*5)/8;
assert( output_len == 20 );
// Because input strings are assumed to be SHA1 hash values in base32, it is also assumed
// that they will be 32 characters (and bytes in this case) in length, and so the output
// string should be 20 bytes in length.
unsigned char *output_str = new unsigned char[output_len];
char curr_char, temp_char;
long long temp_buffer = 0; //formerly: __int64 temp_buffer = 0;
for( int i=0; i<input_len; i++ )
{
curr_char = input_str[i];
if( curr_char >= 'A' && curr_char <= 'Z' )
temp_char = curr_char - 'A';
if( curr_char >= '2' && curr_char <= '7' )
temp_char = curr_char - '2' + 26;
if( temp_buffer )
temp_buffer <<= 5; //temp_buffer = (temp_buffer << 5);
temp_buffer |= temp_char;
// if 8 encoded characters have been decoded into the temp location,
// then copy them to the appropriate section of the final decoded location
if( (i>0) && !((i+1) % 8) )
{
unsigned char * source = reinterpret_cast<unsigned char*>(&temp_buffer);
//strncpy(output_str+(5*(((i+1)/8)-1)), source, 5);
int start_index = 5*(((i+1)/8)-1);
int copy_index = 4;
for( int x=start_index; x<(start_index+5); x++, copy_index-- )
output_str[x] = source[copy_index];
temp_buffer = 0;
// I could be mistaken, but I'm guessing that the necessity of copying
// in "reverse" order results from temp_buffer's little endian byte order.
}
}
// Here's the base16 encoding (for human-readable output and the chosen validation tests):
// The "Base 16 Encoding" section of http://tools.ietf.org/html/rfc4648#page-10
// shows that every byte original data must be encoded as two characters from the
// base16 alphabet - one charactor for the original byte's high nibble, and one for
// its low nibble.
unsigned char out_temp, chr_temp;
for( int y=0; y<output_len; y++ )
{
out_temp = Base16EncodeNibble( output_str[y] >> 4 ); //encode the high nibble
output.append( 1, static_cast<char>(out_temp) );
out_temp = Base16EncodeNibble( output_str[y] & 0xF ); //encode the low nibble
output.append( 1, static_cast<char>(out_temp) );
}
delete [] output_str;
}
int _tmain(int argc, _TCHAR* argv[])
{
//string input = "J3WEDSJDRMJHE2FUHERUR6YWLGE3USRH";
vector<string> input_b32_strings, output_b16_strings, expected_b16_strings;
input_b32_strings.push_back("J3WEDSJDRMJHE2FUHERUR6YWLGE3USRH");
expected_b16_strings.push_back("4EEC41C9238B127268B4392348FB165989BA4A27");
input_b32_strings.push_back("2HPUCIVW2EVBANIWCXOIQZX6N5NDIUSX");
expected_b16_strings.push_back("D1DF4122B6D12A10351615DC8866FE6F5A345257");
input_b32_strings.push_back("U4BDNCBAQFCPVDBL4FBG3AANGWVESI5J");
expected_b16_strings.push_back("A7023688208144FA8C2BE1426D800D35AA4923A9");
// Use the base conversion tool at http://darkfader.net/toolbox/convert/
// to verify that the above base32/base16 pairs are equivalent.
int num_input_strs = input_b32_strings.size();
for(int i=0; i<num_input_strs; i++)
{
string temp;
Base32DecodeBase16Encode(input_b32_strings[i], temp);
output_b16_strings.push_back(temp);
}
for(int j=0; j<num_input_strs; j++)
{
cout << input_b32_strings[j] << endl;
cout << output_b16_strings[j] << endl;
cout << expected_b16_strings[j] << endl;
if( output_b16_strings[j] != expected_b16_strings[j] )
{
cout << "Error in conversion for string " << j << endl;
}
}
return 0;
}