Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How convert int to char, please help me!!!

Status
Not open for further replies.

hermesax

Programmer
May 7, 2002
1
PE
Hi!..

I have a problem!...
I need to convert integers to chars,but i`m using char(int)....
So, only accept until ascii code 128..

Please, What can I do?

This is my code:

Thanks!

public Convert() {
}
public static void main(String[] args) {
Convert convert1 = new Convert();

System.out.print(f_Transforma("ABCD"));

}
public static String f_Transforma(String Password)
{
int IntStrLong=Password.length();
String StrResultado=new String();
while (IntStrLong >=1)
{
int tmpchar=(int)Password.charAt(IntStrLong-1); //valor ascii
tmpchar=256-tmpchar+IntStrLong;
String aChar = new Character((char)tmpchar).toString(); //ASCII code to String
StrResultado=StrResultado+aChar;
IntStrLong-=1;
}
return (StrResultado);
}
 
Dang! that's some really ugly code.

- use indentation for nesting
- local variables usually start in lowercase
- most people work forward(++) not backwards =-
- use capitalization rather than underscore in method names

if you want the ascii value of a char in a String...

String s = "candy";
int asciiValueofC = (int)s.charAt(0);
int asciiValueofA = (int)s.charAt(1);
int asciiValueofN = (int)s.charAt(2);
int asciiValueofD = (int)s.charAt(3);
int asciiValueofY = (int)s.charAt(4);
StringBuffer sb = new StringBuffer();
sb.append(asciiValueofC);
sb.append(asciiValueofA);
sb.append(asciiValueofN);
sb.append(asciiValueofD);
sb.append(asciiValueofY);
String candy = sb.toString();
System.out.println("I like " + candy);




 

String s = "candy";
StringBuffer sb = new StringBuffer();

for (int i = 0; i < s.length() - 1 ; i++){
sb.append((int)s.charAt(i));
}
String candy = sb.toString();
System.out.println(&quot;I like &quot; + candy);

I like this a little better
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top