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

标签 java android arrays

我想将字节转换为字符串。

我有一个 Android 应用程序,我使用 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" 时,就会产生问题。

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,所以在这一行中 ojit_代码

我收到以下异常:

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 时我能做什么。

我有两个解决方案 - 我可以检查我的 s = s.substring(0, s.indexOf(0)); 然后使其不执行 subString 条件,否则 inputBytes.length == 10

但我不想使用这个解决方案,因为我在应用程序的很多地方都使用了这个东西。那么,有没有其他方法可以实现这个目标呢?

请给我推荐一些适用于各种情况的好解决方案。我认为最后第二个解决方案会很棒。 (检查字节数组中是否包含 0,然后应用子字符串函数)。

最佳答案

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

关于java - 将 ByteArray 转换为固定长度字符串时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080350/

相关文章:

java - SWT Table 在绘制文本时使用哪个文本边距?

java - 显示打印机对话框

android - 如何同时使用 Realm.io 和 Dagger(或任何需要注释处理的多个依赖项)?

java - Android Studio LibGDX 代码重复问题

java - 停止线程列表

java - 比较具有两个属性的项目数组

java - 从 Item Press 上的 SimpleAdapter/ListView 检索原始 HashMap

java - 如何旋转数组?

java - Java 中的字符串数组 - 声明固定长度,将值传递给构造函数并添加到它们中

javascript - 拼接数组中的对象 - 未定义的错误