EzDevInfo.com

type-conversion interview questions

Top type-conversion frequently asked interview questions

Convert a Unicode string to a string in Python (containing extra symbols)

How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?


Source: (StackOverflow)

How to convert int to QString?

Is there a QString function which takes an int and outputs it as a QString?


Source: (StackOverflow)

Advertisements

double to long conversion

In java, what is the best way to convert a double to a long?

Just cast? or

double d = 394.000;
long l = (new Double(d)).longValue();
System.out.println("double=" + d + ", long=" + l);

Source: (StackOverflow)

Converting String to Int in Java?

How can I convert a String to an int in Java?

My String contains only numbers and I want to return the number it represents.

For example, given the string "1234" the result should be the number 1234.


Source: (StackOverflow)

How do I check if a string is a number (float) in Python?

What is the best possible way to check if a string can be represented as a number in Python?

The function I currently have right now is:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

Which, not only is ugly and slow, seems clunky. However I haven't found a better method because calling float in the main function is even worse.


Source: (StackOverflow)

Easiest way to convert int to string in C++

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

1.

int a = 10;
char *intStr = itoa(a);
string str = string(intStr);

2.

int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

Source: (StackOverflow)

How to convert a char to a string in Java?

I have a char and I need a String. How do I convert from one to the other?


Source: (StackOverflow)

When setting a form's opacity should I use a decimal or double?

I want to use a track-bar to change a form's opacity.

This is my code:

decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;

When I try to build it, I get this error:

Cannot implicitly convert type 'decimal' to 'double'.

I tried making trans a double, but then the control doesn't work. This code has worked fine for me in VB.NET in the past.


Source: (StackOverflow)

Does it make sense to use "as" instead of a cast even if there is no null check? [closed]

In development blogs, online code examples and (recently) even a book, I keep stumbling about code like this:

var y = x as T;
y.SomeMethod();

or, even worse:

(x as T).SomeMethod();

That doesn't make sense to me. If you are sure that x is of type T, you should use a direct cast: (T)x. If you are not sure, you can use as but need to check for null before performing some operation. All that the above code does is to turn a (useful) InvalidCastException into a (useless) NullReferenceException.

Am I the only one who thinks that this a blatant abuse of the as keyword? Or did I miss something obvious and the above pattern actually makes sense?


Source: (StackOverflow)

Setting a property by reflection with a string value

I'd like to set a property of an object through Reflection, with a value of type string. So, for instance, suppose I have a Ship class, with a property of Latitude, which is a double.

Here's what I'd like to do:

Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, value, null);

As is, this throws an ArgumentException:

Object of type 'System.String' cannot be converted to type 'System.Double'.

How can I convert value to the proper type, based on propertyInfo?


Source: (StackOverflow)

Java:Convert / Cast long to String ?

I am absolutely new to Java and Blackberry development. I've started learning Java and Blackberry development. I just created sample BB app, which can allow to choose the date.

DateField curDateFld = new DateField("Choose Date: ",
  System.currentTimeMillis(), DateField.DATE | DateField.FIELD_LEFT);

After choosing the date, i need to convert that long value to String, so that i can easily store the date value somewhere in database.

long date = curDateFld.getDate();

As i haven't found any link, could someone give me the link or idea how should i convert this long value to String? Also i want to convert then again back to long from String, i think for that i can use "long l = Long.parseLong("myStr");" ?


Source: (StackOverflow)

How can I convert a character to a integer in Python, and viceversa?

I want to get, given a character, its ASCII value.

For example, for the character a, I want to get 97, and vice versa.


Source: (StackOverflow)

ARC and bridged cast

With ARC, I can no longer cast CGColorRef to id. I learned that I need to do a bridged cast. According clang docs:

A bridged cast is a C-style cast annotated with one of three keywords:

(__bridge T) op casts the operand to the destination type T. If T is a retainable object pointer type, then op must have a non-retainable pointer type. If T is a non-retainable pointer type, then op must have a retainable object pointer type. Otherwise the cast is ill-formed. There is no transfer of ownership, and ARC inserts no retain operations.

(__bridge_retained T) op casts the operand, which must have retainable object pointer type, to the destination type, which must be a non-retainable pointer type. ARC retains the value, subject to the usual optimizations on local values, and the recipient is responsible for balancing that +1.

(__bridge_transfer T) op casts the operand, which must have non-retainable pointer type, to the destination type, which must be a retainable object pointer type. ARC will release the value at the end of the enclosing full-expression, subject to the usual optimizations on local values.

These casts are required in order to transfer objects in and out of ARC control; see the rationale in the section on conversion of retainable object pointers.

Using a __bridge_retained or __bridge_transfer cast purely to convince ARC to emit an unbalanced retain or release, respectively, is poor form.

In what kind of situations would I use each?

For example, CAGradientLayer has a colors property which accepts an array of CGColorRefs. My guess is that I should use __brige here, but exactly why I should (or should not) is unclear.


Source: (StackOverflow)

How can I convert string to datetime with format specification in JavaScript?

How can I convert a string to a date time object in javascript by specifying a format string?

I am looking for something like:

var dateTime = convertToDateTime("23.11.2009 12:34:56", "dd.MM.yyyy HH:mm:ss");

Source: (StackOverflow)

How do I convert a string to a number in PHP?

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP?

Input             Output
'2'               2
'2.34'            2.34
'0.3454545'       0.3454545

Source: (StackOverflow)