Does Java read integers in little endian or big endian?
Use the network byte order (big endian), which is the same as Java uses anyway. See man htons for the different translators in C.
Does Java read a single byte in big endian bit order?
Endianness only really applies when a unit is broken down into other units. So if you were transmitting a byte over a bit stream, you could observe whether the least significant bit was transmitted first or last. And at that point we could say that the stream was little-endian or big-endian.
But within a byte-addressable machine, i.e., where the byte is the smallest unit of storage, there is no "endianness" within the byte. No bit of the byte is "before" any other bit of the byte.
Note that another term for endianness is "byte order". The order of bytes within larger entities.
It is true we like to number bits (0 to 7, for an 8-bit byte) so we can talk about them, but this really does not define endianness, even though the numbering is often chosen to match the byte order of the machine; this is convention.
With respect to FileInputStream - according to its documentation, that transfers bytes: no part of the byte is sent before any other part, at least not as far as FileInputStream is concerned. If the byte has to be sent bitwise over some interconnect (say, a SATA cable), then the decision about which bit goes first is a matter for the hardware. The higher layer code is dealing in bytes (or even blocks).
little endian to integers (or BigInteger)
I'll assume a similar structure in Java:
long rlwe_table[][] = {
{0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0x1FFFFFFFFFFFFFFFL},
{0xE0C81DA0D6A8BD22L, 0x161ABD186DA13542L, 0x5CEF2C248806C827L},
{0x8D026C4E14BC7408L, 0x4344C125B3533F22L, 0x9186506BCC065F20L},
{0x10AC7CEC7D7E2A3BL, 0x5D62CE65E6217813L, 0xBAAB5F82BCDB43B3L},
{0x709C92996E94D801L, 0x1411F551608E4D22L, 0xD7D9769FAD23BCB1L},
{0x6287D827008404B7L, 0x7E1526D618902F20L, 0xEA9BE2F4D6DDB5EDL},
{0x34CBDC118C15F40EL, 0xE7D2A13787E94674L, 0xF58A99474919B8C9L},
{0xD521F7EBBBE8C3A2L, 0xE8A773D9A1EA0AABL, 0xFB5117812753B7B8L},
{0xC3D9E58131089A6AL, 0x148CB49FF716491BL, 0xFE151BD0928596D3L},
{0x2E060C4A842A27F6L, 0x07E44D009ADB0049L, 0xFF487508BA9F7208L},
{0xFCEDEFCFAA887582L, 0x1A5409BF5D4B039EL, 0xFFC16686270CFC82L},
{0x4FE22E5DF9FAAC20L, 0xFDC99BFE0F991958L, 0xFFEC8AC3C159431BL},
{0xA36605F81B14FEDFL, 0xA6FCD4C13F4AFCE0L, 0xFFFA7DF4B6E92C28L},
{0x9D1FDCFF97BBC957L, 0x4B869C6286ED0BB5L, 0xFFFE94BB4554B5ACL},
{0x6B3EEBA74AAD104BL, 0xEC72329E974D63C7L, 0xFFFFAADE1B1CAA95L},
{0x48C8DA4009C10760L, 0x337F6316C1FF0A59L, 0xFFFFEDDC1C6436DCL},
{0x84480A71312F35E7L, 0xD95E7B2CD6933C97L, 0xFFFFFC7C9DC2569AL},
{0x23C01DAC1513FA0FL, 0x8E0B132AE72F729FL, 0xFFFFFF61BC337FEDL},
{0x90C89D6570165907L, 0x05B9D725AAEA5CADL, 0xFFFFFFE6B3CF05F7L},
{0x692E2A94C500EC7DL, 0x99E8F72C370F27A6L, 0xFFFFFFFC53EA610EL},
{0x28C2998CEAE37CC8L, 0xC6E2F0D7CAFA9AB8L, 0xFFFFFFFF841943DEL},
{0xC515CF4CB0130256L, 0x4745913CB4F9E4DDL, 0xFFFFFFFFF12D07ECL},
{0x39F0ECEA047D6E3AL, 0xEE62D42142AC6544L, 0xFFFFFFFFFE63E348L},
{0xDF11BB25B50462D6L, 0x064A0C6CC136E943L, 0xFFFFFFFFFFD762C7L},
{0xCDBA0DD69FD2EA0FL, 0xC672F3A74DB0F175L, 0xFFFFFFFFFFFC5E37L},
{0xFDB966A75F3604D9L, 0x6ABEF8B144723D83L, 0xFFFFFFFFFFFFB48FL},
{0x3C4FECBB600740D1L, 0x697598CEADD71A15L, 0xFFFFFFFFFFFFFA72L},
{0x1574CC916D60E673L, 0x12F5A30DD99D7051L, 0xFFFFFFFFFFFFFFA1L},
{0xDD3DCD1B9CB7321DL, 0x4016ED3E05883572L, 0xFFFFFFFFFFFFFFFAL},
{0xB4A4E8CF3DF79A7AL, 0xAF22D9AFAD5A73CFL, 0xFFFFFFFFFFFFFFFFL},
{0x91056A8196F74466L, 0xFBF88681905332BAL, 0xFFFFFFFFFFFFFFFFL},
{0x965B9ED9BD366C04L, 0xFFD16385AF29A51FL, 0xFFFFFFFFFFFFFFFFL},
{0xF05F75D38F2D28A3L, 0xFFFE16FF8EA2B60CL, 0xFFFFFFFFFFFFFFFFL},
{0x77E35C8980421EE8L, 0xFFFFEDD3C9DDC7E8L, 0xFFFFFFFFFFFFFFFFL},
{0x92783617956F140AL, 0xFFFFFF63392B6E8FL, 0xFFFFFFFFFFFFFFFFL},
{0xA536DC994639AD78L, 0xFFFFFFFB3592B3D1L, 0xFFFFFFFFFFFFFFFFL},
{0x8F3A871874DD9FD5L, 0xFFFFFFFFDE04A5BBL, 0xFFFFFFFFFFFFFFFFL},
{0x310DE3650170B717L, 0xFFFFFFFFFF257152L, 0xFFFFFFFFFFFFFFFFL},
{0x1F21A853A422F8CCL, 0xFFFFFFFFFFFB057BL, 0xFFFFFFFFFFFFFFFFL},
{0x3CA9D5C6DB4EE2BAL, 0xFFFFFFFFFFFFE5ADL, 0xFFFFFFFFFFFFFFFFL},
{0xCFD9CE958E59869CL, 0xFFFFFFFFFFFFFF81L, 0xFFFFFFFFFFFFFFFFL},
{0xDB8E1F91D955C452L, 0xFFFFFFFFFFFFFFFDL, 0xFFFFFFFFFFFFFFFFL},
{0xF78EE3A8E99E08C3L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFE1D7858BABDA25L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFF9E52E32CAB4AL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFEE13217574FL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFFFD04888041L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFFFFF8CD8A56L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFFFFFFF04111L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFFFFFFFFE0C5L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFFFFFFFFFFC7L, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL},
{0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL}
};
If you want to combine them to an array of BigInteger
s, you can append the numbers with the shiftLeft
and or
methods.
Like this:
BigInteger[] bigIntegers = new BigInteger[rlwe_table.length];
for(int i=0; i<rlwe_table.length; i++){
BigInteger big = BigInteger.valueOf(0);
for(int j=0; j<rlwe_table[i].length; j++){
big = big.or(new BigInteger(Long.toHexString(rlwe_table[i][j]),16).shiftLeft(j*64));
}
bigIntegers[i] = big;
}
The result is in variable bigIntegers
.
Read unsigned integers in little endian byte order from a socket
Integers will likely be sent as four byte values from a 32-bit system. So you need to read four bytes into an array, then convert that into an int:
Socket socket = new Socket("vortex.labs.overthewire.org", 5842);
DataInputStream in = new DataInputStream(socket.getInputStream());
byte[] intData = new byte[4];
for (int i = 0; i < 4; i++) {
in.readFully(intData);
System.out.println(ByteBuffer.wrap(intData)
.order(ByteOrder.LITTLE_ENDIAN).getInt());
}
Related: How can I convert a 4-byte array to an integer?
Read 16Bit Integer from 2 bytes big endian
An integer is comprised of 4 bytes, so your byteArr needs to have 4 elements, not 2:
byte[] byteArr = new byte[4];
Write an integer in little endian
When you don't know why something failed, it is a bad idea to ignore exceptions in an empty try-catch block.
Odds are excellent that you are running the program in an environment where the file cannot be created; however, the instructions you gave to handle such an exceptional situation is to do nothing. So, odds are you have a program that attempted to run, but failed with some reason, which was handled by not even displaying the reason to you.
try this
public static void copy(String fileOutName, boolean append){
File fileOut = new File (fileOutName);
try {
FileChannel wChannel = new FileOutputStream(fileOut, append).getChannel();
int i = 5;
ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(i);
bb.flip();
int written = wChannel.write(bb);
System.out.println(written);
wChannel.close();
} catch (IOException e) {
// this is the new line of code
e.printStackTrace();
}
}
And I'll bet you find out why it doesn't work right away.
Fast reading of little endian integers from file
Why not read into a 4-byte buffer and then rearrange the bytes manually? It will look like this:
for (int i=0; i < SIZE_Y; i++) {
for (int j=0; j < SIZE_X; j++) {
inp.read(buffer);
int nextInt = (buffer[0] & 0xFF) | (buffer[1] & 0xFF) << 8 | (buffer[2] & 0xFF) << 16 | (buffer[3] & 0xFF) << 24;
test_data[j][SIZE_Y - i - 1] = nextInt;
}
}
Of course, it is assumed that read
reads all four bytes, but you should check for the situation when it's not. This way you won't create any objects during reading (so no strain on the garbage collector), you don't call anything, you just use bitwise operations.
Little and Big Endian in Java (android)
Little Endian and Big Endian simply refer to the order in which the bytes of a data structure are presented.
Imagine for a moment that you have a 16-bit integer that is represented by the hexidecimal value 0xabcd. Because 8 bits = 1 byte, we have our integer is composed of two bytes, ab and cd. In a Big Endian system, the most significant bytes are placed in the lower memory address, while in Little Endian systems, we put them in the higher one.
To show this visually, assume we've put our integer at the memory address 0.
In a Big Endian system, our memory would look like this:
Memory address -> | 0 | 1 |
Value -> | ab | cd |
In a Little Endian system, it would look like this:
Memory address -> | 0 | 1 |
Value -> | cd | ab |
Traditionally, network byte order is Big Endian.
write a function that determines whether a computer is big endian or little endian in java
In a Little Endian architecture, the integer value 0x1 will be written out in memory like
0x1 0x0 0x0 ....
so that function will return 1
Conversely, in a Big Endian architecture, the order of bytes will be
0x0 0x0 ... 0x1
however many bytes are in an int (greater or equal to 2 for it to work), and so
this function will return 0.
Here's a reference for why it can't be done in Java directly, but you could use JNI to escape out to a C library and return the result.
Related Topics
How to Create an 2D Arraylist in Java
String Replacement in Java, Similar to a Velocity Template
How to Get the Http Status Code Out of a Servletresponse in a Servletfilter
Differencebetween Canonical Name, Simple Name and Class Name in Java Class
JPA How to Make Composite Foreign Key Part of Composite Primary Key
Javamail with Gmail: 535-5.7.1 Username and Password Not Accepted
Tool to Convert Java to C# Code
When Not to Use the Static Keyword in Java
When Do You Need to Explicitly Call a Superclass Constructor
Data Structures That Can Map a Range of Keys to a Value
How Do Java Method Annotations Work in Conjunction with Method Overriding
How to Turn Off the Httpsession in Web.Xml
Accurate Sleep for Java on Windows
In Which Language Are the Java Compiler and Jvm Written