[英]How to convert a Byte Array to an Int Array
我正在使用以下方式讀取文件:
int len = (int)(new File(args[0]).length());
FileInputStream fis =
new FileInputStream(args[0]);
byte buf[] = new byte[len];
fis.read(buf);
我在這里發現。 是否可以將byte array buf
轉換為Int Array
? 將Byte Array
轉換為Int Array
會占用更多空間嗎?
編輯:我的文件包含數百萬個整數,例如
100000000 200000000 .....(使用普通的int文件寫入)。 我讀到字節緩沖區。 現在,我想將其包裝到IntBuffer數組中。 怎么做 ? 我不想將每個字節轉換為int。
您已經在注釋中說過,您希望輸入數組中的四個字節對應於輸出數組中的一個整數,因此效果很好。
取決於您希望字節是大端還是小端順序,但是...
IntBuffer intBuf =
ByteBuffer.wrap(byteArray)
.order(ByteOrder.BIG_ENDIAN)
.asIntBuffer();
int[] array = new int[intBuf.remaining()];
intBuf.get(array);
完成,分為三行。
將字節數組的每4個字節轉換為整數數組:
public int[] convert(byte buf[]) {
int intArr[] = new int[buf.length / 4];
int offset = 0;
for(int i = 0; i < intArr.length; i++) {
intArr[i] = (buf[3 + offset] & 0xFF) | ((buf[2 + offset] & 0xFF) << 8) |
((buf[1 + offset] & 0xFF) << 16) | ((buf[0 + offset] & 0xFF) << 24);
offset += 4;
}
return intArr;
}
這樣可以嗎
int IntToByte(byte arrayDst[], int arrayOrg[], int maxOrg){
int i;
int idxDst;
int maxDst;
//
maxDst = maxOrg*4;
//
if (arrayDst==null)
return 0;
if (arrayOrg==null)
return 0;
if (arrayDst.length < maxDst)
return 0;
if (arrayOrg.length < maxOrg)
return 0;
//
idxDst = 0;
for (i=0; i<maxOrg; i++){
// Copia o int, byte a byte.
arrayDst[idxDst] = (byte)(arrayOrg[i]);
idxDst++;
arrayDst[idxDst] = (byte)(arrayOrg[i] >> 8);
idxDst++;
arrayDst[idxDst] = (byte)(arrayOrg[i] >> 16);
idxDst++;
arrayDst[idxDst] = (byte)(arrayOrg[i] >> 24);
idxDst++;
}
//
return idxDst;
}
int ByteToInt(int arrayDst[], byte arrayOrg[], int maxOrg){
int i;
int v;
int idxOrg;
int maxDst;
//
maxDst = maxOrg/4;
//
if (arrayDst==null)
return 0;
if (arrayOrg==null)
return 0;
if (arrayDst.length < maxDst)
return 0;
if (arrayOrg.length < maxOrg)
return 0;
//
idxOrg = 0;
for (i=0; i<maxDst; i++){
arrayDst[i] = 0;
//
v = 0x000000FF & arrayOrg[idxOrg];
arrayDst[i] = arrayDst[i] | v;
idxOrg++;
//
v = 0x000000FF & arrayOrg[idxOrg];
arrayDst[i] = arrayDst[i] | (v << 8);
idxOrg++;
//
v = 0x000000FF & arrayOrg[idxOrg];
arrayDst[i] = arrayDst[i] | (v << 16);
idxOrg++;
//
v = 0x000000FF & arrayOrg[idxOrg];
arrayDst[i] = arrayDst[i] | (v << 24);
idxOrg++;
}
//
return maxDst;
}
將字節數組轉換為整數數組的解決方案,其中每4個字節的集合代表一個整數。 字節輸入是byte[] srcByte
。 int輸出是dstInt[]
。
小尾數源字節:
int shiftBits;
int byteNum = 0;
int[] dstInt = new int[srcByte.length/4]; //you might have to hard code the array length
//Convert array of source bytes (srcByte) into array of integers (dstInt)
for (int intNum = 0; intNum < srcByte.length/4; ++intNum) { //for the four integers
dstInt[intNum] = 0; //Start with the integer = 0
for(shiftBits = 0; shiftBits < 32; shiftBits += 8) { //Add in each data byte, lowest first
dstInt[intNum] |= (srcByte[byteNum++] & 0xFF) << shiftBits;
}
}
對於Big-Endian替換此行:
for(shiftBits = 24; shiftBits >= 0; shiftBits -= 8) //Add in each data byte, highest first
創建一個新的int
數組並復制值,並根據需要進行轉換。
int[] arr = new int[len];
for(int i = 0; i < len; i++)
arr[i] = (int)buf[i];
定義“顯着”。 在Java中,一個int是4個字節,因此根據定義,該數組將是該空間的4倍。 參見: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
在轉換期間,您必須同時擁有兩者,因此在復制部分中,如果您要一次復制整個數組,則將使用更多資源。
至於轉換,有很多相關的問題:
在Java中:
為了進行轉換,您可以執行以下操作:
byte[] byteArray = new byte[] {123, 12, 87};
int[] intArray = new int[byteArray.length];
// converting byteArray to intArray
for (int i = 0; i < byteArray.length; intArray[i] = byteArray[i++]);
System.out.println(Arrays.toString(intArray));
這將輸出:
[123, 12, 87]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.