Solved

char and int conversions...

Posted on 1997-06-30
2
306 Views
Last Modified: 2010-04-16
I need to do the following:

I have an integer with a value, and for this example we'll use 65.

Now, I want to convert that value to the character 65 or "A".

In C, I can use:
   int temp1=65;
   char temp2;

   temp2=temp1;

and boom, I have temp2 == 'A'

However, in Java I can't do that.  I can do:
  int temp1;
  char temp2='A';
 
  temp1=temp2;

and instantly temp1=65, but I can't go the other way.  Is there a way to do this?

Thank you.
0
Comment
Question by:zathael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
webster030697 earned 200 total points
ID: 1222523
Unlike C, Java is strongly typed (thankfully). To do what you want, you must expliciltly cast the data type:

import java.io.*;

public class TestChar
{
      
      public static void main(String Args[])
      {      int theInt = 65;
            char theChar;
            theChar = (char) theInt;
            System.out.println("theInt = " + theInt + "\n");
            System.out.println("theChar = " + theChar);
      }
      
}

produces the output:

theInt = 65

theChar = A

Why you would want to do this is another question. Strong typing is one of the great features of Java - you can't just go juxtaposing data types willy-nilly, causing all sorts of confusion in the process.
0
 

Author Comment

by:zathael
ID: 1222524
The reason I needed to do this is there are certain c routines that I'm taking to Java, certain functions that I find useful in my projects that work great in C, and now I need in Java.  One such function required this.  For the most part I can work around them, but rather than add 10 lines of code to do the same thing, I thought there might be a better way to take it to one line of code. Thank you for your answer.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question