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.

13523 questions
4073
votes
42 answers

Create ArrayList from array

Given an array of type Element[]: Element[] array = {new Element(1), new Element(2), new Element(3)}; How do I convert this array into an object of type ArrayList? ArrayList arrayList = ???;
Ron Tuffin
  • 53,859
  • 24
  • 66
  • 78
3479
votes
30 answers

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

How can I convert a String to an int? "1234" → 1234
Unknown user
  • 44,551
  • 16
  • 38
  • 42
2709
votes
33 answers

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

How can I convert a str to float? "545.2222" → 545.2222 How can I convert a str to int? "31" → 31 For the reverse, see Convert integer to string in Python and Converting a float to a string without rounding it. Please instead use…
Tristan Havelick
  • 67,400
  • 20
  • 54
  • 64
2096
votes
31 answers

How to convert int to string in C++?

How can I convert from int to the equivalent string in C++? I am aware of two methods. Is there another 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();
Nemo
  • 24,540
  • 12
  • 45
  • 61
1947
votes
41 answers

How do I check if a string represents a number (float or int)?

How do I check if a string represents a numeric value in Python? def is_number(s): try: float(s) return True except ValueError: return False The above works, but it seems clunky. If what you are testing comes from…
Daniel Goldberg
  • 19,908
  • 4
  • 21
  • 29
1135
votes
16 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 memcopy,…
BCS
  • 75,627
  • 68
  • 187
  • 294
1011
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
  • 10,433
  • 16
  • 56
  • 62
937
votes
13 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
  • 76,451
  • 45
  • 104
  • 130
916
votes
35 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
  • 14,098
  • 13
  • 34
  • 50
807
votes
31 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
  • 8,139
  • 2
  • 19
  • 7
800
votes
13 answers

How to convert Decimal to Double in C#?

I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal trans = trackBar1.Value / 5000; this.Opacity = trans; When I build the app it gives the following error: Cannot implicitly convert type decimal to double
Eggs McLaren
  • 1,777
  • 3
  • 12
  • 13
780
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
  • 18,267
  • 14
  • 46
  • 55
767
votes
17 answers

Converting a String to DateTime

How do you convert a string such as 2009-05-08 14:40:52,531 into a DateTime?
dban
664
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
  • 4,891
  • 3
  • 15
  • 30
616
votes
15 answers

Converting an integer to a string in PHP

Is there a way to convert an integer to a string in PHP?
kman99
  • 7,315
  • 4
  • 24
  • 20
1
2 3
99 100