Get int from boost::gregorian::date_duration - boost

I have date_duration variable and i want to convert it to int, I found the same topic here, about how to convert for output by <<, but i don't want it. I have to use integer value for arithmetic operations. How to convert it to string or integer? I can write it to file and then fscanf but it's idiot method.

You can use the days() method to get the number of days (as a value, not an object).

Related

Float with 'unnecessary' digits

I need to make a number out of the string. to do that i use well known maneuver looking something like this:
Float(string_var) rescue nil
This works nicely, however I do have a tiny, little problem. If a string is "2.50", variable I get is 2.5. Is it even possible to create Float object with 'unnecessary' 0 digit at the end? can I literally translate "2.50" into 2.50 ?
In short, the answer is no, given the question, as any Float, when examined, will use Float's to_s function, eliciting an answer without trailing zeroes.
Float will always give you a numeric value that can be interpreted any way you wish, though. In your example, you will get a float value (given a string that is a parsable float). What you are asking then, is how to display that value with trailing zeroes. To do that, you will be turning the float value back into a string.
Easiest way to accomplish that is to use the format given by one of your respondents, namely
string_var = "2.50"
float_value = Float(string_var) rescue nil # 2.5
with_trailing_zeroes = "%0.2f" % float_value # '2.50'

Does random_string function return unique string?

I'm using random_string('alnum', 10) function from String Helper available in Codeigniter 3.0. Does the string returned by function each time remain unique as well? If not then what can I use?
No It does not unique.
As Example
random_string('alnum', 1);//if you run this more than 63 you will get minimum one duplicate.
For unique you can use
random_string('unique');
See the Full documentaion
As the name suggests the string is random. That means with a certain (potentially small) probability this function will return the same string twice.
One way to make create a unique string is to have a counter variable which is not a string but a number. Every time you need a new unique string, you increment the counter and then convert the number to a string.

Boost Array - conversion to BYTE

So i have this: boost::array data_;
How do i convert it to normal BYTE/Char buffer or how do i print the data inside without converting it , using printf?
How can i compare it with other normal chracter buffer for example "hello".
It will be also very helpfull to know how does boost::array work, (i am creating boost async.tcp server).
I have tried some things but i was unable to print the characters inside the buffer, i'm new to boost.
I could not find much documentation about boost.
Thank you.
The boost::array class is a parameterized type, meaning that the full type name of a variable of this type is something like boost::array<char,10> for an array containing 10 elements of type char, or boost::array<float,100> for an array containing 100 elements of type float.
If you happen to have a variable data_ of some type boost::array<T,N> where T is char, then printing out the characters in it is easy:
std::cout.write(data_.data(), data_.size());
If T is wchar, you could do
std::wcout.write(data_.data(), data_.size());
If your particular boost::array type contains some other element type T, you need to consider how you would want to print out the elements. For example, if your happy with the default stream representation of the type, you may do something like
for (auto element : _data) {
std::cout << element << "\n";
}
to print out one element per line.
You can find the documentation of the boost::array class at http://www.boost.org/doc/libs/1_53_0/doc/html/boost/array.html

Cast String in multi-dim array to Integer

I have a variable (result) that looks like this when doing YAML::dump(result):
responseHeader:
status: 0
QTime: 1
params:
wt: ruby
q: enid:(15542697739)
response:
numFound: 1
start: 0
docs:
- enid: "15542697739"
I want to do a conditional comparison on the enid like this:
if result["response"]["docs"]["enid"].to_i == num['1']['car']
where num['1']['car'] is an integer.
Whenever I attempt this, I get a TypeError thrown,
can't convert String into Integer
(TypeError)
even if I attempt
result["response"]["docs"]["enid"].to_i
or
Integer(result["response"]["docs"]["enid"])
How do I get my enid value converted to an integer so I can make this comparison?
The problem is that what's in result["response"]["docs"] is NOT a hash and you're addressing it like one. What you need in this case is result["response"]["docs"][0]["enid"]. If you want to see why, try p result["response"] to see what Ruby data structures are being used at each level. YAML can be a little misleading here even if you've been reading it a while.

VB6 Cast Expression

