# Converting Decimal numbers into Binary

11 messages
Open this post in threaded view
|

## Converting Decimal numbers into Binary

 Dear friends, Hope you are all doing well. I need to find a way to convert ascii numbers to six digit binary numbers: I am working with this example, I converted the string to ascii, and finally to decimal, but I am having trouble converting the decimal numbers into their six digit binary representation. The code below is exactly what I have so far: ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k") ascii_datformat Base <- ascii_datformat - 48 ifelse(Base > 40, Base-8, Base) x <- rev(intToBits(Base)) dec2bin <- function(x) paste(as.integer(rev(intToBits(x))), collapse = "") dec2bin any guidance will be greatly appreciated, Best regards, Paul         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

 > On Dec 27, 2019, at 10:42 AM, Paul Bernal <[hidden email]> wrote: > > Dear friends, > > Hope you are all doing well. I need to find a way to convert ascii numbers > to six digit binary numbers: > > I am working with this example, I converted the string to ascii, and > finally to decimal, but I am having trouble converting the decimal numbers > into their six digit binary representation. The code below is exactly what > I have so far: > > ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k") > ascii_datformat > > Base <- ascii_datformat - 48 > > ifelse(Base > 40, Base-8, Base) > > x <- rev(intToBits(Base)) > dec2bin <- function(x) paste(as.integer(rev(intToBits(x))), collapse = "") > dec2bin > > any guidance will be greatly appreciated, > > Best regards, > > Paul You might look at the intToBin() function in Henrik's R.utils package on CRAN: https://cran.r-project.org/web/packages/R.utils/index.htmlRegards, Marc Schwartz ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

 In reply to this post by PaulJr Your question is incomplete... what do you expect the result to be? Perhaps [1] is relevant? [1] https://stackoverflow.com/questions/52298995/r-binary-decimal-conversion-confusion-ais-dataOn December 27, 2019 7:42:36 AM PST, Paul Bernal <[hidden email]> wrote: >Dear friends, > >Hope you are all doing well. I need to find a way to convert ascii >numbers >to six digit binary numbers: > >I am working with this example, I converted the string to ascii, and >finally to decimal, but I am having trouble converting the decimal >numbers >into their six digit binary representation. The code below is exactly >what >I have so far: > >ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k") >ascii_datformat > >Base <- ascii_datformat - 48 > >ifelse(Base > 40, Base-8, Base) > >x <- rev(intToBits(Base)) >dec2bin <- function(x) paste(as.integer(rev(intToBits(x))), collapse = >"") >dec2bin > >any guidance will be greatly appreciated, > >Best regards, > >Paul > > [[alternative HTML version deleted]] > >______________________________________________ >[hidden email] 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. -- Sent from my phone. Please excuse my brevity. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

 Dear Jeff, Hope you are doing great. The link I provide below has the results I am expecting. I am doing a test, trying to convert this string: "133m@ogP00PD ;88MD5MTDww@2D7k" into ascii numbers, then to decimal, and ultimately, into binary. I am trying to recreate the results obtained in the link below. http://www.it-digin.com/blog/?p=20Hope this answers your question. Thanks for any guidance you can provide, Cheers, Paul El vie., 27 dic. 2019 a las 11:18, Jeff Newmiller (<[hidden email]>) escribió: > Your question is incomplete... what do you expect the result to be? > > Perhaps [1] is relevant? > > [1] > https://stackoverflow.com/questions/52298995/r-binary-decimal-conversion-confusion-ais-data> > On December 27, 2019 7:42:36 AM PST, Paul Bernal <[hidden email]> > wrote: > >Dear friends, > > > >Hope you are all doing well. I need to find a way to convert ascii > >numbers > >to six digit binary numbers: > > > >I am working with this example, I converted the string to ascii, and > >finally to decimal, but I am having trouble converting the decimal > >numbers > >into their six digit binary representation. The code below is exactly > >what > >I have so far: > > > >ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k") > >ascii_datformat > > > >Base <- ascii_datformat - 48 > > > >ifelse(Base > 40, Base-8, Base) > > > >x <- rev(intToBits(Base)) > >dec2bin <- function(x) paste(as.integer(rev(intToBits(x))), collapse = > >"") > >dec2bin > > > >any guidance will be greatly appreciated, > > > >Best regards, > > > >Paul > > > >       [[alternative HTML version deleted]] > > > >______________________________________________ > >[hidden email] 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. > > -- > Sent from my phone. Please excuse my brevity. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

 In reply to this post by PaulJr Use the Rmpfr  package. it will print numbers in any base from 2  to 62 > library(Rmpfr) > ?Rmpfr > b15 <- mpfr(15, precBits=6) > formatBin(b15) [1] +0b1.11100p+3 > On Fri, Dec 27, 2019 at 10:43 AM Paul Bernal <[hidden email]> wrote: > > Dear friends, > > Hope you are all doing well. I need to find a way to convert ascii numbers > to six digit binary numbers: > > I am working with this example, I converted the string to ascii, and > finally to decimal, but I am having trouble converting the decimal numbers > into their six digit binary representation. The code below is exactly what > I have so far: > > ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k") > ascii_datformat > > Base <- ascii_datformat - 48 > > ifelse(Base > 40, Base-8, Base) > > x <- rev(intToBits(Base)) > dec2bin <- function(x) paste(as.integer(rev(intToBits(x))), collapse = "") > dec2bin > > any guidance will be greatly appreciated, > > Best regards, > > Paul > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

 In reply to this post by R help mailing list-2 Hi Paul, Since you start from strings, it's not clear to me where ASCII enters the picture. If you really need ASCII, you can use the charToInt() function in the "R.oo" package. Also there's the AsciiToInt() function in the  "sfsmisc" package. If you just want to use R's native as.numeric() conversion, there's the digitsBase() function in the "sfsmisc" package: > library(sfsmisc) > digitsBase(as.numeric("63"), base = 2) Class 'basedInt'(base = 2) [1:1]      [,1] [1,]    1 [2,]    1 [3,]    1 [4,]    1 [5,]    1 [6,]    1 > HTH, Bill. W. Michels, Ph.D. On Fri, Dec 27, 2019 at 8:11 AM Marc Schwartz via R-help <[hidden email]> wrote: > > > > On Dec 27, 2019, at 10:42 AM, Paul Bernal <[hidden email]> wrote: > > > > Dear friends, > > > > Hope you are all doing well. I need to find a way to convert ascii numbers > > to six digit binary numbers: > > > > I am working with this example, I converted the string to ascii, and > > finally to decimal, but I am having trouble converting the decimal numbers > > into their six digit binary representation. The code below is exactly what > > I have so far: > > > > ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k") > > ascii_datformat > > > > Base <- ascii_datformat - 48 > > > > ifelse(Base > 40, Base-8, Base) > > > > x <- rev(intToBits(Base)) > > dec2bin <- function(x) paste(as.integer(rev(intToBits(x))), collapse = "") > > dec2bin > > > > any guidance will be greatly appreciated, > > > > Best regards, > > > > Paul > > > You might look at the intToBin() function in Henrik's R.utils package on CRAN: > > https://cran.r-project.org/web/packages/R.utils/index.html> > Regards, > > Marc Schwartz > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

Open this post in threaded view
|

## Re: Converting Decimal numbers into Binary

Open this post in threaded view
|