answersLogoWhite

0

Assuming - char mychar ; and int myint have been properly declared,

myint = (int) mychar; // converts

This is a feature of Java= to change types, put the type you want to convert into in parenthes before the variable that stores the converted value.

^Will convert your char into an int, but it will be the ascii value.

For example,

mychar = 3;

myint = (int) mychar; //returns 51

To make an accurate one that will not return an error, you can use a try-catch statement

Example

char mychar = '3';

try

{

int myint = (int) mychar;

}catch(NumberFormatException e)

{

//Do whatever you want with it!

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

How do you calculate ascii value in java?

char a = 'A'; System.out.println((int)a);


Why char consumes 2 bytes in java and int consumes 4 bytes in java?

in java, char consumes two bytes because it uses unicode instead of ascii. and int takes 4 bytes because 32-bit no will be taken


How do we convert between an int to a char?

With an explicit cast, for example (in Java): int i = 0; char c; c = (char) i; Please note that data may be lost in such a conversion; the explicit cast basically tells the compiler "go ahead; I know what I am doing". Without an explicit cast, the compiler won't accept the conversion.


How do you convert string to int in Java?

One can convert a string variable to an int variable in Java using the parse integer command. The syntax is int foo = Integer.parseInt("1234"). This line will convert the string in the parenthesis into an integer.


How can someone convert string to int in Java?

To convert string to int in Java, the easiest way is to simply use the method Integer.parseInt(). For more information how to do this, refer to the integer class documents.


How can you insert an integer value to a character array in java?

It depends on what you mean by "convert an int to char". If you simply want to cast the value in the int, you can cast it using Java's typecast notation: int i = 97; // 97 is 'a' in ASCII char c = (char) i; // c is now 'a' If you mean transforming the integer 1 into the character '1', you can do it like this: if (i >= 0 && i <= 9) { char c = Character.forDigit(i, 10); .... }


How do you convert a char to and int in c plus plus but keep the value of the char so if the char was 3 the int would also be 3?

Formally, you cannot do this. A char containing a character value has a different bit configuration than an int. Attempting to convert between the two is non-portable. However, in the ASCII character set, the character '0' has the value 48, or 0x30. If you subtract 48 from the char you will get the int value, but only if the char was one of the digits '0' through '9'. It is better to use the library routines to convert, such as atoi and sscanf, because they will give you predictable, portable results.


What are the only types in Java that are not classes?

The non-class Java data types are primitives: * byte * short * int * long * float * double * boolean * char


Eight basic data types used in java?

char, boolean, byte, short, int, long, double, or float


What is earlier declaration for main?

pick one: int main (void); int main (int argc, char **argv); int main (int argc, char **argv, char **envp);


Why java not use int as a generic argument?

Only Objects can be used as generic arguments. Primitives Data Types (int, long, char ...) are not objects. But you can use Integer, an Object that wrap the data type int instead.


Types of main function in C programming?

minimalist: int main (void); standard: int main (int argc, char **argv); unix-only: int main (int argc, char **argv, char **envp);