检查字符串内especific值 [英] Check for a especific value inside a string

查看:122
本文介绍了检查字符串内especific值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新时间:

在我的应用程序,我有通过蓝牙PCB一个连续的通信。此通信是通过HEX字符串的。

该字符串格式是这样的:

 图2b(线程初始化),05(写入命令),09(字节),04(可变字节),5af2ff1f(存储位置),04a0bcf1(变量值),7E(校验)

这十六进制字符串的功能是访问到PCB的一个具体的存储位置和写入一个值

要做到这一点更容易,让suppouse的十六进制字符串是:

  hex_string = 2B05A82BF1

这个字符串,通过蓝牙发送前,我将其转换为一个byte []。我需要的是创建一个检查所有我送字符串的函数。

我必须寻找,是字符串中的任何2B字节。这是因为在PCB读取2B作为初始化线程值,如果有任何2B的字符串内,在PCB会读它作为另一个线程的init和串将不被完全发送。

我不知道,如果是比较容易的十六进制字符串来检查,或之后将其转换为字节]

该功能将是disscard字符串的第一个2B,并检查是否有另一个2B字符串里面,如果是这样,复制它,造成这一点。

 前:hex_string = 2B05A82BF1
后:hex_string = 2B05A82B2BF1

这样,PCB理解的2B不是一个初始化线程值。

我想要做的是检查所有的字符串包含它内部的2B值的函数。但我不知道它是更好地与十六进制字符串,当转换为byte [] ...或...时,如何做才能做到这一点。


解决方案

这是你在找什么呢?不知道...


  

输入:2BXX09XX2BYYYY输出:2BXX0AXX2B2BYYYY


 公共类温度{    公共静态无效的主要(字符串ARGS []){
        字符串输入= convertSting(2BXX09XX2BYYYY);
        输入= replaceSize(输入,0A);        的System.out.println(输入);
    }    私有静态字符串replaceSize(字符串输入,字符串newSizeVal){
        INT sizePosition = 4;
        返回input.substring(0,sizePosition)+ newSizeVal + input.substring(sizePosition + newSizeVal.length(),input.length());
    }    公共静态字符串convertSting(字符串输入){
        字符串FINDSTRING =2B        INT firstIndex = input.indexOf(FINDSTRING)+ findString.length();
        返回input.substring(0,firstIndex)+ input.substring(firstIndex和input.length())替换(FINDSTRING2B2B);
    }
}

UPDATED

In my app, I have a continuos communication with a PCB via bluetooth. This communication is made by HEX Strings.

The string format is like this:

2b(thread init), 05(write command), 09(bytes), 04(variable bytes), 5af2ff1f(memory location), 04a0bcf1(variable value), 7e(checksum)

The function of this Hex string is to access to a concrete memory location of the PCB and write a value.

To do it easier, lets suppouse that the Hex string is:

hex_string= 2B05A82BF1

This string, before sending it via bluetooth, I convert it to a Byte[]. What I need is to create a function that checks all the strings I send.

What I must look for, is for any "2B" byte inside the string. This is because the PCB reads the "2b" as a "init thread" value and if there is any "2b" inside the string, the PCB would read it as the init of another thread and the string wouldn't be sent entirely.

I don't know if is easier to check this with the Hex String, or after converting it to Byte[].

The function would be to disscard the first "2b" of the string and check if there is another "2b" inside the string, and if it is, duplicate it, resulting this.

before: hex_string= 2B05A82BF1
after: hex_string= 2B05A82B2BF1

This way the PCB understands that that "2b" is not a init thread value.

What I want to do is a function that checks all string for containing "2b" value inside of it. But i don't know if it's better to do this with the Hex string, when converted to byte[]... or when... and how to do it.

解决方案

is this what you are looking for? not sure...

Input: 2BXX09XX2BYYYY Output: 2BXX0AXX2B2BYYYY

public class Temp {

    public static void main (String args[]) {
        String input = convertSting("2BXX09XX2BYYYY");
        input = replaceSize(input, "0A");

        System.out.println(input);
    }

    private static String replaceSize(String input, String newSizeVal) {
        int sizePosition = 4;
        return input.substring(0, sizePosition) + newSizeVal + input.substring(sizePosition + newSizeVal.length(), input.length());
    }

    public static String convertSting (String input) {
        String findString = "2B";

        int firstIndex = input.indexOf(findString) + findString.length();
        return input.substring(0, firstIndex) + input.substring(firstIndex, input.length()).replace(findString, "2B2B");
    }
}

这篇关于检查字符串内especific值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