黑马程序员技术交流社区

标题: java中如何将 ASC码转换成字符? [打印本页]

作者: qushuai    时间: 2013-7-22 09:46
标题: java中如何将 ASC码转换成字符?
本帖最后由 qushuai 于 2013-7-22 10:44 编辑

如题  js 中貌似可以用  String.fromCharCode()方法  Java中怎么弄,查了半天api  无解
作者: qushuai    时间: 2013-7-22 10:37
解决了。忘了强转这一说。 将ASC  码值 强转 成char  就好了。   char(ASC)
作者: tonygone    时间: 2013-7-22 10:38
题目没看明白,大小写转换吗?
  1.                 //大写转小写
  2.                 public char upp2low(char upp){
  3.                         return (char)(upp+32);
  4.                 }
  5.                 //小写转大写
  6.                 public char low2upp(char low){
  7.                         return (char)(low+32);
  8.                 }
复制代码

作者: qushuai    时间: 2013-7-22 10:41
tonygone 发表于 2013-7-22 10:38
题目没看明白,大小写转换吗?

哦  不是   是 生成 一个 随机数   然后将这些作为 ASC 码 转换成相应的字符。
long tempRandom = Math.round(Math.random()*3);
                if(tempRandom == 0)
                {
                        long Rnum = Math.round(Math.random()*9 + 48);
                          return ""+(char)Rnum;
                }
不加那个强转的  话 返回的 是一个数字。
作者: 陌路行者    时间: 2013-7-22 10:46
tonygone 发表于 2013-7-22 10:38
题目没看明白,大小写转换吗?

大小写转换用 toUpperCase  和 toLowerCase
作者: tanzhiqiu    时间: 2013-7-22 10:51

大小写转换用 toUpperCase  和 toLowerCase
作者: xwf4348    时间: 2013-7-22 11:16
强制转换(char)ASC
作者: 深圳在漂移    时间: 2013-7-22 11:24
  1. public class ASCTest { private static int ascNum; private static char strChar; public static void main(String[] args) { System.out.println(getAsc("a")); System.out.println(backchar(98)); } /** * 字符转ASC * * @param st * @return */ public static int getAsc(String st) { byte[] gc = st.getBytes(); ascNum = (int) gc[0]; return ascNum; } /** * ASC转字符 * * @param backnum * @return */ public static char backchar(int backnum) { strChar = (char) backnum; return strChar; }}
复制代码
ASC码转字符
作者: 王丽达    时间: 2013-7-22 12:08
  1. class  ASCTest
  2. {
  3.                 private static char getChar;
  4.                 public static void main(String[] args)
  5.                 {
  6.                         //打印传入的ASC
  7.                         System.out.println(backChar(77));
  8.                 }
  9.         //定义转换ASC的方法
  10.         public static char backChar(int ascnum)
  11.         {
  12.                 //将ASC强转成char
  13.                 getChar = (char)ascnum;
  14.                 return getChar;
  15.         }
  16. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2