Integer.toString(int i) vs String.valueOf(int i) in Java

In String type we have several method valueOf static String valueOf(boolean b) static String valueOf(char c) static String valueOf(char[] data) static String valueOf(char[] data, int offset, int count) static String valueOf(double d) static String valueOf(float f) static String valueOf(int i) static String valueOf(long l) static String valueOf(Object obj) As we can see those method are … Read more

How to convert int to string in C++

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string. #include <string> std::string s = std::to_string(42); is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword: auto s = std::to_string(42); … Read more

Does one double promote every int in the equation to double?

I purposefully did not compile and run then on my system, since this is the type of thing that could be compiler dependent. This is not compiler dependent. C++ clearly defines the order of these operations and how they are converted. How the conversion happens is dependent on the order of operations. double result1 = … Read more

WPF – converting Bitmap to ImageSource

For others, this works: //If you get ‘dllimport unknown’-, then add ‘using System.Runtime.InteropServices;’ [DllImport(“gdi32.dll”, EntryPoint = “DeleteObject”)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeleteObject([In] IntPtr hObject); public ImageSource ImageSourceFromBitmap(Bitmap bmp) { var handle = bmp.GetHbitmap(); try { return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(handle); } }

narrowing conversion from unsigned to double

Why is this a narrowing conversion? Because the definition includes (with my emphasis): C++11 8.5.4/7 A narrowing conversion is an implicit conversion […] from an integer type […] to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce … Read more

Convert Java Map to Scala Map

Edit: the recommended way is now to use JavaConverters and the .asScala method: import scala.collection.JavaConverters._ val myScalaMap = myJavaMap.asScala.mapValues(_.asScala.toSet) This has the advantage of not using magical implicit conversions but explicit calls to .asScala, while staying clean and consise. The original answer with JavaConversions: You can use scala.collection.JavaConversions to implicitly convert between Java and Scala: … Read more

Converting from byte to int in Java

Your array is of byte primitives, but you’re trying to call a method on them. You don’t need to do anything explicit to convert a byte to an int, just: int i=rno[0]; …since it’s not a downcast. Note that the default behavior of byte-to-int conversion is to preserve the sign of the value (remember byte … Read more

tech