[Rd] R-devel: as.character() for hexmode no longer pads with zeros

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Thu Sep 23 09:46:35 CEST 2021


>>>>> Henrik Bengtsson 
>>>>>     on Wed, 22 Sep 2021 20:48:05 -0700 writes:

    > The update in rev 80946
    > (https://github.com/wch/r-source/commit/d970867722e14811e8ba6b0ba8e0f478ff482f5e)
    > caused as.character() on hexmode objects to no longer pads with zeros.

Yes -- very much on purpose; by me, after discussing a related issue
within R-core which showed "how wrong" the previous (current R)
behavior of the as.character() method is for
hexmode and octmode objects :

If you look at the whole rev 80946 , you also read NEWS

 * as.character(<obj>) for "hexmode" or "octmode" objects now
   fulfills the important basic rule

  as.character(x)[j] === as.character(x[j]) 
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

rather than just calling format().

The format() generic (notably for "atomic-alike" objects) should indeed
return a character vector where each string has the same "width",
however, the result of  as.character(x) --- at least for all
"atomic-alike" / "vector-alike" objects --
for a single x[j] should not be influenced by other elements in x.




    > Before:

    >> x <- structure(as.integer(c(0,8,16,24,32)), class="hexmode")
    >> x
    > [1] "00" "08" "10" "18" "20"
    >> as.character(x)
    > [1] "00" "08" "10" "18" "20"

    > After:

    >> x <- structure(as.integer(c(0,8,16,24,32)), class="hexmode")
    >> x
    > [1] "00" "08" "10" "18" "20"
    >> as.character(x)
    > [1] "0"  "8"  "10" "18" "20"

    > Was that intended?

Yes!
You have to explore your example a bit to notice how "illogical"
the behavior before was:

> as.character(as.hexmode(0:15))
 [1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f"
> as.character(as.hexmode(0:16))
 [1] "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "0a" "0b" "0c" "0d" "0e"
[16] "0f" "10"

> as.character(as.hexmode(16^(0:2)))
[1] "001" "010" "100"
> as.character(as.hexmode(16^(0:3)))
[1] "0001" "0010" "0100" "1000"
> as.character(as.hexmode(16^(0:4)))
[1] "00001" "00010" "00100" "01000" "10000"

all breaking the rule in the NEWS  and given above.

If you want format()  you should use format(),
but as.character() should never have used format() ..

Martin
    
    > /Henrik

    > PS. This breaks R.utils::intToHex()
    > [https://cran.r-project.org/web/checks/check_results_R.utils.html]



More information about the R-devel mailing list