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

Paul Bernal p@u|bern@|07 @end|ng |rom gm@||@com
Fri Jan 24 17:33:29 CET 2020


Dear friend Rui,

Hope you are doing great. Firstly, I want to thank you for your super
valuable and kind support of always. As I mentioned in earlier e-mails, I
am trying to decode AIS type messages, and the only ones I am having a real
hard time with, is with latitude and longitude.

I tried the function you provided me in one of your replies, and it works
well with the examples  you provided, but in other cases it doesn´t.

The messages I am trying to decode are in the 6th column of the data. I
will provide you with a small sample first, and then the complete dataset
(which has 100 rows). This is the small sample:

> head(dat)
    ...1 ...2 ...3 ...4 ...5                         ...6 ...7       ...8
...9 ...10 ...11 ...12 ...13
1 !AIVDM    1    1   NA    A 15?f5H?P00rCQat5:Oah0?wn2 using S6 0*54 1485907200
<NA>    NA    NA    NA  <NA>
2 !AIVDM    1    1   NA    A 1349B:3000rCtrn553aR using JH02PRp 0*39 1485907200
<NA>    NA    NA    NA  <NA>
3 !AIVDM    1    1   NA    A      D03Iuph1TNfp4dv9J<`N000 2*0D 1485907200
<NA>    NA    NA    NA  <NA>
4 !AIVDM    1    1   NA    A      D03Iu6QGLN01MdN01StN000 2*43 1485907200
<NA>    NA    NA    NA  <NA>
5 !AIVDO    1    1   NA <NA> B;s using N9h00>TtPEQAslh03wuUwP06 0*29 1485907200
<NA>    NA    NA    NA  <NA>
6 !AIVDM    1    1   NA    A 15A using av3P00rClHn53<I8M?v02<2B 0*2D 1485907200
<NA>    NA    NA    NA  <NA>

It is worth mentioning that each row of the 6th column provides several
information about maritime vessels, like speed over ground, latitude,
longitude, vessel ID, etc. I am only concerned with latitude and longitude
since those are the only two fields I have not been able to decode
successfully. Also, I am working on R version 3.6.2 for windows 64-bit OS.

The messages to decode are of the following format:
15?f5H?P00rCQat5:Oah0?wn2 using S6,  1349B:3000rCtrn553aR using JH02PRp, etc.

Now, here is the complete dataset:

> dput(dat)
structure(list(...1 = c("!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDO", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDO", "!AIVDM", "$GPRMC", "!AIVDM", "!AIVDM", "!AIVDM", "$GPGBS",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDM", "!AIVDO", "$GPRMC", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDM", "$GPGBS", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDO", "!AIVDM",
"!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM", "!AIVDM",
"!AIVDM", "$GPRMC", "!AIVDO", "!AIVDM", "!AIVDM"), ...2 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 50002, 1, 2, 2, 50002, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
1, 50006, 1, 1, 1, 1, 1, 50006, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 50010, 1,
1, 1), ...3 = c("1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "A", "1", "1", "2", "1.3", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "2", "1", "1", "1", "1",
"1", "1", "1", "2", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"2", "1", "A", "1", "1", "1", "1", "1", "1.3999999999999999",
"1", "2", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "2", "1", "1", "1", "1", "2", "1", "1", "1", "1", "1",
"1", "A", "1", "1", "1"), ...4 = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 856.96783, NA, 7, 7, 1.5, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 8, 8, NA, NA, NA, NA, NA, NA, 9, 9, NA, NA, NA,
NA, NA, NA, NA, NA, 0, 0, NA, 856.96805, NA, NA, NA, NA, NA,
1.5, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2,
2, NA, NA, NA, 3, 3, NA, NA, NA, NA, NA, NA, 856.96827, NA, NA,
NA), ...5 = c("A", "A", "A", "A", NA, "A", "A", "B", "B", "A",
"A", "A", "A", "B", "B", NA, "B", "A", "B", "A", "B", "B", "A",
"B", "A", NA, "B", "N", "B", "A", "A", "3.2000000000000002",
"B", "A", "A", NA, "A", "A", "A", "A", "B", "A", "A", NA, "B",
"A", "A", "A", "A", "A", "A", NA, "A", "A", "A", "B", "B", "B",
"B", "A", "A", NA, "N", "A", "A", "B", "B", "B", "3.2999999999999998",
"B", "B", "B", NA, "B", "B", "A", "B", "B", NA, "B", "B", "A",
"A", "B", "B", "A", NA, "B", "B", "B", "A", "A", "A", "A", "A",
"B", "N", NA, "B", "B"), ...6 = c("15?f5H?P00rCQat5:Oah0?wn2 using S6",
"1349B:3000rCtrn553aR using JH02PRp", "D03Iuph1TNfp4dv9J<`N000",
"D03Iu6QGLN01MdN01StN000",
"B;s using N9h00>TtPEQAslh03wuUwP06", "15A using av3P00rClHn53<I8M?v02<2B",
"1000Fo using P01rCuG<56bnkN?v004`0", "15QK900001JCq=d54l?5J0op0l4e",
"15 using eD@8000rC`bl59kW`mFn004`0", "15QIK`0P00rC`Sb59jFUUgv02<1g",
"403Iupiv4PU00rC9065>=fW00H0I", "403Iu6Qv4PU00rCk0d57rwW00<2g",
"H5?AU:4U653hhhi8 using lkihP000000", "15TGcJ0002rD<>p55FgmI using Ul0H0S",
"15Aq00?P00rC`a`59mFeogv004`0", "B;s using N9h00>TtPF1Asll03wP5wP06",
"13P;K8 using Oh1rCfgp58=ec1bD22<4J", "139NL4000LrCc8j59FEED4 using 000S<",
"403Iupiv4PU00rC9065>=fW00H0j", "39NS using m11@1rCrb:53:E<v0j3R000",
"403Iu6Qv4PU01rCk0d57rwW00<2g", "15PvE using 0002rCi7R57pokCT:424`0",
"15TgVb0000rCgVd57oFc31R42L46", "15PoOh0001rCgbt58CwUaBD004`0",
"A03IupkAC4:dH0v90>@P1cF6nud using NrP5wH5T", "B;s using N9h00>TtPF1Asll03wPUwP06",
"15E=q08P00JCrnR52cb>4?v200Sw", "7933.8836099999999",
"15>uP00P00rC`U:59im;H?v22 using 1D",
"54aMBf02:9M`?IDOH018DL4j085T000000000016>`Q8>4Oc0?@lRDm3hPC0",
"3", NA, "1018lEWP?w<tSF0l4Q@>4?wp0W3h", "15BkV00P00rCQBf5:Q5JQOv42D1o",
"35QN<D1000rCr5l53esbgPR20000", "B;s using N9h00>TtPF1Aslp03wQ5wP06",
"34`odN1000rD1V2537=dfPJ60000", "15>nNj0000rCT<@5::qUpkt604`0",
"15UHOn9P00rCQ`D5:OcTkgv80<4:", "35A=Rh1001rD;s454vSTuP`40000",
"15U?B00000rCgb>58DFJfRl620RT", "55B7iD42CSGC<H`WF20L5>1=@5:222222222221 using G
@W9K4Oi0D using PC0ShK40C",
"PC using H8888880", "B;s using N9h00>TtPFQAslt03wQUwP06",
"15De7F?P00JCr5r5517v4?v80h2P",
"15U using cn0000rCgU>57oPLGiT:2D4D", "137g`F8007rCaIj59Tc5Dl using 800SN",
"15?7P`0P00rD1S453KSlj?v824`0", "15?mqH?P00rCek458rkEN?v:00S4",
"55R4:002?H<`Q3S7GR1<lUB0 using 4pF22222222220l000004p60;0E7kBE8888",
"88888888880", "B;s using N9h00>TtPFQAslt03wR5wP06",
"15E:BR0P00rCgaT58DdJUwv82H34",
"15AIw`0P0GrCcO859DO5Ogv:0T`0", "14aMBf000wrCKKN5:sdU0Sv<083C",
"1819?@H001rC9TB5=bppM9<82D0T", "13M using Hk00jSJD@RD4s=qG1mT80 using 3J",
"15BI>P0001rCgUD58DRalRj:00S5", "100000?P00JCkt:583J=r?v:283Q",
"A03IupkAC4:dH0N90C9p0goOwj<Cw`P05A7vnh081wqU05DFwAKw<0?va using 1>",
"1gu00CLLwfh2 using Asw9@1<", "B;s using N9h00>TtPFQAsm003wRUwP06",
"7933.8835099999997",
"18K1kH000FrCSMt5:@;m>l8>0<4J", "19NSFKh000JCTeH5:7g1LT8:0`3g",
"15E=m60000rC`W459k28Wnd:083h", "1:u0KOh001rCq5P529qqubqh2 using 3n",
"13P>4mhw1CrCi5H57aK5WlN>0<4F", NA,
"A03IupkAC4:dH0N90D=p0goP02<Cw``05A7vnwt81wqVwUDFwAOt<0?vah1>",
"1gu103LLwfl1 using Asw9P1<", "14S8 using n001LrD?bH53iGe1rN>0 using 49",
"B;s using N9h00>TtPG1Asm403wSUwP06",

