Read array element and convert into string - ruby

if (RARRAY_LEN(arr) > 0)
{
VALUE str = rb_ary_entry(arr, 0);
abc = some_method(*str);
}
rb_ary_entry(arr, 0) gives me an index value. Then I want to convert that value to a string so I can pass it to the next method. I tried:
rb_str_new2(rb_ary_entry(arr, 0));
but I get error saying:
error: indirection requires pointer operand `('VALUE' (aka 'unsigned long')` `invalid`)`
`ipDict = some_method(*str)`;

Use the StringValueCStr macro to convert a Ruby String into a char* (the rb_str_new functions are for converting in the other direction).
VALUE str = rb_ary_entry(arr, 0); // str is now a Ruby String
char *c_str = StringValueCStr(str);
abc = some_method(c_str);

Related

What the difference between google.protobuf.Any and google.protobuf.Value?

I want th serialize int/int64/double/float/uint32/uint64 into protobuf, which one should I use ? which one is more effective ?
For example :
message Test {
google.protobuf.Any any = 1; // solution 1
google.protobuf.Value value = 2; // solution 2
};
message Test { // solution 3
oneof Data {
uint32 int_value = 1;
double double_value = 2;
bytes string_value = 3;
...
};
};
In your case, you'd better use oneof.
You can not pack from or unpack to a built-in type, e.g. double, int32, int64, to google.protobuf.Any. Instead, you can only pack from or unpack to a message, i.e. a class derived from google::protobuf::Message.
google.protobuf.Value, in fact, is a wrapper on oneof:
message Value {
// The kind of value.
oneof kind {
// Represents a null value.
NullValue null_value = 1;
// Represents a double value.
double number_value = 2;
// Represents a string value.
string string_value = 3;
// Represents a boolean value.
bool bool_value = 4;
// Represents a structured value.
Struct struct_value = 5;
// Represents a repeated `Value`.
ListValue list_value = 6;
}
}
Also from the definition of google.protobuf.Value, you can see, that there's no int32, int64, or unint64 fields, but only a double field. IMHO (correct me, if I'm wrong), you might lose precision if the the integer is very large. Normally, google.protobuf.Value is used with google.protobuf.Struct. Check google/protobuf/struct.proto for detail.

How to convert a byte array into a string in CAPL?

I have a byte array and I need to print the elements in a single line.
I tried using 'snprintf()' but it won't take a byte array as its input parameter.
I tried copying the byte array into an integer array and then used the snprintf(), but instead of printing the HEX values, corresponding ASCII values are printed.
You can try this code :
variables
{
int ar[100];
}
on diagResponse TCCM.*
{
char tmp[8]; // Temporary buffer containing single HEX value
char out[301]; // Bigger output string and "local" to function
// Better to place them there (i and response) if they are not global
int i;
byte response[100];
out[0] = 0; // Clear output string
s1 = DiagGetPrimitiveData(this, response, elcount(response));
for (i = 0; i < s1; i++)
{
ar[i] = response[i];
snprintf(tmp, elcount(tmp), "%.2X ", response[i]); // byte to HEX convert
strncat(out, tmp, elcount(out)); // Concatenate HEX value to output string
}
write("HEX Response : %s", out);
}
Olivier

How to convert string to rapid json value

I am using rapidjson library to encode and deconde the json.
I have received the string lets say string Str = "msisdn-123456789";
i want to covert this into a rapidjson value but it says parsing error. and value type is still kNullType
I am using below code snippest.
std::string Str = "msisdn-123456789";
rapidjson::Document newDoc;
newDoc.Parse(Str.c_str());
rapidjson::Value value(rapidjson::kNullType);
value.CopyFrom(newDoc, newDoc.GetAllocator());
cout << "Type of value" << static_cast<uint32_t>(value.GetType()) << endl;
The output is kNullType (0) .
How can i convert string to rapidjson value ?
std::string Str = "msisdn-123456789"; is not a valid JSON. So parsing it will have an null value at the document root.
For constructing a Value by std::string s, just
Value v(s, allocator); // When RAPIDJSON_HAS_STDSTRING=1
Value v(s.c_str(), s.size(), allocator); // Otherwise

Decimal to hexadecimal conversion using CAPL

Is there any CAPL function for converting decimal value into hexadecimal value? I have already looked in help option of CAPL browser.
Assuming that you want the number to be converted to a string, that you can print out. Either to the write window, into the testreport, etc.
You can use snprintf like this:
snprintf(buffer,elcount(buffer),"%x",integervariable);
where buffer is a char array big enough.
This example is taken from the Vector knowledge base and was among the first result on google.
For hexadecimal equivalent value:
You can make use of _pow function (returns x to the power of y) and while in the following way, which would return you the hexadecimal equivalent value:
double decToHexEquivalent(int n)
{
int counter,remainder,decimal_number,hexadecimal_number = 0;
while(n!=0)
{
remainder = decimal_number % 16;
hexadecimal_number = hexadecimal_number + remainder * _pow(10, counter);
n=n/16;
++counter;
}
return hexadecimal_number;
}
you can call the above function in the following way:
testfunction xyz(int n)
{
write("Hexadecimal:%d", decToHexa(n));
}
Caution: not tested
For hexadecimal value
declare a global variable char buffer[100] in the variable section
variables
{
char buffer[100];
}
and then using snprintf function you can convert an integer variable to a character array, like this:
void dectohexValue(int decimal_number)
{
snprintf(buffer,elcount(buffer),"%02X",decimal_number);
}
then finally you can use the function as follows:
testfunction xyz(int n)
{
dectohexValue(n);
write("Hexadecimal:%s", buffer);
}

Why Kotlin doesn't implement Int.plus(value: String)?

It causes discomfort when you can do that:
val string = " abc "
val integer = 8
val result = string + integer
and can't do:
val result = integer + string
It has hidden meaning or it's an omission?
Kotlin is static typed language and in basicly you can't add String to Integer. But there are possible to overload operators, so we can now.
In case when we want add any object to string, it's clear: every object can be implicitly converted to String (Any#toString())
But in case of Int + smthg it's not so clear, so only Int + kotlin.Number is defined in standard library.
I suggest to use string interpolation:
val result = "${integer}${string}"
Or define own overloaded plus operator:
operator fun Int.plus(string: String): String = string + this

Resources