如何将String类型转换为ascii码或者其编码?

zl_xue 2003-03-18 01:10:23
如何将String类型转换为ascii码或者其编码,我想做密码加密!!
各位谁有好的建议?
...全文
294 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
twos 2003-03-18
  • 打赏
  • 举报
回复
byte[] b;
String a=new String(b);
你便可以得到转换后的String,这个方法简单又方便。
zl_xue 2003-03-18
  • 打赏
  • 举报
回复
这个完成了!!谢谢
散分~!
那如何将byte转换为String类型?????????????????????解决后散分
hoxisoft 2003-03-18
  • 打赏
  • 举报
回复
//转过去

char temChr ;
int ascChr ;
int i ;
String ss = "" ;
String rtStr = "地从" ;
int length = rtStr.length() ;
for ( i = 0 ; i < length ; i++ )
{
temChr = rtStr.charAt( i ) ;
ascChr = temChr + 0 ;
ss = ss + "&#x" + Integer.toHexString( ascChr ) + ";" ;

//转回来

String sArray[] = ss.split(";");
byte bArray[] = new byte[2 * sArray.length];
for (i = 0 ; i < sArray.length; i++) {
try {
int tmp = Integer.parseInt(sArray[i].substring(3), 16);
System.out.println(">>>>>int value is " + sArray[i].substring(3));
bArray[i*2 + 1] = (byte)(tmp & 0xff);
bArray[i*2 ] = (byte)((tmp >> 8) & 0xff);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
System.out.println("convert to orginal String is " + new String(bArray));
zl_xue 2003-03-18
  • 打赏
  • 举报
回复
如用String类的getBytes()方法就可以把String拆成byte,
这个函数在那个包下?
望告知!谢先
nirvana_hg 2003-03-18
  • 打赏
  • 举报
回复
不知道byte可以不可以,我觉得应该可以吧。字节就是构成文件的最低元素了。用
String类的getBytes()方法就可以把String拆成byte.然后你再对BYTE进行加密行么?
acefr 2003-03-18
  • 打赏
  • 举报
回复
String(byte[] ascii, int hibyte)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a charset name or that use the platform's default charset.

String(byte[] ascii, int hibyte, int offset, int count)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a charset name or that use the platform's default charset.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