কি ফাস্ট একটি রূপান্তর করতে উপায় Integer
একটি মধ্যে Byte Array
?
যেমন 0xAABBCCDD => {AA, BB, CC, DD}
কি ফাস্ট একটি রূপান্তর করতে উপায় Integer
একটি মধ্যে Byte Array
?
যেমন 0xAABBCCDD => {AA, BB, CC, DD}
উত্তর:
কটাক্ষপাত আছে ByteBuffer বর্গ।
ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(0xAABBCCDD);
byte[] result = b.array();
বাইট অর্ডার নিশ্চিত সেট যে result[0] == 0xAA
, result[1] == 0xBB
, result[2] == 0xCC
এবং result[3] == 0xDD
।
অথবা বিকল্পভাবে, আপনি নিজেই এটি করতে পারেন:
byte[] toBytes(int i)
{
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);
return result;
}
ByteBuffer
বর্গ যদিও মলিন হাত কাজের জন্য পরিকল্পনা করা হয়েছিল। বাস্তবে ব্যক্তিগত java.nio.Bits
সাহায্যকারী পদ্ধতিগুলি যা দ্বারা ব্যবহৃত হয় তা সংজ্ঞায়িত করে ByteBuffer.putInt()
:
private static byte int3(int x) { return (byte)(x >> 24); }
private static byte int2(int x) { return (byte)(x >> 16); }
private static byte int1(int x) { return (byte)(x >> 8); }
private static byte int0(int x) { return (byte)(x >> 0); }
ব্যবহার BigInteger
:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
ব্যবহার DataOutputStream
:
private byte[] intToByteArray ( final int i ) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(i);
dos.flush();
return bos.toByteArray();
}
ব্যবহার ByteBuffer
:
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
ByteBuffer
যদি আপনি 2 ^ 31 - 1 এর চেয়ে বেশি
এই ফাংশনটি ব্যবহার করুন এটি আমার জন্য কাজ করে
public byte[] toByteArray(int value) {
return new byte[] {
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value};
}
এটি একটি বাইট মান মধ্যে ইন্ট অনুবাদ
আপনি যদি পেয়ারা পছন্দ করেন তবে আপনি এটির Ints
ক্লাস ব্যবহার করতে পারেন :
জন্য int
→ byte[]
, ব্যবহার toByteArray()
:
byte[] byteArray = Ints.toByteArray(0xAABBCCDD);
ফলাফল হয় {0xAA, 0xBB, 0xCC, 0xDD}
।
তার বিপরীত হয় fromByteArray()
বা fromBytes()
:
int intValue = Ints.fromByteArray(new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD});
int intValue = Ints.fromBytes((byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD);
ফলাফল হয় 0xAABBCCDD
।
আপনি ব্যবহার করতে পারেন BigInteger
:
পূর্ণসংখ্যা থেকে:
byte[] array = BigInteger.valueOf(0xAABBCCDD).toByteArray();
System.out.println(Arrays.toString(array))
// --> {-86, -69, -52, -35 }
প্রত্যাবর্তিত অ্যারেটি এমন আকারের হয় যা সংখ্যার প্রতিনিধিত্ব করতে প্রয়োজনীয় হয়, সুতরাং এটি উদাহরণস্বরূপ 1 টি উপস্থাপন করার জন্য এটি আকারের 1 হতে পারে। যাইহোক, কোনও int পাস করা হলে আকারটি চার বাইটের বেশি হতে পারে না।
স্ট্রিংগুলি থেকে:
BigInteger v = new BigInteger("AABBCCDD", 16);
byte[] array = v.toByteArray();
তবে আপনাকে প্রথম 0x7F
নজরদারিটি দেখতে হবে, যদি প্রথম বাইটটি উচ্চতর হয় (যেমন এই ক্ষেত্রে রয়েছে), যেখানে বিগইন্টিজার অ্যারের শুরুতে 0x00 বাইট প্রবেশ করিয়ে দেবে। ইতিবাচক এবং নেতিবাচক মানগুলির মধ্যে পার্থক্য করার জন্য এটি প্রয়োজন।
অ্যান্ড্রয়েডের সাথে খুব সহজ
int i=10000;
byte b1=(byte)Color.alpha(i);
byte b2=(byte)Color.red(i);
byte b3=(byte)Color.green(i);
byte b4=(byte)Color.blue(i);
শিফটও করতে পারে -
byte[] ba = new byte[4];
int val = Integer.MAX_VALUE;
for(byte i=0;i<4;i++)
ba[i] = (byte)(val >> i*8);
//ba[3-i] = (byte)(val >> i*8); //Big-endian
এখানে একটি পদ্ধতি যা ঠিক কাজটি করা উচিত।
public byte[] toByteArray(int value)
{
final byte[] destination = new byte[Integer.BYTES];
for(int index = Integer.BYTES - 1; index >= 0; index--)
{
destination[i] = (byte) value;
value = value >> 8;
};
return destination;
};
এটি আমার সমাধান:
public void getBytes(int val) {
byte[] bytes = new byte[Integer.BYTES];
for (int i = 0;i < bytes.length; i ++) {
int j = val % Byte.MAX_VALUE;
bytes[i] = (j == 0 ? Byte.MAX_VALUE : j);
}
}
এছাড়াও String
y পদ্ধতি:
public void getBytes(int val) {
String hex = Integer.toHexString(val);
byte[] val = new byte[hex.length()/2]; // because byte is 2 hex chars
for (int i = 0; i < hex.length(); i+=2)
val[i] = Byte.parseByte("0x" + hex.substring(i, i+2), 16);
return val;
}