What is the cast expression equivalent of VB.NET's CType in Visual Basic 6?
There are a number of them depending on the type you are casting to
cint() Cast to integer
cstr() cast to string
clng() cast to long
cdbl() cast to double
cdate() cast to date
It also has implicit casting so you can do this myString=myInt
Quite a few posters seem to have misread the question, so I will try to set things straight by rephrasing the question and summarizing the correct answers given so far.
Problem
I want to cast data of one type to another type. In my VB.NET code I would use CType to do this. However, when I try to use CType in VB6, I get a "Sub or Function not defined" error. So, how can I perform casts in VB6 if CType won't work?
Solution
As you may have discovered, VB6 does not have a CType function like VB.NET does. However, the other conversion functions (those that have names beginning with C), which you may have encountered in VB.NET code, such as CInt and CStr, do exist in VB6, and you can use them to convert to and from non-object types. There is no built-in function for converting an object of one class to an object of another class. Keep in mind that VB6, unlike VB.NET, does not support inheritance. A class in VB6 can implement one or more interfaces, but it cannot inherit from another class. However, if a object's class implements more than one interface, you can use the Set statement to cast an object to one of the interfaces it supports (as Ant suggested). An extended version of Ant's code example is provided below:
Example: Casting a class to one of its supported interfaces
Dim base As BaseClass
Dim child As ChildClass 'implements BaseClass'
Set child = New ChildClass
Set base = child '"Cast" child to BaseClass'
Built-in type conversion functions in VB6
Below is a complete list of the built-in conversion functions available in VB6, taken directly from the VB6 Help file.
CBool
Returns
Boolean
Description
Convert expression to Boolean.
Range for expression argument:
Any valid string or numeric expression.
CByte
Returns
Byte
Description
Convert expression to Byte.
Range for expression argument:
0 to 255.
CCur
Returns
Currency
Description
Convert expression to Currency.
Range for expression argument:
-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
CDate
Returns
Date
Description
Convert expression to Date.
Range for expression argument:
Any valid date expression.
CDbl
Returns
Double
Description
Convert expression to Double.
Range for expression argument:
-1.79769313486232E308 to
-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
CDec
Returns
Decimal
Description
Convert expression to Decimal.
Range for expression argument:
+/-79,228,162,514,264,337,593,543,950,335 for zero-scaled numbers, that is, numbers with no decimal places. For numbers with 28 decimal places, the range is
+/-7.9228162514264337593543950335. The smallest possible non-zero number is 0.0000000000000000000000000001.
CInt
Returns
Integer
Description
Convert expression to Long.
Range for expression argument:
-32,768 to 32,767; fractions are rounded.
CLng
Returns
Long
Description
Convert expression to Long.
Range for expression argument:
-2,147,483,648 to 2,147,483,647; fractions are rounded.
CSng
Returns
Single
Description
Convert expression to Single.
Range for expression argument:
-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
CStr
Returns
String
Description
Convert expression to String.
Range for expression argument:
Returns for CStr depend on the expression argument.
CVar
Returns
Variant
Description
Convert expression to Variant.
Range for expression argument:
Same range as Double for numerics. Same range as String for non-numerics.
Let's say you have an object of ChildClass (child) that you want to cast to BaseClass. You do this:
Dim base As BaseClass
Set base = child
Because of the way VB6 handles compile-time type safety, you can just do that without any extra syntax.
Note: Given that everyone else seems to have mentioned CType, I may just have misunderstood the question completely, and I apologise if that's the case!
The casts already mentioned are correct, but if the type is an Object then you have to use "Set" in VB6, such as:
If IsObject(Value) Then
Set myObject = Value ' VB6 does not have CType(Value, MyObjectType)
Else
myObject = Value ' VB6 does not have CType(Value, MyObjectType)
End If
That, of course, depends on the type you are casting to. Almost all user classes are objects as well as Collection, Dictionary, and many others. The built-in types such as long, integer, boolean, etc. are obviously not objects.
Ctype() I believe. The C* (CDate(), CStr(), etc) are holdovers for the most part.
Conversions are not "casts" at all. For example try:
MsgBox CLng(CBool(3&))
The result is -1, not 3. This is because those are conversion functions, not casts. Language is important!

Resources