From 1110a14669ef526920cec02cc417709c8e6e02c2 Mon Sep 17 00:00:00 2001 From: benfry Date: Thu, 2 Sep 2004 21:12:09 +0000 Subject: [PATCH] fixed binary() function --- processing/core/PApplet.java | 30 +++++++++++++++++++++++++-- processing/core/todo.txt | 39 +++--------------------------------- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/processing/core/PApplet.java b/processing/core/PApplet.java index 636535bb6..044a21df8 100644 --- a/processing/core/PApplet.java +++ b/processing/core/PApplet.java @@ -3264,18 +3264,40 @@ public class PApplet extends Applet // + /** + * Returns a String that contains the binary value of a byte. + * The returned value will always have 8 digits. + */ static final public String binary(byte what) { return binary(what, 8); } + /** + * Returns a String that contains the binary value of a char. + * The returned value will always have 16 digits because chars + * are two bytes long. + */ static final public String binary(char what) { return binary(what, 16); } + /** + * Returns a String that contains the binary value of an int. + * The length depends on the size of the number itself. + * An int can be up to 32 binary digits, but that seems like + * overkill for almost any situation, so this function just + * auto-sizes. If you want a specific number of digits (like all 32) + * use binary(int what, int digits) to specify how many digits. + */ static final public String binary(int what) { - return binary(what, 32); + return Integer.toBinaryString(what); + //return binary(what, 32); } + /** + * Returns a String that contains the binary value of an int. + * The digits parameter determines how many digits will be used. + */ static final public String binary(int what, int digits) { String stuff = Integer.toBinaryString(what); @@ -3284,13 +3306,17 @@ public class PApplet extends Applet return stuff.substring(length - digits); } else if (length < digits) { - int offset = 8 - (digits-length); + int offset = 32 - (digits-length); return "00000000000000000000000000000000".substring(offset) + stuff; } return stuff; } + /** + * Unpack a binary String into an int. + * i.e. unbinary("00001000") would return 8. + */ static final int unbinary(String what) { return Integer.parseInt(what, 2); } diff --git a/processing/core/todo.txt b/processing/core/todo.txt index dccd9d8a0..4a723ff30 100644 --- a/processing/core/todo.txt +++ b/processing/core/todo.txt @@ -133,11 +133,12 @@ X http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_Software;acti X incorporated NO_FLYING_POO line/poly hack developed for axel 040902 -X sort should return an array, rather than sort in place +X sort() should return an array, rather than sort in place +X fix binary function, had wrong offset number +X casey: i wan't able to get binary() to work at all: _ standalone 'alpha' function for PImage - The biggest problem was the key and mouse functions not working. For example: Input: Mouse_Functions Input: Keyboard_Functions @@ -148,40 +149,6 @@ Typography: Helix won't compile .... -i wan't able to get binary() to work at all: -here are three examples: - -/ / / / - -int i = 8; -println(binary(i)); - -java.lang.StringIndexOutOfBoundsException: -String index out of range: -20 - at java.lang.String.substring(Unknown Source) - -/ / / / - -char c = 8; -println(binary(c)); - -java.lang.StringIndexOutOfBoundsException: -String index out of range: -4 - at java.lang.String.substring(Unknown Source) - -/ / / / - -char c = 8; -String s = binary(c); -println(s); - - -java.lang.StringIndexOutOfBoundsException: -String index out of range: -4 - at java.lang.String.substring(Unknown Source) - -/ / / / - _ processing.net -> PClient, PServer _ api for file-based renderers