Conversion & utility functions

Type conversion, array manipulation, and string formatting. Processing/Java needed dedicated functions for most of these because of stricter primitive typing and no operator overloading — in C# they exist mainly for 1:1 API parity with sketches ported from Processing.

On this page · 24 entries

Int

  • Int(float value)
  • Int(string value)
  • Int(bool value) → int

Truncates a float toward zero (not rounding), parses a string (0 on failure), or converts true/false to 1/0.

Float

  • Float(int value)
  • Float(string value) → float

Converts an int, or parses a string — float.NaN on failure, matching Processing's own documented behavior.

Boolean

  • Boolean(float value)
  • Boolean(string value) → bool

Nonzero is true for numbers; a case-insensitive "true" for strings, anything else false.

Byte

  • Byte(int value)
  • Byte(float value)
  • Byte(char value) → byte

Truncates/wraps into a byte using two's-complement, matching Java/Processing's own byte() rather than throwing on overflow.

Char

  • Char(int value)
  • Char(byte value) → char

Converts a numeric code point to its character — Char(65) is 'A'.

Str

  • Str(int)
  • Str(float)
  • Str(bool)
  • Str(char) → string

Renders a value as text. Exists mainly so ported sketch code compiles unchanged — C#'s own ToString() already covers every case here.

Hex

  • Hex(int)
  • Hex(int, digits)
  • Hex(byte)
  • Hex(char) → string

Hexadecimal string, zero-padded — 8 digits for int by default (or a custom digit count), 2 for byte, 4 for char.

Unhex

  • Unhex(string value) → int

Parses a hex string back into an int. Ignores an optional leading "0x"; returns 0 if the text isn't valid hex.

Binary

  • Binary(int)
  • Binary(int, digits)
  • Binary(byte)
  • Binary(char) → string

Binary string, zero-padded — 32 digits for int by default (or the rightmost digits bits), 8 for byte, 16 for char. Negative int values render their full two's-complement bit pattern, matching Processing/Java.

Unbinary

  • Unbinary(string value) → int

Parses a binary string back into an int. Anything other than '0'/'1' in the string makes the whole result 0; strings longer than 32 bits keep only the rightmost 32, matching int's width.

Prefer a plain C# cast. These exist purely for parity with sketches ported line-for-line from Processing. New DanaProcessing code can just write (int)value, value.ToString(), etc. directly.

Append<T> · Concat<T> · Expand<T>

  • Append<T>(array, value)
  • Concat<T>(a, b)
  • Expand<T>(array, newSize?) → T[]

Grows an array without mutating the original. Expand called with no size doubles the array (or grows an empty one to size 1) and never shrinks — matching Processing's own expand().

Reverse<T> · Shorten<T> · Subset<T>

  • Reverse<T>(array)
  • Shorten<T>(array)
  • Subset<T>(array, start, count?) → T[]

Returns a new, reordered or trimmed array. None of the array functions on this page mutate their input — the result is always a fresh array, matching Processing's own contract.

Sort<T>

  • Sort<T>(array)
  • Sort<T>(array, count) → T[]

Returns a new, ascending-sorted copy using T's natural ordering. The two-argument overload sorts only the first count elements.

Splice<T>

  • Splice<T>(array, value, index)
  • Splice<T>(array, insertion, index) → T[]

Inserts a single value, or every element of another array, starting at index.

ArrayCopy<T>

  • ArrayCopy<T>(src, dst)
  • ArrayCopy<T>(src, srcPos, dst, dstPos, count)

Copies elements into an already-sized destination array — the one function here that writes into an existing array rather than returning a new one, matching Processing's own arrayCopy().

Generic, not per-type. Processing needs a separate overload of each array function per primitive type (Java has no clean generics-over-arrays story). DanaProcessing needs just one <T> implementation, covering int[], float[], string[], PVector[], or anything else.

Join · Split

  • Join(array, separator) → string
  • Split(value, delimiter) → string[]

Split takes either a single char or a (possibly multi-character) string delimiter, matched literally.

SplitTokens

  • SplitTokens(value)
  • SplitTokens(value, delimiters) → string[]

Splits on runs of whitespace, or on runs of any character found in delimiters (each character is its own delimiter, not a multi-character sequence). Empty tokens between adjacent delimiters are dropped.

Trim

  • Trim(string)
  • Trim(string[])

Trims leading/trailing whitespace — a single string, or every element of an array.

Match · MatchAll

  • Match(value, regexp) → string[]?
  • MatchAll(value, regexp) → string[][]?

Regular-expression matching. Match returns the first match's groups (index 0 is the whole match); MatchAll returns one row of groups per match, non-overlapping. Both return null rather than an empty array when there's no match at all.

Nf

  • Nf(num, digits)
  • Nf(num, left, right) → string

Zero-pads a number to a fixed digit count — an int overload, and a float overload with separate widths for the digits before and after the decimal point.

Nfc

  • Nfc(num)
  • Nfc(num, right) → string

Same idea as Nf, with thousands separators added.

Nfp

  • Nfp(num, digits)
  • Nfp(num, left, right) → string

Same as Nf, but always shows a leading + or - sign.

Nfs

  • Nfs(num, digits)
  • Nfs(num, left, right) → string

Same as Nf, but pads with spaces instead of zeros.

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// Conversion and formatting helpers: Nf, Hex, Binary, Int, Float, Split, Join.
public class Utils : Sketch
{
    public override void Setup() => Size(520, 260);

    public override void Draw()
    {
        Background(250);
        Fill(30);
        int n = (int)MouseX;
        string[] parts = Split("red,green,blue", ',');
        string[] lines =
        {
$"MouseX = {n}",
$"Nf(n, 4)       = {Nf(n, 4)}",
$"Hex(n)         = {Hex(n)}",
$"Binary(n, 10)  = {Binary(n, 10)}",
$"Int(\"42\") + 1  = {Int("42") + 1}",
$"Float(\"2.5\")*2 = {Float("2.5") * 2}",
$"Split + Join   = {Join(parts, " | ")}",
$"Time           = {Nf(Hour(), 2)}:{Nf(Minute(), 2)}:{Nf(Second(), 2)}",
        };
        TextSize(15);
        for (int i = 0; i < lines.Length; i++)
Text(lines[i], 20, 30 + i * 28);
    }
}