"15E:N using 0000rCgOd57p45bW><0<2H", "15 using EA<0P01JCo8l53=BFgwv using 0D47",
"10007NgP00rCQGV5:Pa=?gv>2<1H", "15TILd?P00JCm4l53`D>4?v>0L1m",
"19NSG<h003rCi@:57pmUkAB<0<1v", "B;s using N9h00>TtPG1Asm403wT5wP06",
"15Q69 using 8000rCiDr57n`bp using tB2<4R", "15QtF00000rCafD59P?VJ9p<0H52",
"15QCl@?P?w<tSF0l4Q@>4?wp0 using 5:", "15QDCP0P?w<tSF0l4Q@>4?wp0D1G",
"803Iu6PF15REPH3 using Dh000000000002c88I2P0002IrbQ0@40TW`800000000",
"HGp0772K07N4d1;0Pf71r0aj19RVmR19RVuR19RW5R19RW;t91Cjp31000C4",
"15D8Gj0P00rCThP5:6T=2Ov>0 using 5H", "B;s using N9h00>TtPG1Asm803wTUwP06",
"15ATk20000rCnrv53N6;gPr>085R",
"55AP::02 using VAlQ3G;7:1<lUB0MD4 using DhuE0F22220l0`G465k90<PlRDm3hPC8",

