[英]How do I take a string, convert said string to bianary, and then take each 1 or 0 and add it as a value in an array
[英]How do I truncate a string, add the value to an Array, and delete the value?
我正在开发一个 Java 转换器,可以将 ASCII 值转换为 RGB。 当我尝试截断十六进制数据时,出现了我的具体问题。 基本上,我想创建一个系统来循环遍历数据,将其转换为 6 位块,然后将其添加到数组中。 同时为额外的数据创建一个单独的字符串。 这是我的代码:
package converter;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
public class ASCIItoRGB{
public static void main(String args[]) {
// Input
String asciiValue = "2121876";
char[] c = asciiValue.toCharArray();
StringBuffer hexValue = new StringBuffer();
for(int i=0; i<c.length; i++) {
hexValue.append(Integer.toHexString((int)c[i]));
}
String hexString = hexValue.toString();
String hexTruncated = hexValue.toString();
ArrayList rgbValues = new ArrayList();
for(int i=0; i<hexString.length(); i++) {
if(hexTruncated.length() >= 6) {
String tempval = hexTruncated.substring(0, 5);
rgbValues.add(Color.decode(tempval));
tempval = hexTruncated.substring(tempval.length(), 5);
}
else {
}
}
// Output
System.out.println("ASCII VALUE: " + asciiValue);
System.out.println("CHAR ARRAY: " + Arrays.toString(c));
System.out.println("HEX VALUE: " + hexString);
System.out.println("RGB VALUES: " + rgbValues.toString());
}
}
好的。 我通过使用 while 循环让它工作。 我也改变tempval
等于一个子hexString
利用自身的legnth,和的legnth hexString
。
// ASCII to Hex
char[] c = asciiValue.toCharArray();
StringBuffer hexValue = new StringBuffer();
for(int i=0; i<c.length; i++) {
hexValue.append(Integer.toHexString((int)c[i]));
}
String hexString = hexValue.toString();
// Hex to RGB
ArrayList<Color> rgbValues = new ArrayList<Color>();
while(hexString.length() >= 6) {
String tempval = hexString.substring(0,6);
rgbValues.add(Color.decode(tempval));
hexString = hexString.substring(tempval.length(), hexString.length());
if(hexString.length() < 6) {
String hexExtra = hexString;
System.out.println("EXTRA DATA: " + hexExtra);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.