请教java中String和Byte转换问题

likaiyihou512 2012-05-04 05:28:34

/**
* 将字符串按字节位数截取
* @param strValue 输入字符串
* @param a 截取起始位数
* @param b 截取结束位数
* @return value
* @throws java.lang.Exception
*/
public String subStringByte(String strValue,int a,int b) throws Exception{
String value=null;
byte[] bytes = new byte[2048];
System.out.println(bytes.length);
bytes = strValue.getBytes();
System.out.println(bytes.length);
byte[] str = new byte[b-a];//要截取的字节码
int index = 0;
for(int i=a;i<b;i++) {
str[index++] = bytes[i] ;//获取b-a的字节码
}
value = new String(str);//转化为string
return value;
}

开始bytes长度是2048,strValue.getBytes()之后怎么变成1024了,我输入的strValue参数很长时,需要截取的参数,a或b超过1024就数组越界,求各位解释下
...全文
326 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
likaiyihou512 2012-05-04
  • 打赏
  • 举报
回复
各位大虾 给点意见,晚上来结贴,我把我的问题说下,我有个很长的字符串,包括中英文。要按byte字节位数 解析成字符串。我的意思是先 将字符串 用getBytes 转换为byte[],然后截取 位数,再转化为字符串,现在的问题是,getBytes 转换为byte[]的数组长度只支持 1024,多余的给丢弃了,我要截取 1024位以后的 就异常,现在不知道 该怎么解决,各位大虾给个意见。
sdojqy1122 2012-05-04
  • 打赏
  • 举报
回复
API 有个方法,但是过时了。。。

strValue.getBytes(0, strValue.length(), bytes, 0);
sdojqy1122 2012-05-04
  • 打赏
  • 举报
回复
楼主copy一份就可以了。。。
likaiyihou512 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

我试了没问题啊。
Java code

public static void main(String[] args) throws Exception {
StringBuffer sb = new StringBuffer();
for(int i = 0;i < 9999;i++) {
sb.append("艹x");
……
[/Quote]
System.out.println(subStringByte(sb.toString(),10,20));这里10,20改成1000,1030,就不行了,现在纠结这个问题
likaiyihou512 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

byte[] bytes = new byte[2048];
bytes = strValue.getBytes(); 返回是是一个新的字节数组,也就是说bytes指向的引用变了。不在指向new byte[2048];
至于strValue.getBytes();是多大的字节数组,要去翻源码了。你也可以直接打印出他的长度是多少,看看结果。
[/Quote]
长度是1024,现在我需要bytes长度大点,需要自己去重写吗
  • 打赏
  • 举报
回复
我试了没问题啊。

public static void main(String[] args) throws Exception {
StringBuffer sb = new StringBuffer();
for(int i = 0;i < 9999;i++) {
sb.append("艹x");
}
System.out.println(subStringByte(sb.toString(),10,20));
}
public static String subStringByte(String strValue,int a,int b) throws Exception{
String value=null;
byte[] bytes = new byte[2048];
System.out.println(bytes.length);
bytes = strValue.getBytes();
System.out.println(bytes.length);
byte[] str = new byte[b-a];//要截取的字节码
int index = 0;
for(int i=a;i<b;i++) {
str[index++] = bytes[i] ;//获取b-a的字节码
}
value = new String(str);//转化为string
return value;
}
sdojqy1122 2012-05-04
  • 打赏
  • 举报
回复
byte[] bytes = new byte[2048];
bytes = strValue.getBytes(); 返回是是一个新的字节数组,也就是说bytes指向的引用变了。不在指向new byte[2048];
至于strValue.getBytes();是多大的字节数组,要去翻源码了。你也可以直接打印出他的长度是多少,看看结果。

likaiyihou512 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

byte[] bytes = new byte[2048];
此时bytes引用的是一个对象
bytes = strValue.getBytes();
这次bytes引用的是strValue.getBytes()返回的另一个对象。这个数组的长度跟2048是没有关系的。
[/Quote]
bytes = strValue.getBytes();怎么让这个 数组的长度 变大点么
cseu 2012-05-04
  • 打赏
  • 举报
回复
byte[] bytes = new byte[2048];
此时bytes引用的是一个对象
bytes = strValue.getBytes();
这次bytes引用的是strValue.getBytes()返回的另一个对象。这个数组的长度跟2048是没有关系的。
likaiyihou512 2012-05-04
  • 打赏
  • 举报
回复
怎么没人,自己顶一个

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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