"88888888880", "15?lSL?P00JCQWD5:OpP0?vB24`0",
"15BW=20P00JCrvH54t=an?vB00Sg",
"13P;K8 using 001rCfgr58=f;QbFD2D4G", "A03IupkAC4:dH0v90FtP1cF6nud using NrP5wH5T",
"85E:BR0F0P0000000000032jS2P000000DE7P3A00h0", "1349B:3000rCtrn553aR using JHD2d4O",

"7933.8835099999997", "B;s using N9h00>TtPFQAsm803wU5wP06",
"15B3Sj0000rC9RD5=mOh40jB20SU",
"15TgVb0000rCgVb57oFc;ARF2 using 67"), ...7 = c("0*54", "0*39", "2*0D",
"2*43", "0*29", "0*2D", "0*27", "0*1D", "0*26", "0*48", "0*4B",
"0*3A", "0*34", "0*3A", "0*76", "0*0B", "0*4A", "0*72", "0*6B",
"0*4E", "0*38", "0*04", "0*11", "0*17", "0*18", "0*6B", "0*64",
"W", "0*53", "0*32", "2*20", NA, "0*61", "0*1C", "0*79", "0*16",
"0*44", "0*53", "0*04", "0*2D", "0*1C", "0*07", "2*37", "0*12",
"0*2D", "0*30", "0*6D", "0*25", "0*26", "0*75", "2*2D", "0*71",
"0*38", "0*6D", "0*0F", "0*34", "0*1D", "0*70", "0*47", "0*70",
"0*4C", "0*54", "W", "0*64", "0*66", "0*69", "0*0C", "0*70",
NA, "0*11", "0*28", "0*38", "0*30", "0*1F", "0*64", "0*7C", "0*38",
"0*67", "0*57", "0*59", "0*75", "0*52", "0*18", "0*08", "0*5F",
"0*26", "0*3B", "0*67", "0*6A", "2*24", "0*47", "0*65", "0*56",
"0*54", "2*76", "0*23", "W", "0*3B", "0*1D", "0*11"), ...8 = c(1485907200,
1485907200, 1485907200, 1485907200, 1485907200, 1485907200, 1485907200,
1485907200, 1485907200, 1485907200, 1485907200, 1485907200, 1485907200,
1485907201, 1485907201, 1485907201, 1485907201, 1485907201, 1485907201,
1485907201, 1485907201, 1485907201, 1485907201, 1485907201, 1485907201,
1485907201, 1485907201, 0.008, 1485907201, 1485907201, 1485907201,
NA, 1485907203, 1485907203, 1485907203, 1485907203, 1485907203,
1485907203, 1485907203, 1485907204, 1485907204, 1485907204, 1485907204,
1485907204, 1485907204, 1485907204, 1485907204, 1485907204, 1485907204,
1485907204, 1485907205, 1485907205, 1485907205, 1485907205, 1485907205,
1485907205, 1485907205, 1485907206, 1485907206, 1485907206, 1485907206,
1485907206, 0.01, 1485907206, 1485907206, 1485907206, 1485907206,
1485907206, NA, 1485907206, 1485907206, 1485907206, 1485907206,
1485907206, 1485907206, 1485907206, 1485907208, 1485907208, 1485907208,
1485907208, 1485907208, 1485907209, 1485907209, 1485907209, 1485907209,
1485907209, 1485907209, 1485907209, 1485907209, 1485907209, 1485907209,
1485907209, 1485907209, 1485907209, 1485907209, 1485907209, 0.005,
1485907209, 1485907209, 1485907209), ...9 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "*41", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, "*43", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), ...10 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
10217, NA, NA, NA, 1485907201, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 10217, NA, NA, NA, NA, NA, 1485907206,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10217, NA, NA, NA
), ...11 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), ...12 = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
    ...13 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    "D*6F", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, "D*60", NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "D*63", NA, NA,
    NA), ...14 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, 1485907201, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, 1485907206, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1485907209,
    NA, NA, NA)), row.names = c(NA, -100L), class = "data.frame")

