2014-03-04 158 views
0

我正在开发一个以int []形式发送红外代码的应用程序。我有一串十六进制代码:“0000 0048 0000 0018 00c1 00c0 0031 0090 0031 0090 0031 0030 0031 0090 0031 0090 0031 0090 0031 0090 0031 0090 0031 0030 0031 0030 0031 0030 0031 0030 0031 0030 0031 0090 0031 0030 0031 0030 0031 0030 0031 0030 0031 0030 0031 0090 0031 0090 0031 0090 0031 073b“ 我需要将它转换为以十进制形式分隔的int []分隔符。将十六进制代码字符串转换为十进制的int []

String hexCode = "0000 0048 0000 0018 00c1 00c0 0031 0090 0031 0090 0031 0030 0031 0090 0031 0090 0031 0090 0031 0090 0031 0090 0031 0030 0031 0030 0031 0030 0031 0030 0031 0030 0031 0090 0031 0030 0031 0030 0031 0030 0031 0030 0031 0030 0031 0090 0031 0090 0031 0090 0031 073b" 
String decimalCode = hex2dec(hexCode); //I don't know how to convert this and keep the spaces 
String[] decArray = decimalCode.split(" "); 
int[] final = decArray; //Not sure how to do this. Maybe a for loop? 

我一直在这工作了几个小时,越来越沮丧。我不能从十六进制转换成十进制字符串,然后我不能把它放到一个int []中。

请帮忙!

+0

我不这么认为,因为我需要一个int []而不是一个字节[] – Jason

+1

一个字节只是一个int的较短版本。他们都拥有一个数值。你应该能够很容易地翻译代码来满足你的需求。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – aliteralmind

+1

你不需要'int []'。你需要一个'short []'。 – tbodt

回答

0

我不确定你的目标是什么,但是到目前为止你有正确的想法......但是,不是做一个hex2dec然后拆分你应该颠倒顺序,说:先拆分然后转换。 ...

String hexCode = "0000 0048 0000 0018 00c1 00c0 0031 0090 0031 0090 0031 0030 0031 0090 0031 0090 0031 0090 0031 0090 0031 0090 0031 0030 0031 0030 0031 0030 0031 0030 0031 0030 0031 0090 0031 0030 0031 0030 0031 0030 0031 0030 0031 0030 0031 0090 0031 0090 0031 0090 0031 073b" 

//splitting the hexcode into a string array 
String[] splits = decimalCode.split(" "); 

//getting the length of the string aray, we need this to set 
//the right size of the int[] 
int amount = splits.length(); 

//values are the values wich you're interested in... 
//we create the array with proper size(amount) 
int[] values = new int[amount] 

//now we iterate through the strong[] splits 
for (int i = 0; i < amount; i ++){ 

    //we take a string vrom the array 
    String str = splits[i]; 

    //the we parse the stringv into a int-value 
    int parsedValue = Integer.parseInt(str, 16); 

    //and fill up the value-array 
    values[i] = parsedValue; 
} 
//when we're through with the iteration loop, we're done (so far) 

如上面提到的,我不是很确定你的目标在什么...这可能会导致错误的分析方法......

相关问题