I want to create a JSON node from a string literal in rapidjson, my code is as follows(which doesn't work of cause):
inline rapidjson::Value to_json(const std::string& myStr) {
auto result = rapidjson::Value(rapidjson::kStringType);
result.SetString(myStr);
return result;
}
I remember that Jackson's APIs are so nice that you can create a String node by TextNode.valueOf(myStr).
Is there a similar way to create a JSON node from string literal ?
You will need an allocator. And if you have defined RAPIDJSON_HAS_STDSTRING=1, you can simply:
Document d;
std::string s = "...";
Value v(s, d.GetAllocator());
Related
I need to create function to convert byte3 to string.
On my contract, data is saved into a state variable.
I want to convert content of variable for user.
This is my function:
function convertByteToString() public view returns(string memory){
string memory result = string(symbol);
return result;
}
but I get a compiler error:
TypeError: Explicit type conversion not allowed from "bytes3" to "string memory".
How can this error be resolved?
To convert bytes3 to string you must use the abi.encodePacked(bytes3 parameter) and this result you must convert it into a string.
Change your function with this:
function convertByteToString(bytes3 symbol) public view returns(string memory){
string memory result = string(abi.encodePacked(symbol));
return result;
}
Only a dynamic-length byte array (Solidity type bytes) can be typecasted to a string, but you're passing a fixed-length byte array (Solidity type bytes3).
Use abi.encode() to convert the bytes3 to bytes.
pragma solidity ^0.8;
contract MyContract {
bytes3 symbol = 0x455448; // hex-encoded ASCII value of "ETH"
function convertByteToString() public view returns(string memory){
string memory result = string(abi.encode(symbol));
return result;
}
}
I'm currently working on node addon, is there an easy way to convert string array parameter from node and use it to c++ like a simple c++ array? I am not that knowledgeable in v8. I just wanna use node addon for my modules to gain more speed.
what I tried is:
//this wont work
void getArray(const FunctionCallbackInfo<Value>& args){
Isolate * isolate = args.GetIsolate();
string c_array = args[0];
for(int x=0; x<5; x++){
//iterate c_array here
}
}
Question I have is I am trying to strip out all "%" of a string called "workorder" and for some reason it's not working any help would be very much appreciated!
example:
String ^ workorder = "%QW1234%12%3"
with the below code I want it to spit out the workorder string like so = "QW1234123"
HERE IS MY CODE
private: System::Void workorder_text_TextChanged(System::Object^ sender, System::EventArgs^ e) {
String ^ workorder;
workorder = workorder_text->Text;
//I CANT USE WORKORDER STRING FOR wO string for some reason....
string wO(workorder);
char bad_chars_wo[] = "%";
for (unsigned int i = 0; i < strlen(bad_chars_wo); ++i)
{
wO.erase (std::remove(wO.begin(), wO.end(), bad_chars_wo[i]), wO.end());
}
}
Do you have a real need to mix System::String and std::string objects here (as in, mix CLI strings and C++ strings)?
The simplest solution to your problem is to use the methods provided by System::String:
auto workorder = workorder_text->Text;
workorder = workorder->Replace("%", String::Empty);
If you really need a std::string for later processing, you can marshal the System::String:
#include <msclr/marshal_cppstd.h>
auto wO = msclr::interop::marshal_as<std::string>(workorder);
See the docs here.
So far I've only written console applications. My first application using MFC (in Visual Studio 2010) is basically a form with two multiline boxes (using String[] arrays noted with String^) and a button to activate text processing. It should search the String^ for a [, look for the ] behind it and delete all characters between them (including the []). With 'normal' C++ strings, this isn't difficult. String^ however is more like an object and MSDN tells me to make use of the Remove method. So, I tried to implement it.
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
String^ DestroyCoords(String^ phrase)
{
int CoordsStart = 0;
int CoordsEnd = 0;
int CharCount = 0;
for each (Char ch in phrase)
{
if (ch == '[')
CoordsStart = CharCount;
if (ch == ']')
{
CoordsEnd = CharCount;
//CoordsEnd = phrase->IndexOf(ch);
phrase->Remove( CoordsStart , CoordsEnd-CoordsStart );
}
CharCount++;
}
return phrase;
}
The button using the method:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
TempString = String::Copy(BoxInput->Text);
DestroyCoords(TempString);
BoxOutput->Text = TempString;
The function seems to hit the correct places at the correct time, but the phrase->Remove() method is doing absolutely nothing..
I'm no OO hero (as said, I normally only build console applications), so it's probably a rookie mistake. What am I doing wrong?
In C++/CLI, System::String is immutable, so Remove creates a new String^. This means you'll need to assign the results:
phrase = phrase->Remove( CoordsStart , CoordsEnd-CoordsStart );
The same is true in your usage:
TempString = DestroyCoords(TempString);
BoxOutput->Text = TempString;
Note that this will still not work, as you'd need to iterate through your string in reverse (as the index will be wrong after the first removal).
No MFC here, that's the C++/CLI that Microsoft uses for writing .NET programs in C++.
The .NET System::String class is immutable, so any operations you expect to modify the string actually return a new string with the adjustment made.
A further problem is that you're trying to modify a container (the string) while iterating through it. Instead of using Remove, have a StringBuilder variable and copy across the parts of the string you want to keep. This means only a single copy and will be far faster than repeated calls to Remove each of which makes a copy. And it won't interfere with iteration.
Here's the right approach:
int BracketDepth = 0;
StringBuilder sb(phrase->Length); // using stack semantics
// preallocated to size of input string
for each (Char ch in phrase)
{
if (ch == '[') { // now we're handling nested brackets
++BracketDepth;
}
else if (ch == ']') { // and complaining if there are too many closing brackets
if (!BracketDepth--) throw gcnew Exception();
}
else if (!BracketDepth) { // keep what's not brackets or inside brackets
sb.Append(ch);
}
}
if (BracketDepth) throw gcnew Exception(); // not enough closing brackets
return sb.ToString();
It seems AS3 has a toString() for the Number class. Is there an equivalent in Haxe? The only solution I could come up with for converting an Int to a String is a function like:
public function IntToString(i:Int):String {
var strbuf:StringBuf = new StringBuf();
strbuf.add(i);
return strbuf.toString();
}
Is there a better method that I'm overlooking?
You don't usually need to manually convert an int to a string because the conversion is automatic.
var i = 1;
var s = "" + i; // s is now "1"
The "formal" way to convert any value to a string is to use Std.string():
var s = Std.string(i);
You could also use string interpolation:
var s = '$i';
The function your wrote is fine but definitely overkilling.