Questions tagged [type-conversion]

Type conversion is the way of implicitly or explicitly changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations.

Wikipedia:

In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations. One example would be small integers, which can be stored in a compact format and converted to a larger representation when used in arithmetic computations. In object-oriented programming, type conversion allows programs to treat objects of one type as one of their ancestor types to simplify interacting with them.

Each programming language has its own rules on how types can be converted. In general, both objects and fundamental data types can be converted. In most languages, the word coercion is used to denote an implicit conversion, either during compilation or during run time. A typical example would be an expression mixing integer and floating point numbers (like 5 + 0.1), where the integers are normally converted into the latter. Explicit type conversions can either be performed via built-in routines (or a special syntax) or via separately defined conversion routines such as an overloaded object constructor.

11914 questions
3788
votes
41 answers

Create ArrayList from array

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList arraylist = ???;
Ron Tuffin
  • 49,960
  • 23
  • 62
  • 76
3213
votes
49 answers

How do I convert a String to an 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.
Unknown user
  • 40,827
  • 16
  • 36
  • 40
2458
votes
28 answers

How do I parse a string to a float or int?

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 545.2222? Or parse the string "31" to an integer, 31? I just want to know how to parse a float str to a float, and (separately) an int str to an int.
Tristan Havelick
  • 58,645
  • 19
  • 52
  • 64
1761
votes
29 answers

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…
Nemo
  • 20,986
  • 9
  • 41
  • 56
1751
votes
35 answers

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

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…
Daniel Goldberg
  • 18,355
  • 4
  • 19
  • 28
1436
votes
18 answers

How do I convert two lists into a dictionary?

Imagine that you have the following list. keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way to produce the following dictionary? a_dict = {'name': 'Monty', 'age': 42, 'food': 'spam'}
Guido
  • 42,125
  • 24
  • 113
  • 167
1001
votes
15 answers

How to convert UTF-8 byte[] to string?

I have a byte[] array that is loaded from a file that I happen to known contains UTF-8. In some debugging code, I need to convert it to a string. Is there a one liner that will do this? Under the covers it should be just an allocation and a…
BCS
  • 67,242
  • 64
  • 175
  • 277
911
votes
5 answers

How do I convert a String to an InputStream in Java?

Given a string: String exampleString = "example"; How do I convert it to an InputStream?
Iain
  • 9,533
  • 16
  • 52
  • 58
869
votes
12 answers

How to convert a char to a String?

I have a char and I need a String. How do I convert from one to the other?
Landon Kuhn
  • 61,957
  • 42
  • 100
  • 130
766
votes
35 answers

Converting a string to a date in JavaScript

How can I convert a string to a date in JavaScript? var st = "date in some format" var dt = new date(); var dt_st= //st in date format same as dt
jslearner
  • 18,191
  • 17
  • 35
  • 35
755
votes
20 answers

How do I convert from int to String?

I'm working on a project where all conversions from int to String are done like this: int i = 5; String strI = "" + i; I'm not familiar with Java. Is this usual practice or is something wrong, as I suppose?
Denis Palnitsky
  • 17,589
  • 13
  • 44
  • 55
754
votes
30 answers

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' …
Sara
  • 12,140
  • 12
  • 32
  • 49
718
votes
14 answers

How to convert a Decimal to a Double in C#?

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 build the application, it gives the following error: Cannot implicitly convert type decimal to double I…
Eggs McLaren
  • 1,787
  • 3
  • 12
  • 13
703
votes
32 answers

How can I convert String to Int?

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database. How can I do this?
turki2009
  • 7,099
  • 2
  • 16
  • 7
612
votes
8 answers

Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?

I was practicing some JavaScript when one of my friends came across this JavaScript code: document.write(('b' + 'a' + + 'a' + 'a').toLowerCase()); The above code answers "banana"! Can anyone explain why?
HV Sharma
  • 3,989
  • 3
  • 10
  • 25
1
2 3
99 100