首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将ByteArray转换为固定长度字符串时出现异常

将ByteArray转换为固定长度字符串时出现异常
EN

Stack Overflow用户
提问于 2012-12-29 17:25:54
回答 2查看 1.2K关注 0票数 0

我想把字节转换成字符串。

我有一个安卓应用程序,我正在使用flatfile进行数据存储。

假设我的flatfile中有很多记录。

在这里,在平面文件数据库中,我的记录大小是固定的,它的10字符,我在这里存储了大量的字符串记录序列。

但是当我从平面文件中读取一条记录时,每条记录的字节数是固定的。因为我为每条记录写了10个字节。

如果我的字符串是S="abc123";,那么它就存储在像abc123 ASCII values for each character and rest would be 0这样的平面文件中。表示字节数组应为[97 ,98 ,99 ,49 ,50 ,51,0,0,0,0]。因此,当我想要从字节数组中获取实际的字符串时,我使用了下面的代码,它工作得很好。

但是,当我给出我的inputString = "1234567890"时,它就会产生问题。

代码语言:javascript
运行
复制
public class MainActivity extends Activity {
    public static short messageNumb = 0;
    public static short appID = 16;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // record with size 10 and its in bytes.
        byte[] recordBytes = new byte[10];
        // fill record by 0's
        Arrays.fill(recordBytes, (byte) 0);

        // input string
        String inputString = "abc123";
        int length = 0;
        int SECTOR_LENGTH = 10;
        // convert in bytes
        byte[] inputBytes = inputString.getBytes();
        // set how many bytes we have to write.
        length = SECTOR_LENGTH < inputBytes.length ? SECTOR_LENGTH
                : inputBytes.length;

        // copy bytes in record size.
        System.arraycopy(inputBytes, 0, recordBytes, 0, length);

        // Here i write this record in the file.
        // Now time to read record from the file.
        // Suppose i read one record from the file successfully.

        // convert this read bytes to string which we wrote.
        Log.d("TAG", "String is  = " + getStringFromBytes(recordBytes));

    }

    public String getStringFromBytes(byte[] inputBytes) {
        String s;
        s = new String(inputBytes);
        return s = s.substring(0, s.indexOf(0));
    }
}

但是当我的字符串有完整的10个字符时,我遇到了问题。这时我的字节数组中有两个0,所以在这行s = s.substring(0, s.indexOf(0));

我得到了以下异常:

代码语言:javascript
运行
复制
java.lang.StringIndexOutOfBoundsException: length=10; regionStart=0; regionLength=-1
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)

那么当我的字符串长度是10的时候,我能做什么呢?

我有两个解决方案-我可以检查我的inputBytes.length == 10,然后让它不做subString条件,否则check contains 0 in byte array

但是我不想使用这个解决方案,因为我在应用程序中的很多地方都使用了这个东西。那么,有没有其他方法可以做到这一点呢?

请建议我一些好的解决方案,在任何情况下都有效。我认为最后的第二个解决方案将是伟大的。(检查字节数组中包含0,然后应用子字符串函数)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-29 17:35:16

代码语言:javascript
运行
复制
public String getStringFromBytes(byte[] inputBytes) {
    String s;
    s = new String(inputBytes);
    int zeroIndex = s.indexOf(0);
    return zeroIndex < 0 ? s : s.substring(0, zeroIndex);
}
票数 1
EN

Stack Overflow用户

发布于 2012-12-29 17:34:00

我认为这一行导致了错误

代码语言:javascript
运行
复制
s = s.substring(0, s.indexOf(0));

s.indexOf(0)

返回-1,也许您应该为零指定ASCII码,即48

所以这将会起作用s = s.substring(0, s.indexOf(48));

查看indexOf(int)的文档

public int API (int c) for : indexOf级别1在此字符串中搜索指定字符的第一个索引。对字符的搜索从开始处开始,并向此字符串的末尾移动。

参数c要查找的字符。返回此字符串中指定字符的索引,如果未找到该字符,则返回-1。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14080350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档