0

I got some JSON value from swagger. I need to convert those values in my function accepted formate.

mt4 function:

JournalRequest(const int mode,const __time32_t from,const __time32_t to,LPCSTR filter,int *total)

for converting date I just typecast (__time32_t) and it all works. But when I try to convert filter value from string to LPCSTR it only returns the first character value. I got stuck. any help, please?

I am new to StackOverflow so forgive me if the question is not cleared.

Here is my code:

            int total;
             ServerLog* records;
             web::json::value jTrades;
             utility::string_t from, to, filter, mode;

             std::wistringstream ss;
             web::json::value jRecords;
             time_t _from = 0, _to = 0;
             int _mode;
             int k = 0;
             int year = 0, month = 0, day = 0, hour = 0, min = 0;

             mode = params[U("mode")];
             to = params[U("to")];
             from = params[U("from")];
             filter = params[U("filter")];

            std::string fromStringT(filter.begin(), filter.end());
        

         loginfo << "Input From: " << _from << " To:" << _to << " Filter:"  << filter << endl;

         records = man->JournalRequest(_mode, (__time32_t)_from, (__time32_t)_to, filter, &total);
  • 3
    you posted similar question before and were asked to provide a [mcve]. If you post the question again with missing details it is likely to be closed again. – 463035818_is_not_an_ai Aug 19 '22 at 08:52
  • please post a [mcve] – 463035818_is_not_an_ai Aug 19 '22 at 08:53
  • @463035818_is_not_a_number what do you mean by details?. Am i post my full code here? – Mohammad Soykot Aug 19 '22 at 08:54
  • read the link: [mcve]. A minimal reproducible example is **not** your full code. Its a small example that is enough code to demonstrate your problem but not more – 463035818_is_not_an_ai Aug 19 '22 at 08:55
  • the problem with only first character is somewhere in your code. We cannot know what it is unless you show it – 463035818_is_not_an_ai Aug 19 '22 at 08:57
  • @MohammadSoykot The advice from the previous question still holds. If you are trying to do this with a cast then you are doing the wrong thing. Unfortunately what the right thing to do is hard to say from the information posted. – john Aug 19 '22 at 08:58
  • @MohammadSoykot To me this seems like a conflict between wide characters and narrow characters, but I'm really only guessing. More details about your code are needed. – john Aug 19 '22 at 09:00
  • My crystal ball says you have a wide string and you're trying to cast it to a narrow string which isn't going to work. How to perform that conversion correctly depends on the source encoding and desired target encoding. – Retired Ninja Aug 19 '22 at 09:01
  • @MohammadSoykot What you need to do to get this question answered is write a new program. First line of this program declares a string in the same way as you have from swagger, second line of the program tries to do the conversion you are doing, third and fourth lines of the program print out the two strings. That's it, this is a [mre], post that program and ask how to do the conversion properly. – john Aug 19 '22 at 09:05
  • I Update the question with code – Mohammad Soykot Aug 19 '22 at 09:06
  • filter data type is class std::basic_string,class std::allocator > I try to convert it in LPCSTR – Mohammad Soykot Aug 19 '22 at 09:07
  • @MohammadSoykot So it is a wide string to narrow string conversion problem. [This](https://stackoverflow.com/questions/34932894/c-conversion-from-utilitystring-t-to-stdstring-crashes-on-return) might help – john Aug 19 '22 at 09:09
  • `std::string fromStringT(filter.begin(), filter.end()); LPCSTR _filter = (LPCSTR)filter.c_str(); ` I try this but still only get the first character @john – Mohammad Soykot Aug 19 '22 at 09:18
  • @MohammadSoykot You might have more luck with this `std::string fromStringT(filter.begin(), filter.end()); LPCSTR _filter = fromStringT.c_str();` – john Aug 19 '22 at 09:28
  • @MohammadSoykot And please don't use casts they are **never** the solution to this problem. – john Aug 19 '22 at 09:29
  • without cast how i convert my string response to LPCSTR formate @john – Mohammad Soykot Aug 19 '22 at 09:32
  • @MohammadSoykot LPCSTR is the same as const char*. There is no need to convert. – john Aug 19 '22 at 09:35
  • @MohammadSoykot When you use casts it just **hides** problems (as you have found). Avoid them if you can. – john Aug 19 '22 at 09:36
  • if I use the direct string value function gives me an error no suitable function from utility string_t to LPCSTR @john – Mohammad Soykot Aug 19 '22 at 09:46
  • @MohammadSoykot That is because you are using`filter.c_str()` when you should be using `fromStringT.c_str()` (as I already said). – john Aug 19 '22 at 10:07
  • @MohammadSoykot You have a `std::wstring`, you need to do the following two conversions `std::wstring` -> `std::string` -> `LPCSTR`. The first conversion can be done in several ways, your code `std::string fromStringT(filter.begin(), filter.end());` is one way to do that (not ideal but it works). The second conversion can be done by just calling `.c_str()` **on the result of the first conversion** not on the original wide string. – john Aug 19 '22 at 10:10
  • i try your first & second both methods. but my function gives an error error no suitable function from utility string_t to LPCSTR @john – Mohammad Soykot Aug 19 '22 at 10:19
  • `fromStringT.c_str()` cannot produce the error message you said, because `fromStringT.c_str()` has type `const char*` not type `utility::string_t`. So what you are saying doesn't make any sense to me. You must be making some other mistake somewhere else. – john Aug 19 '22 at 10:24
  • @MohammadSoykot If you still want to pursue this then post the code that you say produces the error `no suitable function from utility string_t to LPCSTR` and I'll see if I can see the mistake. – john Aug 19 '22 at 10:33
  • where i post the code @john – Mohammad Soykot Aug 19 '22 at 10:41
  • @MohammadSoykot Edit the question. I don't mean post all your code, just the three or four lines where you are trying to do the conversion. – john Aug 19 '22 at 10:44
  • @john I edit the code. Error occurs at last line – Mohammad Soykot Aug 19 '22 at 10:50
  • @MohammadSoykot The last line should be `records = man->JournalRequest(_mode, (__time32_t)_from, (__time32_t)_to, fromStringT.c_str(), &total);` – john Aug 19 '22 at 10:55
  • @MohammadSoykot I'm not sure why you put `filter` in the last line, because that is the string that you **haven't** converted. The string you have converted is `fromStringT`. Then you need to convert that string to LPCSTR by adding `.c_str()`. – john Aug 19 '22 at 10:57

1 Answers1

0

This should do:

std::string s{ filter };

It will copy the string. If you simply want a better wrapper around the pointer, and not to copy the buffer, you may do:

std::string_view s{ filter };
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93