Converting Decimal numbers into Binary

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting Decimal numbers into Binary

PaulJr
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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

R help mailing list-2

> 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

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

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

PaulJr
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=20

Hope 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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

Richard M. Heiberger
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

R help mailing list-2
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

Rui Barradas
In reply to this post by PaulJr
Hello,


Your code and the answers provided, specially Marc's, led me to


utf8ToBin <- function(x, out = c("ascii", "dec", "bin")){
   out <- match.arg(out)
   ascii_datformat <- utf8ToInt(x)
   Base <- ascii_datformat - 48
   Base <- ifelse(Base > 40, Base - 8, Base)
   Bin <- R.utils::intToBin(Base)
   switch (out,
     "ascii" = ascii_datformat,
     "dec" = Base,
     "bin" = Bin
   )
}

utf8ToBin("133m@", out = "ascii")
utf8ToBin("133m@", out = "dec")
utf8ToBin("133m@", out = "bin")


Hope this helps,

Rui Barradas

Às 16:30 de 27/12/19, Paul Bernal escreveu:

> 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=20
>
> Hope 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-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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

Rui Barradas
Hello,

You forgot to cc the list, I'm replying all to have a complete thread so
that others now and in the future can search similar problems they might
encounter.
The following function bin2dec works as expected.


bin2dec <- function(x){
   s <- strsplit(x, "")
   s <- lapply(s, function(x){
     sum(as.integer(x)*2^((length(x) - 1):0))
   })
   unlist(s)
}


bin2dec("001100001111010101000011011110")
#[1] 205344990

b <- utf8ToBin("133m@ogP00PD;88MD5MTDww@2D7k", out = "bin")
bin2dec(b)
# [1]  1  3  3 53 16 55 47 32  0  0 32 20 11  8  8 29 20  5 29 36
#[21] 20 63 63 16  2 20  7 51


Hope this helps,

Rui Barradas

Às 20:52 de 27/12/19, Paul Bernal escreveu:

