0

So... I want to create simple HTTP Chunked transfer encoding prototype. I have messages as std::strings. And my server API is all string based... so I wonder how to turn std::string length into hex and than back into string?

So say we had std::string("This is the data in the first chunk\r\n").length() that would return say int 37. I want to convert it into hex 0x25 and than to get out from that hex std::string("25"). How to do such thing (using stl and boost)?

8
  • 3
    You don't know to convert an integer into hexadecimal? Then how can you even think that you can work with HTTP or any network protocol for that matter? Commented Dec 21, 2011 at 12:29
  • @Nawaz: by learning, one step at a time. Commented Dec 21, 2011 at 12:33
  • @MikeSeymour: That is definitely the way. But it doesn't seem so from OP's post, or you didn't understand what I meant in my previous comment. I mean what if I say I want to design and write my own OS, when I don't know anything about programming? Commented Dec 21, 2011 at 12:36
  • 3
    @Nawaz: and what if you don't say that, but instead you say something entirely reasonable and that has a short answer? For example, how to convert an integer to a hex string in C++. Maybe the questioner has a reasonable amount of experience with networking, but doesn't know the bizarre details of C++ stream formatting. Commented Dec 21, 2011 at 12:45
  • 1
    @MikeSeymour: Again, converting a decimal integer into hexadecimal integer has nothing to do with C++ library or stream. Commented Dec 21, 2011 at 12:54

6 Answers 6

4
#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';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2
std::stringstream buffer;
buffer << std::hex << your_number;

buffer.str() will now give you a hex-representation of your number; If you want the 0x before the number, use this:

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

Comments

1
#include <sstream>
#include <iomanip>

std::ostringstream str;
str << "0x" << std::hex << length;

std::string result = str.str();

Demonstrated here.

Comments

1

Stringstreams are one way:

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

An alternative is the all-mighty boost::format.

Comments

1

This would be a solution:

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

Comments

1

Here is my example of dec to hex conversion and then back

#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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.