To tested your function I took the first message, which is located in the
6th column and the 1st row, and did the following:

library(stringi)
library(dplyr)
library(R.utils)
library(RANN)
library(NISTunits)
library(pracma)
library(celestial)
library(stringr)

dat <- readXL("U:/RawSampleData.xls", rownames=FALSE, header=FALSE, na="",
+   sheet="RawSampleData", stringsAsFactors=FALSE)

testmessage1 <- dat[1,6]

ascii_datformat <- utf8ToInt(testmessage1)

Base <- ascii_datformat - 48

decy <- ifelse(Base > 40, Base - 8, Base)

biny <- intToBin(decy)

binyframe <- data.frame(biny)

tbinyframe <- paste(t(binyframe[,1]), collapse="")  #at this point, I have
the complete first message, all in binary format

#according to the literature of AIS message decoding, longitude goes from
position 62 to position 89
#and latitude goes from position 90 to position 116

longitude <- substr(tbinyframe, 62, 89)
latitude    <- substr(tbinyframe, 90, 116)

#now I apply the function you provided me with:

 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(longitude)
[1] 220663102
>
> fun(latitude)
[1] 5414823
>
> fun("1101001001110000110100111110")
[1] 220663102
>
> fun("000010100101001111110100111")
[1] 5414823
>
> fun("10110010")
[1] -78

as you can see, the function only worked or showed expected result on the
last case with a -78, but in the other cases, it the results were not as
expected, maybe I am missing something here?

Any help and/or guidance will be greatly appreciated,

Best regards,

Paul

El lun., 20 ene. 2020 a las 10:28, Rui Barradas (<ruipbarradas using sapo.pt>)
escribió:

> 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.
> >
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list