【问题标题】:How to turn std::string length into hex and than back into string?如何将 std::string 长度转换为十六进制而不是返回字符串?
【发布时间】:2011-12-21 12:27:23
【问题描述】:

所以...我想创建简单的HTTP Chunked transfer encoding 原型。我有消息作为 std::strings。而且我的服务器 API 都是基于字符串的......所以我想知道如何将 std::string 长度转换为十六进制而不是返回字符串?

假设我们有std::string("This is the data in the first chunk\r\n").length(),它会返回say int 37。我想将其转换为十六进制 0x25,而不是从该十六进制 std::string("25") 中取出。如何做这样的事情(使用 stl 和 boost)?

【问题讨论】:

  • 你不知道将整数转换为十六进制?那么你怎么能认为你可以使用 HTTP 或任何网络协议呢?
  • @Nawaz:通过学习,一步一个脚印。
  • @MikeSeymour:绝对是这样。但从 OP 的帖子看来并非如此,或者您不明白我在之前的评论中的意思。我的意思是,如果我说我想设计和编写自己的操作系统,而我对编程一无所知?
  • @Nawaz:如果你不这么说,而是说一些完全合理的东西并且答案很简短怎么办?例如,如何在 C++ 中将整数转换为十六进制字符串。也许提问者有相当多的网络经验,但不知道 C++ 流格式的奇怪细节。
  • @MikeSeymour:同样,将十进制整数转换为十六进制整数与 C++ 库或流无关。

标签: c++ string boost stl hex


【解决方案1】:
#include <sstream>
#include <iomanip>
#include <iostream>

int main() {
    // To string:

    std::ostringstream oss;
    oss << std::hex << 37;
    std::string result = oss.str();

    std::cout << result << '\n';

    // Back from string (if you need it):

    std::istringstream iss(result);
    int original;
    if (!(iss >> std::hex >> original)) {
        // handle error here
    } else {
        std::cout << original << '\n';
    }
}

【讨论】:

    【解决方案2】:
    std::stringstream buffer;
    buffer << std::hex << your_number;
    

    buffer.str() 现在会给你一个十六进制表示的号码;如果您想在数字前添加0x,请使用:

    buffer << std::hex << showbase << your_number;
    

    【讨论】:

      【解决方案3】:
      #include <sstream>
      #include <iomanip>
      
      std::ostringstream str;
      str << "0x" << std::hex << length;
      
      std::string result = str.str();
      

      证明here

      【讨论】:

        【解决方案4】:

        字符串流是一种方式:

        std::ostringstream ss;
        ss << "0x" << std::hex << 12345;
        std::string aString = ss.str();
        

        另一种选择是全能的boost::format

        【讨论】:

          【解决方案5】:

          这将是一个解决方案:

          std::string convert_length_to_hex(const std::string& str)
          {
              std::ostringstream result;
              result << std::hex << str.length();
              return result.str();
          }
          

          【讨论】:

            【解决方案6】:

            这是我的十进制到十六进制转换然后返回的示例

            #include <iostream>
            #include <sstream>
            #include <string>
            #include <algorithm>   
            
            std::string dec2hex(int dec){
                std::string result;    
                std::stringstream ss;
                ss << std::hex << dec;
                ss >> result;
                std::transform(result.begin(), result.end(), result.begin(), ::toupper);
                return result;}
            
            int hex2dec(std::string& hex){
                int x;
                std::stringstream ss;
                ss << std::hex << hex;
                ss >> x;
                return x;
            }
            
            int main()
            {
                std::string hex = "ABCD";
                int dec = hex2dec(hex);
                std::cout << dec << std::endl;
                std::cout << dec2hex(dec) << std::endl;
                return 0;
            }
            

            【讨论】:

              猜你喜欢
              • 2012-03-25
              • 1970-01-01
              • 2012-11-09
              • 2016-06-13
              • 2013-02-07
              • 2012-07-22
              • 1970-01-01
              • 2011-08-24
              相关资源
              最近更新 更多