Skip to content

Instantly share code, notes, and snippets.

@KeyMaster-
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeyMaster-/1a2a0d6ea6d70180ea91 to your computer and use it in GitHub Desktop.
Save KeyMaster-/1a2a0d6ea6d70180ea91 to your computer and use it in GitHub Desktop.
haxe.io.Bytes to bit string representation
import haxe.io.Bytes;
class BytesUtil {
/**
* Represent a Bytes object as single bits in a string
* @param b The bytes
* @return A string of '0's and '1's representing the bytes
*/
public static function toBits(b:Bytes):String {
var s:String = "";
for (i in 0...b.length) {
for (j in 0...8) {
s += Std.string(b.get(i) & (1 << (7 - j)) != 0 ? 1 : 0);
}
}
return s;
}
}
@Gama11
Copy link

Gama11 commented Jul 7, 2014

More of a toString() really. :)

@KeyMaster-
Copy link
Author

Well there is also the toHex function which is why I called it toBits. Also, toString already exists which interprets bytes into characters to form a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment