Sorry, missunderstood the problem.
Here it goes:

fun <- function(x){
  res <- sapply(x, function(y){
    if(nchar(y) %% 8 != 0 || substr(y, 1, 1) == "0"){
      strtoi(y, base = 2)
    }else{
      y <- unlist(strsplit(y, ""))
      -sum((y != "1")*2^((length(y) - 1):0)) - 1
    }
  })
  unname(res)
}

fun("10110010")
fun("10000000")
fun(c("01000000", "01111111", "10110010", "10000000"))


Hope this helps,

Rui Barradas

Às 11:38 de 20/01/20, Rui Barradas escreveu:
Hello,

Is this what you want?


x <- "10110010"
strtoi(x, base = 2)
#[1] 178


Hope this helps,

Rui Barradas

Às 16:31 de 16/01/20, Paul Bernal escreveu:
Dear friends,

How can I convert the following binary number in two´s complement
representation in R?

10110010

Any help and/or guidance will be greatly appreciated,

Best regards,

Paul

    [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to