首页 > 解决方案 > 将不带空终止的字符串传递给java中的方法

问题描述

我在 Raspberry Pi3 上使用 vr3 语音转文本模块。我想用我给出的语音命令激活引脚输出。下面的代码块有问题。如果我string()直接输入方法speech2command,一切看起来都很正常;(speech2command("ONE") ..etc)。如果我使用语音字符串值,我会遇到问题。也许我需要一个空终止的字符串?如果是这样,我应该如何添加?

private static String toASCII(int value) {

    int length = 4;
    StringBuilder builder = new StringBuilder(length);
    for (int i = length - 1; i >= 0; i--) {
        builder.append((char) ((value >> (8 * i)) & 0xFF));
    }
    return builder.toString();
}
//******************************************************************************   

public static void speech2command(String strSource) {

    //   System.out.println("--> Comingstring-->"+strSource);      
    if(strSource.equals("ZERO")==true) {
        System.out.println("--> GPIO state-->ZERO STATE");
        output.low();  // LED OFF
    }
    if(strSource.equals("ONE")==true) {
        System.out.println("--> GPIO state-->ONE STATE");
        output.high();  // LED ON
    }    
}

//****************************************************************************
//
//
//

for (int i = 0; i < l; i++) {
                     
    ret[i] = Integer.valueOf(globalstringhex.substring(i * 2, i * 2 + 2), 
16).byteValue();//Hexstring --> ASCII 
    int k=ret[i];
    if (k>=32 && k<=126) {//input "readable chars"   
        speechstring+= toASCII(k);           
    }                   
}

globalstringhex="";
speech2command(speechstring);//---> This line is not working correctly. 
textArea_4.append("\n\r");

speechcommand("ONE");//---> if i use parameter "ZERO" or "ONE" everything is ok. But if i use 
speechcommand(speechstring); line ,  LED never work.

标签: java

解决方案


推荐阅读