简体   繁体   English

c++中将一长串字符转换为uint32_t或uint64_t

[英]convert a long string of characters to uint32_t or uint64_t in c++

I want to convert a string containing alphanumeric characters into either uint32_t or uint64_t.我想将包含字母数字字符的字符串转换为 uint32_t 或 uint64_t。 Tried doing the following.尝试执行以下操作。 But, I am getting "terminate called after throwing an instance of 'std::out_of_range' " error.但是,我收到“在抛出 'std::out_of_range' 实例后调用终止”错误。 Also, when the string is smaller in length, for example : string s = "hello", it works, but what if I want to convert a longer string into uint32_t or uint64_t.此外,当字符串的长度较小时,例如:string s = "hello",它可以工作,但是如果我想将较长的字符串转换为 uint32_t 或 uint64_t 该怎么办。

#include <iostream>
#include <sstream>
#include <iomanip>

std::string string_to_hex(const std::string& in) {
    std::stringstream ss;
    ss << std::hex << std::setfill('0');
    for (size_t i = 0; i < in.size(); ++i) {
        ss << std::setw(2) << static_cast<unsigned int>(static_cast<unsigned char>(in[i]));
    }

    return ss.str(); 
}

int main() {
   std::string s = "hello world, this 123";
   std::string hex_str = string_to_hex(s);
   uint32_t value = std::stoul(hex_str , nullptr, 16);
   cout<<value<<endl; 
}

Okay, so according to the comments given, is the following the best way to do it?好的,所以根据给出的评论,以下是最好的方法吗?

int main()
{
    std::string s = "hello world, this 123";
    std::hash<std::string> hashed_name;
    uint32_t value = hashed_name(s);
    cout<<value<<endl;

    return 0;
}

This, strictly speaking, does what you need:严格来说,这可以满足您的需求:

#include <iostream>
#include <string>
#include <unordered_map>

class Converter
{
public:
  static int StringToInt(const std::string& s)
  {
    auto it = cache.find(s);
    if (it != cache.end())
      return it->second;
    cache[s] = count;
    lookup[count++] = s;
  }
  static std::string IntToString(unsigned i)
  {
    auto it = lookup.find(i);
    if (it != lookup.end())
      return it->second;
    return "";
  }
private:
  static inline unsigned count = 0;
  static inline std::unordered_map<std::string, int> cache;
  static inline std::unordered_map<int, std::string> lookup;
};

int main() {
  std::string s = "hello world, this 123";
  int  value = Converter::StringToInt(s);
  std::cout << value << std::endl;
  std::string s2 = Converter::IntToString(value);
  std::cout << s2 << std::endl;
  int  value2 = Converter::StringToInt("hello world, this 123");
  std::cout << value2 << std::endl;

}

Your issue is that the resultant hex_str is a MASSIVE number.您的问题是生成的hex_str是一个hex_str的数字。 Specifically: 0x68656c6c6f20776f726c642c207468697320313233具体: 0x68656c6c6f20776f726c642c207468697320313233

You are converting every character to a two-digit hex value (ie one byte).您正在将每个字符转换为两位十六进制值(即一个字节)。 Strings of size 4 (8 if using 64 bit int) or more is going to result in a number way too large to fit in your resultant uint32_t or uint64_t大小为 4(如果使用 64 位 int 则为 8)或更多的字符串将导致数字太大而无法放入生成的uint32_tuint64_t

See this GDB printout of your program查看您程序的 GDB 打印输出

As Remy mentioned in the comments, look into hash algorithms.正如 Remy 在评论中提到的,研究哈希算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C 或 C++ 中,uint64_t 的 memory 布局是否保证与 uint32_t[2] 相同? 可以将一个转换为另一个吗? - In C or C++, is memory layout of uint64_t guaranteed to be the same as uint32_t[2] ? Can one be cast as the other? 函数重载,只有c ++中的纯虚函数中的“ uint32_t”和uint64_t”发生了变化 - Function overloading with the only change in “uint32_t” & uint64_t" in pure virtual function in c++ C++ 将字符串转换为 uint64_t - C++ convert string to uint64_t 在 C++20 中输入双关 uint64_t 作为两个 uint32_t - Type-pun uint64_t as two uint32_t in C++20 无符号长的类型与Windows上的uint32_t和uint64_t不同(VS2010) - Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010) 将uint64_t转换为std :: string - Convert uint64_t to std::string 在c ++中将位序列转换为uint32_t - Convert bit sequence to uint32_t in c++ 将uint32_t转换为uint64_t导致不同的值? - casting uint32_t to uint64_t results in different value? 如何以类型安全的方式分配uint32_t :: max和uint64_t的最小值? - How to assign min of uint32_t::max and uint64_t in type safe manner? 使用多个uint32_t整数生成uint64_t哈希键 - Generate uint64_t hash key with several uint32_t integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM