[R] Converting binary number to in Two´s complement representation

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Jan 20 16:28:14 CET 2020


Hello,

The function I included converts signed binary numbers into their 
decimal representation. They are negative if a) they are multiples of 8 
bits and b) the most significant bit is a "1". If not just convert to 
integer.

As for a) above, I assume that you will have 8 bit numbers. And the 
conversion is done as follows:

input: 10110010

splitting, to make it more clear:

1 0 1 1 0 0 1 0 - input
0 1 0 0 1 1 0 1 - reversed
               1 - add 1 to the number with reversed bits
0 1 0 0 1 1 1 0 - result is the two's complement

c(0, 1, 0, 0, 1, 1, 1, 0) %*% 2^(7:0) is 78

But the msb is "1" so it's -78


This is what the function does, but instead of %*% it uses

sum(two's compl * powers of two)


Hope this helps,

Rui Barradas

The input must be a character string or character vector.

Às 14:36 de 20/01/20, Paul Bernal escreveu:
> Dear friend Rui,
> 
> Hope you are doing great, thanks for your kind feedback. The challenge I 
> currently have at hand is to decode AIS messages and obtain latitude and 
> longitude values from those.
> 
> So basically, I want to accomplish something like in the example below. 
> I want to convert this binary number (10110010) into the two´s 
> complement representation, there is the logic they are using for that. 
> Since longitude ranges from
> 
> 
>       Example of conversion to decimal of a signed binary number in
>       two's complement representation
> 
> Let's convert to decimal the following signed binary number: 10110010
> 
> 10110010 = -1×27 + 0×26 + 1×25 + 1×24 + 0×23 + 0×22 + 1×21 + 0×20 = -128 
> + 32 + 16 + 2 = -78.
> 
> El lun., 20 ene. 2020 a las 7:22, Rui Barradas (<ruipbarradas using sapo.pt 
> <mailto:ruipbarradas using sapo.pt>>) escribió:
> 
>     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 using r-project.org <mailto:R-help using 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 using r-project.org <mailto:R-help using 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.
>



More information about the R-help mailing list