in reply to Add 1 to an arbitrary-length binary string

Here's a guess ...

#!/usr/bin/perl use strict; # https://www.perlmonks.org/?node_id=11155633 use warnings; use Data::Dump 'dd'; for ( 'aa', "a\xff", 'frequency/is/measured/in/hertz' ) { dd 'in', $_; my $onelarger = s/([^\xff])\xff*\z/ $1 =~ tr||\x01-\xff|cr /er; dd 'out', $onelarger; }

Outputs:

("in", "aa") ("out", "ab") ("in", "a\xFF") ("out", "b") ("in", "frequency/is/measured/in/hertz") ("out", "frequency/is/measured/in/hert{")

Replies are listed 'Best First'.
Re^2: Add 1 to an arbitrary-length binary string
by ikegami (Patriarch) on Nov 16, 2023 at 01:56 UTC

    61FF + 1 = 6200, not 62. (Second test)

      That would be true if all strings were hex numbers, but "/Some/Arbitrarily/long/string" (see Re^2: Add 1 to an arbitrary-length binary string) doesn't look like a hex number to me...

      Shouldn't  "61FF" + 1 = "61FG" ?

      If a set of test cases were provided, that could clarify the matter.

        Shouldn't "61FF" + 1 = "61FG" ?

        In short, the string "\x61\xFF" gets incremented to "\x62\x00" ... which seems a reasonable way to do it.
        Perhaps this is already clear (either implicitly or explicitly) from what others have provided to this thread.

        Cheers,
        Rob

        I'm not talking about the string produced by "61FF" but the string produced by "a\xFF", which is a weird and apparently confusing way of referring to the hex number 61FF.

      actually since this is a range bound, the truncation is fine
Re^2: Add 1 to an arbitrary-length binary string
by einhverfr (Friar) on Nov 16, 2023 at 02:24 UTC
    Thank you so much. This certainly gets me started.