【发布时间】:2016-03-02 21:57:00
【问题描述】:
我只是想弄清楚如何将 int 数字转换为字节。 这听起来很简单,但对我来说很难知道如何转换它。
例如:
public byte[] getByteArray(String ipOrMac){
String[] temp = new String[0];
ArrayList<Integer> intList = new ArrayList<Integer>();
if(ipOrMac.contains(":")){
temp = ipOrMac.split(":"); // this makes temp into a new Array
//with splitted strings
}
if(ipOrMac.contains(".")){
temp = ipOrMac.split(".");
}
for(int a = 0; a<=temp.length-1; a++){
intList.add(Integer.parseInt(temp[a]));
}
// System.out.println(stringList.toArray()[0]);
// for(int a = 0; a<=stringList.size()-1; a++){
// stringList.
// }
return null;
}
我的主要目标是得到一个像这样“2:2:2:2”的字符串(它是一个mac地址) 成一个字节[]。 所以现在我有问题将我的arraylist-integer 中的所有整数转换为字节。 我不想使用数组列表字节,因为它效率低下...
有什么想法吗?
希望你能帮助我。 :)
【问题讨论】:
-
“我不想使用 arraylist-byte,因为它效率低下……” 程序变慢是因为它们做的事情太多。除非你用
List做一百万次,否则没有理由考虑微优化,更不用说围绕它设计程序了。 -
@Radiodef,提示“过早的优化是万恶之源”。
标签: java arraylist int byte bytearray