Convert __int64 to std :: u32string

I am writing my own Windows C ++ application that uses the Casablanca REST API. I am trying to pass an integer value from a C ++ application to a Java servlet that will run in the cloud. When making a GET

REST call , the Casablanca API expects what I will use std::u32string

to store the request parameter. It's somewhat intuitive for me why one needs to use UTF-32 encoding to ensure support for each character type. I am not interested in how to do this.

Here is my current code:

__int64 queryID = 12345689;               // the integer to pass
std::stringstream stream;
stream << queryID;
std::string queryID_utf8(stream.str());   // the integer as a UTF-8 string
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
std::u32string queryID_utf32 = convert.from_bytes(queryID_utf8); // UTF-32 string

http_client client(U("http://localhost:8080/MyJavaWebApp"));
uri_builder builder(U("/getContent"));
builder.append_query(U("queryID"), queryID_utf32.c_str());
client.request(methods::GET, builder.to_string()) // etc.

      

I'm also not entirely sure how I am supposed to handle things on the Java side, once I get this UTF-32 encoded string. Any expert advice in C ++ would be greatly appreciated here.

+3


source to share


2 answers


I used wchar

and swprintf

etc. when I was working with Casablanca in C ++.



pplx::task<MPS_Request::ProcessResult*> MPS_Request::ProcessCreate (ProcessResult * processData)
{
    http_request request (methods::GET);
    request.set_request_uri (L"?format=xml&action=query&titles=Main%20Page&prop=revisions&rvprop=content");

    http_client client (L"http://en.wikipedia.org/w/api.php");

    // Make the request and asynchronously process the response
    return client.request (request).then ([processData](http_response response)
    {
        // Grab the status code
        processData->statusCodeTSG = response.status_code ();

        if (processData->statusCodeTSG == HTTP_RET_OK)
        {
            // Read the stream into a string
            auto bodyStream = response.body ();
            container_buffer<string> stringBuffer;

            bodyStream.read_to_end (stringBuffer).then ([stringBuffer, processData](size_t bytesRead)
            {
                // Allocate and copy
                const string &line = stringBuffer.collection ();
                const char * output = line.c_str ();

                processData->resultStreamTSG = new char [strlen (output) + 1];
                strcpy_s (processData->resultStreamTSG, (strlen (output) + 1), output);
            })
            .wait ();
        }

        return processData;
    });
}

      

+1


source


I ended up using swprintf

what was correctly suggested by @ CharlieScott-Skinner in his thoughtful answer. The trick here is using a formatting option %I64u

to ensure correct handling __int64

.

As a general comment, please note that this is more of a C-style approach to solving this problem than using C ++ classes string

. That being said, classes string

were created to make our life easier, and if there are times when they don't help, then we should be able to try other things.



__int64 queryID = 9223372036854775807;                    // largest supported value
wchar_t query_buffer[30];                                 // give enough space for largest
swprintf(query_buffer, 30, L"%I64u", queryID);            //     possible value
web::json::value result = RestDownload(query_buffer);     // call helper function

// my helper method returns JSON which is then processed elsewhere
pplx::task<json::value> RestDownload(const wchar_t* queryID) {
    http_client client(U("http://localhost:8080/MyJavaWebApp"));
    uri_builder builder(U("/getContent"));
    builder.append_query(U("queryID"), queryID);          // this is OK

    return client.request(methods::GET, builder.to_string()).then([](http_response response) {
        if (response.status_code() == status_codes::OK) {
            return response.extract_json();
        }

        // Handle error cases, for now return empty json value... 
        return pplx::task_from_result(json::value());
    });
}

      

0


source







All Articles