只能考虑按位处理,因为数字超长会溢出。
下面是一个把64byte的数字变成16进制的数字字符串的例子
static const char *id_hex_digits = "0123456789abcdef";
const char* bpeer_id_to_string(BSS_DATA_HASH_ID* m_hashid,char m_str_dist[BSS_DATA_HASH_ID_STRING_LENGTH] )
{
hash_byte *d = m_hashid->id;
int i;
char* s_dist=m_str_dist;
for (i = 0; i < SHA512_DIGEST_LENGTH; i++)
{
*s_dist++ = id_hex_digits [(*d & 0xf0) >> 4];
*s_dist++ = id_hex_digits [*d & 0x0f];
d++;
}
*s_dist = '\0';
return m_str_dist;
}
怎么写变成10进制的数字字符串函数?
下面是一个把64byte的数字变成16进制的数字字符串的例子
static const char *id_hex_digits = "0123456789abcdef";
const char* bpeer_id_to_string(BSS_DATA_HASH_ID* m_hashid,char m_str_dist[BSS_DATA_HASH_ID_STRING_LENGTH] )
{
hash_byte *d = m_hashid->id;
int i;
char* s_dist=m_str_dist;
for (i = 0; i < SHA512_DIGEST_LENGTH; i++)
{
*s_dist++ = id_hex_digits [(*d & 0xf0) >> 4];
*s_dist++ = id_hex_digits [*d & 0x0f];
d++;
}
*s_dist = '\0';
return m_str_dist;
}
怎么写变成10进制的数字字符串函数?
SHA512_DIGEST_LENGTH 长度为64
单步跟进就可以看到atoi的源码。
比如
int i = 0;
char ch = i+48;
这个ch字符就是'0'。我说对问题了吗?
const char* bpeer_id_to_dec_string(BSS_DATA_HASH_ID* m_hashid,char m_dist[BSS_DATA_HASH_ID_STRING_LENGTH*3] )
{
hash_byte *d = m_hashid->id;
int i;
int m_str_len=0;
int b;
char m_str_dist[BSS_DATA_HASH_ID_STRING_LENGTH*3];
memset(m_str_dist,0,BSS_DATA_HASH_ID_STRING_LENGTH*3);
for (i = 0; i < SHA512_DIGEST_LENGTH; i++)
{
char c=0;
hash_byte c_tmp = d[i];
for( c=7;c>=0;--c)
{
for (b=0;b<m_str_len;b++)
m_str_dist[b] *=2;
m_str_dist[0] +=((c_tmp >> c) & 1);
for (b=0;b<m_str_len;b++)
{
m_str_dist[b+1] += m_str_dist[b]/10;
m_str_dist[b] %=10;
}
m_str_len +=m_str_dist[m_str_len];
}
}
//inversion copy
char* seek_dist=m_dist;
for (i=m_str_len-1;i>=0;i--)
*seek_dist++=m_str_dist[i]+'0';
*seek_dist = '\0';
return m_dist;
}