【问题标题】:Converting a Hex Code String to an int[] of decimal [duplicate]将十六进制代码字符串转换为十进制的 int[] [重复]
【发布时间】:2014-04-05 07:49:21
【问题描述】:

我正在开发一个应用程序以 int[] 的形式发送红外线代码。我有一串十六进制代码:“0000 0048 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 0031 003 0030 0031 0030 0031 0090 0031 0090 0031 0090 0031 073b" 我需要将其转换为 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[]。

请帮忙!

【问题讨论】:

  • 我不这么认为,因为我需要一个 int[] 而不是 byte[]
  • 一个字节只是一个较短的整数版本。他们都持有一个数值。您应该能够很容易地将代码翻译成您的需要。 docs.oracle.com/javase/tutorial/java/nutsandbolts/…
  • 您不需要int[]。你需要一个short[]
  • 无论如何你都会使用Integer.parseInt(yourHexSubstring, 16);
  • 感谢各位的帮助。我想我明白了!

标签: java arrays string hex decimal


【解决方案1】:

我不确定你的目标是什么,但到目前为止你的想法是正确的...... 但不要先执行 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)

如上所述,我不太确定您的目标是什么...这可能导致解析方法错误...

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2017-05-29
    • 1970-01-01
    • 2020-01-03
    • 2019-01-17
    • 2011-04-01
    • 1970-01-01
    • 2016-12-27
    • 2018-01-22
    相关资源
    最近更新 更多