>    Dear friend Rui,
>
> Hope you are doing well, thanks for the previous feedback,
>
> I have tried different things but have not been able to convert binary
> numbers back to decimal, to test if the output is  correct, I am taking
> the binary sequence = "001100001111010101000011011110" as a string, and
> this should give the following value (after converting back to decimal)
> = 205,344,990‬
>
> I have labeled my different attempts as (Attempt 1, ..., Attempt 3) but
> none have worked (see the code below):
>
> library(stringi)
> library(dplyr)
> library(R.utils)
>
>
> #dataset1 <- data.frame(maml.mapInputPort(1)) # class: data.frame
>
>
> ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k")
> ascii_datformat
>
> Base <- ascii_datformat - 48
>
> decy <- ifelse(Base > 40, Base-8, Base)
>
>
> biny <- intToBin(decy)
>
> binyframe <- data.frame(biny)
>
> tbinyframe <- paste(t(binyframe[,1]), collapse="")
>
> binyframe
> tbinyframe
>
> # Attempt 1
>
> bin2dec <- function(x)
> {
>    x <- as.character(as.numeric(x))
>    b <- as.numeric(unlist(strsplit(x, "")))
>    pow <- 2 ^ ((length(b) - 1):0)
>    sum(pow[b == 1])
> }
>
> # Attempt 2
> #bin2dec.easy <- function(binaryvector) {
> #  sum(2^(which(rev(binaryvector)==TRUE)-1))
> #}
>
> # Attempt 3
>
> #utf8ToBin <- function(x, out = c("ascii", "dec", "bin")){
> #   out <- match.arg(out)
> #   ascii_datformat <- utf8ToInt(x)
> #   Base <- ascii_datformat - 48
> #   Base <- ifelse(Base > 40, Base - 8, Base)
> #   Bin <- R.utils::intToBin(Base)
> #   switch (out,
> #     "ascii" = ascii_datformat,
> #     "dec" = Base,
> #     "bin" = Bin
> #   )
> #}
>
>
> z <- substr(tbinyframe, 9, 38)
>
> result <- bin2dec(z)
> result
>
> Any guidance will be greatly appreciated,
>
> Best regards,
>
> Paul
>
> El vie., 27 dic. 2019 a las 12:31, Rui Barradas (<[hidden email]
> <mailto:[hidden email]>>) escribió:
>
>     Hello,
>
>
>     Your code and the answers provided, specially Marc's, led me to
>
>
>     utf8ToBin <- function(x, out = c("ascii", "dec", "bin")){
>         out <- match.arg(out)
>         ascii_datformat <- utf8ToInt(x)
>         Base <- ascii_datformat - 48
>         Base <- ifelse(Base > 40, Base - 8, Base)
>         Bin <- R.utils::intToBin(Base)
>         switch (out,
>           "ascii" = ascii_datformat,
>           "dec" = Base,
>           "bin" = Bin
>         )
>     }
>
>     utf8ToBin("133m@", out = "ascii")
>     utf8ToBin("133m@", out = "dec")
>     utf8ToBin("133m@", out = "bin")
>
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Às 16:30 de 27/12/19, Paul Bernal escreveu:
>      > 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=20
>      >
>      > Hope 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] <mailto:[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] <mailto:[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] <mailto:[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] <mailto:[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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

Richard O'Keefe-2
In reply to this post by PaulJr
The specific problem you are trying to solve is so constrained that
you do not need a
general purpose method.
You start with a string that contains characters drawn from a *subset*
of ASCII with
at most 64 elements.  Accordingly, all you need is a table mapping characters to
6-character strings.
table <- c("8" = "001000", w = "111111", ...)
Then you just split your 28-character string into a character vector,
index the table with that vector, and paste the results together.

(By the way, at no point in the process do you have the least interest
in converting
anything to decimal.)

On Sat, 28 Dec 2019 at 05:31, Paul Bernal <[hidden email]> wrote:

>
> 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=20
>
> Hope 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-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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

PaulJr
In reply to this post by Rui Barradas
Thank you very much Rui.

Best regards!

El vie., 27 de diciembre de 2019 4:22 p. m., Rui Barradas <
[hidden email]> escribió:

> Hello,
>
> You forgot to cc the list, I'm replying all to have a complete thread so
> that others now and in the future can search similar problems they might
> encounter.
> The following function bin2dec works as expected.
>
>
> bin2dec <- function(x){
>    s <- strsplit(x, "")
>    s <- lapply(s, function(x){
>      sum(as.integer(x)*2^((length(x) - 1):0))
>    })
>    unlist(s)
> }
>
>
> bin2dec("001100001111010101000011011110")
> #[1] 205344990
>
> b <- utf8ToBin("133m@ogP00PD;88MD5MTDww@2D7k", out = "bin")
> bin2dec(b)
> # [1]  1  3  3 53 16 55 47 32  0  0 32 20 11  8  8 29 20  5 29 36
> #[21] 20 63 63 16  2 20  7 51
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 20:52 de 27/12/19, Paul Bernal escreveu:
> >    Dear friend Rui,
> >
> > Hope you are doing well, thanks for the previous feedback,
> >
> > I have tried different things but have not been able to convert binary
> > numbers back to decimal, to test if the output is  correct, I am taking
> > the binary sequence = "001100001111010101000011011110" as a string, and
> > this should give the following value (after converting back to decimal)
> > = 205,344,990‬
> >
> > I have labeled my different attempts as (Attempt 1, ..., Attempt 3) but
> > none have worked (see the code below):
> >
> > library(stringi)
> > library(dplyr)
> > library(R.utils)
> >
> >
> > #dataset1 <- data.frame(maml.mapInputPort(1)) # class: data.frame
> >
> >
> > ascii_datformat <- utf8ToInt("133m@ogP00PD;88MD5MTDww@2D7k")
> > ascii_datformat
> >
> > Base <- ascii_datformat - 48
> >
> > decy <- ifelse(Base > 40, Base-8, Base)
> >
> >
> > biny <- intToBin(decy)
> >
> > binyframe <- data.frame(biny)
> >
> > tbinyframe <- paste(t(binyframe[,1]), collapse="")
> >
> > binyframe
> > tbinyframe
> >
> > # Attempt 1
> >
> > bin2dec <- function(x)
> > {
> >    x <- as.character(as.numeric(x))
> >    b <- as.numeric(unlist(strsplit(x, "")))
> >    pow <- 2 ^ ((length(b) - 1):0)
> >    sum(pow[b == 1])
> > }
> >
> > # Attempt 2
> > #bin2dec.easy <- function(binaryvector) {
> > #  sum(2^(which(rev(binaryvector)==TRUE)-1))
> > #}
> >
> > # Attempt 3
> >
> > #utf8ToBin <- function(x, out = c("ascii", "dec", "bin")){
> > #   out <- match.arg(out)
> > #   ascii_datformat <- utf8ToInt(x)
> > #   Base <- ascii_datformat - 48
> > #   Base <- ifelse(Base > 40, Base - 8, Base)
> > #   Bin <- R.utils::intToBin(Base)
> > #   switch (out,
> > #     "ascii" = ascii_datformat,
> > #     "dec" = Base,
> > #     "bin" = Bin
> > #   )
> > #}
> >
> >
> > z <- substr(tbinyframe, 9, 38)
> >
> > result <- bin2dec(z)
> > result
> >
> > Any guidance will be greatly appreciated,
> >
> > Best regards,
> >
> > Paul
> >
> > El vie., 27 dic. 2019 a las 12:31, Rui Barradas (<[hidden email]
> > <mailto:[hidden email]>>) escribió:
> >
> >     Hello,
> >
> >
> >     Your code and the answers provided, specially Marc's, led me to
> >
> >
> >     utf8ToBin <- function(x, out = c("ascii", "dec", "bin")){
> >         out <- match.arg(out)
> >         ascii_datformat <- utf8ToInt(x)
> >         Base <- ascii_datformat - 48
> >         Base <- ifelse(Base > 40, Base - 8, Base)
> >         Bin <- R.utils::intToBin(Base)
> >         switch (out,
> >           "ascii" = ascii_datformat,
> >           "dec" = Base,
> >           "bin" = Bin
> >         )
> >     }
> >
> >     utf8ToBin("133m@", out = "ascii")
> >     utf8ToBin("133m@", out = "dec")
> >     utf8ToBin("133m@", out = "bin")
> >
> >
> >     Hope this helps,
> >
> >     Rui Barradas
> >
> >     Às 16:30 de 27/12/19, Paul Bernal escreveu:
> >      > 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=20
> >      >
> >      > Hope 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] <mailto:[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] <mailto:[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] <mailto:[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] <mailto:[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.
> >      >
> >
>

        [[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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Decimal numbers into Binary

PaulJr
In reply to this post by Richard O'Keefe-2
Thank you Richard.

Best regards,

Paul

El vie., 27 de diciembre de 2019 4:27 p. m., Richard O'Keefe <
[hidden email]> escribió:

> The specific problem you are trying to solve is so constrained that
> you do not need a
> general purpose method.
> You start with a string that contains characters drawn from a *subset*
> of ASCII with
> at most 64 elements.  Accordingly, all you need is a table mapping
> characters to
> 6-character strings.
> table <- c("8" = "001000", w = "111111", ...)
> Then you just split your 28-character string into a character vector,
> index the table with that vector, and paste the results together.
>
> (By the way, at no point in the process do you have the least interest
> in converting
> anything to decimal.)
>
> On Sat, 28 Dec 2019 at 05:31, Paul Bernal <[hidden email]> wrote:
> >
> > 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=20
> >
> > Hope 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-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]]

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