使用c++实现一个算法,实现uint64_t的数和一个长度为15的字符串的相互转换,字符串包含数字和大写字母,时间复杂度尽可能小
时间: 2023-01-30 14:08:01 浏览: 130
首先,我们来看如何将一个uint64_t数字转换为字符串。这里假设我们有一个名为num的uint64_t类型的数字,我们希望将其转换为一个包含数字和大写字母的长度为15的字符串。
我们可以使用一个循环来不断地取余数,然后将余数转换为字符。如下所示:
```c
char str[16]; // 字符串数组,用于存储转换后的字符串
int i = 0;
while (num > 0) {
int remainder = num % 36; // 计算余数
if (remainder < 10) {
str[i] = '0' + remainder; // 将余数转换为数字字符
} else {
str[i] = 'A' + (remainder - 10); // 将余数转换为大写字母字符
}
num /= 36; // 更新num的值
i++;
}
// 将字符串数组倒序,使得字符串中的数字和字母顺序与原数字的顺序一致
for (int j = 0; j < i / 2; j++) {
char temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
// 在字符串数组末尾添加结束符
str[i] = '\0';
```
接下来,我们来看如何将一个长度为15的字符串转换为uint64_t数字。这里假设我们有一个名为str的字符串数组,我们希望将其转换为一个uint64_t数字。
我们可以使用一个
相关问题
fnv1a64 哈希算法 用c++实现
以下是使用C++实现FNV1a64哈希算法的示例代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
const uint64_t FNV_OFFSET_BASIS = 14695981039346656037ull;
const uint64_t FNV_PRIME = 1099511628211ull;
uint64_t fnv1a64(const void* data, size_t size)
{
uint64_t hash = FNV_OFFSET_BASIS;
const uint8_t* ptr = static_cast<const uint8_t*>(data);
for (size_t i = 0; i < size; ++i)
{
hash ^= static_cast<uint64_t>(ptr[i]);
hash *= FNV_PRIME;
}
return hash;
}
int main()
{
const char* str = "Hello, world!";
uint64_t hash = fnv1a64(str, strlen(str));
cout << "Hash of \"" << str << "\" is: " << hash << endl;
return 0;
}
```
在这个示例中,`fnv1a64`函数接收一个指向数据的指针和数据的大小,并返回计算出的哈希值。首先,将哈希值初始化为偏移基础值`FNV_OFFSET_BASIS`。然后,对于数据中的每个字节,使用异或运算符将其与哈希值进行异或运算,然后将哈希值乘以FNV素数`FNV_PRIME`。最后,返回计算出的哈希值。
在`main`函数中,我们将一个字符串传递给`fnv1a64`函数,并输出计算出的哈希值。
请注意,此代码中使用了C++11标准中的`uint64_t`和`size_t`类型,因此您需要编译器支持C++11才能编译此代码。
C++实现SHA-1算法
以下是C++实现SHA-1算法的示例代码:
```c++
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdint>
// 左移循环移位
#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
// SHA-1 常量
#define SHA1_K0 0x5A827999
#define SHA1_K1 0x6ED9EBA1
#define SHA1_K2 0x8F1BBCDC
#define SHA1_K3 0xCA62C1D6
// SHA-1 初始化向量
const std::uint32_t SHA1_IV[] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
// SHA-1 消息填充
void sha1_pad(std::string& message) {
// 向消息尾部添加一个比特 1
message += '\x80';
// 添加 0 到 64 位填充,使消息长度满足模 512 余 448
std::size_t original_size = message.size();
std::size_t padding_size = (56 - (original_size % 64)) % 64;
message.resize(original_size + padding_size, '\0');
// 在消息尾部添加 64 位的原始消息长度
std::uint64_t message_bits = original_size * 8;
message.append(reinterpret_cast<const char*>(&message_bits), sizeof(message_bits));
}
// SHA-1 压缩函数
void sha1_compress(std::uint32_t* state, const std::uint8_t* block) {
// 初始化变量
std::uint32_t a = state[0];
std::uint32_t b = state[1];
std::uint32_t c = state[2];
std::uint32_t d = state[3];
std::uint32_t e = state[4];
std::uint32_t w[80];
// 将 16 个字分组成 80 个字
for (int i = 0; i < 16; i++) {
w[i] = (block[i * 4] << 24) | (block[i * 4 + 1] << 16) | (block[i * 4 + 2] << 8) | block[i * 4 + 3];
}
for (int i = 16; i < 80; i++) {
w[i] = ROTL(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
}
// SHA-1 主循环
for (int i = 0; i < 80; i++) {
std::uint32_t f, k;
if (i < 20) {
f = (b & c) | ((~b) & d);
k = SHA1_K0;
} else if (i < 40) {
f = b ^ c ^ d;
k = SHA1_K1;
} else if (i < 60) {
f = (b & c) | (b & d) | (c & d);
k = SHA1_K2;
} else {
f = b ^ c ^ d;
k = SHA1_K3;
}
std::uint32_t temp = ROTL(a, 5) + f + e + k + w[i];
e = d;
d = c;
c = ROTL(b, 30);
b = a;
a = temp;
}
// 更新状态
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
}
// 计算 SHA-1 哈希值
std::string sha1(const std::string& message) {
// 初始化状态
std::uint32_t state[5];
std::memcpy(state, SHA1_IV, sizeof(SHA1_IV));
// 对消息进行填充
std::string padded_message = message;
sha1_pad(padded_message);
// 对填充后的消息进行压缩
for (std::size_t i = 0; i < padded_message.size(); i += 64) {
sha1_compress(state, reinterpret_cast<const std::uint8_t*>(&padded_message[i]));
}
// 将结果转换为十六进制字符串
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for (int i = 0; i < 5; i++) {
oss << std::setw(8) << state[i];
}
return oss.str();
}
// 测试
int main() {
std::cout << sha1("Hello, world!") << std::endl; // 0x7b502c3a1f48c860e3c0feeb6a1c9a22d1aee6cb
return 0;
}
```
在该实现中,`sha1()` 函数接受一个字符串作为输入,并返回该字符串的 SHA-1 哈希值。哈希值以十六进制字符串的形式返回。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)