请教关于getBytes()方法的使用

loning_sp 2004-03-16 06:06:08
小弟在用getBytes()方法时出现以下问题:

出现:

Note: E:\SGIP协议1.2版\spApi\Bind.java uses or overrides a deprecated API.
的提示,但又可以编译成功.


我加参数 -deprecation 编译就出现:

Bind.java:43: warning: getBytes(int,int,byte[],int) in java.lang.String has been deprecated
s1.getBytes(0, s1.length()-1, bodybytes, 17);
^
的错误.

请问哪位老大知不知道怎么样才能解决这个问题????

在程序中的代码是:

public String s;
bodybytes[0] = (byte)i;
s.getBytes(0, s.length(), bodybytes, 1);
...全文
556 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
laughsmile 2004-03-16
  • 打赏
  • 举报
回复
或者
public String s;
try{
byte[] bodybytes = s.getBytes("UTF-8");
} catch (Exception e){
System.out.println(e);
}
aaronyes 2004-03-16
  • 打赏
  • 举报
回复
UP
icy_csdn 2004-03-16
  • 打赏
  • 举报
回复
这么写
public String s;
byte[] bodybytes = s.getBytes();
fohoo 2004-03-16
  • 打赏
  • 举报
回复
public String s;
bodybytes[] = s.getBytes();

注意getBytes返回的byte[]使用系统默认字符集

如果通讯时,注意通讯平台的字符集匹配问题
simonhappy 2004-03-16
  • 打赏
  • 举报
回复
老兄,看看JDK的帮助。
@deprecated This method does not properly convert characters into bytes.
* As of JDK 1.1, the preferred way to do this is via the
* the <code>getBytes()</code> method, which uses the platform's default
* charset.

-deprecation 的开关是对于deprecated的方法是否认为是错误。
loning_sp 2004-03-16
  • 打赏
  • 举报
回复
请问要是按最新的方法写这个程序应该如何写?
kingfish 2004-03-16
  • 打赏
  • 举报
回复
不是错误,只是告诉你这个函数在新的JDK下已经被deprecated (也就是淘汰的意思)
用当然也可以继续用

Deprecated. This method does not properly convert characters into bytes. As of JDK 1.1, the preferred way to do this is via the the getBytes() method, which uses the platform's default charset.

62,614

社区成员

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

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