Java Iterate Bits in Byte Array

You’d have to write your own implementation of Iterable<Boolean> which took an array of bytes, and then created Iterator<Boolean> values which remembered the current index into the byte array and the current index within the current byte. Then a utility method like this would come in handy: private static Boolean isBitSet(byte b, int bit) { … Read more

Hashing with SHA1 Algorithm in C#

For those who want a “standard” text formatting of the hash, you can use something like the following: static string Hash(string input) { using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); var sb = new StringBuilder(hash.Length * 2); foreach (byte b in hash) { // can be “x2” if you want lowercase … Read more

How to Get True Size of MySQL Database?

From S. Prakash, found at the MySQL forum: SELECT table_schema “database name”, sum( data_length + index_length ) / 1024 / 1024 “database size in MB”, sum( data_free )/ 1024 / 1024 “free space in MB” FROM information_schema.TABLES GROUP BY table_schema; Or in a single line for easier copy-pasting: SELECT table_schema “database name”, sum( data_length + … Read more

C# Replace bytes in Byte[]

You could program it…. try this for a start… this is however not robust not production like code yet…beaware of off-by-one errors I didn’t fully test this… public int FindBytes(byte[] src, byte[] find) { int index = -1; int matchIndex = 0; // handle the complete source array for(int i=0; i<src.Length; i++) { if(src[i] == … Read more

Converting 2 bytes to Short in C#

If you reverse the values in the BitConverter call, you should get the expected result: int actualPort = BitConverter.ToUInt16(new byte[2] {(byte)port2 , (byte)port1 }, 0); On a little-endian architecture, the low order byte needs to be second in the array. And as lasseespeholt points out in the comments, you would need to reverse the order … Read more

Is a byte always 8 bits?

Yes, a byte is always 8 bits in modern computing. The book uses Words, not bytes In the book, the word and the size of the word is explicitly mentioned, while there is not a word (haha) about bytes. Look at the phrase ..is represented in RAM by 32 consecutive 16-bit words.. The whole size … Read more