Home > other >  How to add a dot and convert a number each two number
How to add a dot and convert a number each two number

Time:11-18

I have this number in R : C0290614 in hexadecimal.

If the input is in hexadecimal and the output is asked in hexadecimal, the output has to be : C0.29.06.14 (Just putting a dot each 2 numbers).

If the input is in hexadecimal and the output is asked in decimal, the output has to be : 192.41.6.20 (Convert in decimal two by two then put a dot at each number converted)

In resume :

Input : C0290614 16

Output : C0.29.06.14

Input : C0290614 10

Output : 192.41.6.20

Help me get the output please

To begin :

Convert <- function(addr, base) {
    if (base == 16) {
a <- as.integer(as.hexmode("C0"))
b <- as.integer(as.hexmode("29"))
c <- as.integer(as.hexmode("06"))
d <- as.integer(as.hexmode("14"))
cat(a, b , c, d)
        

    } else if (base == 10) {

    }


    return(paste0(addr, "-", base))
}

Convert(C0290614, 16)

CodePudding user response:

Converting hex to numeric

d <- "C0290614"

paste(as.numeric(as.hexmode(substring(
  d, seq(1, nchar(d), 2), seq(2, nchar(d), 2)))), collapse=".")
[1] "192.41.6.20"
  •  Tags:  
  • r
  • Related