MadSeb
MadSeb

Reputation: 8254

R cast string to raw?

How do I convert from "character" to "raw"?

class ( plot_binary  )
# [1] "raw"
plot_binary[3]
# [1] 4e
class ( plot_binary[3] ) 
# [1] "raw"

I want to do a conversion from a character "4e" to a raw 4e.

How do I do this?

as.raw obviously not work because

as.raw("4e")
# [1] 04

Upvotes: 3

Views: 2640

Answers (3)

RHex
RHex

Reputation: 11

R accepts hexadecimal numeric input prefixed with 0x.

> as.raw(0x4e)
[1] 4e

Upvotes: 1

MadSeb
MadSeb

Reputation: 8254

Found a solution:

as.raw ( as.hexmode ( "4e" ) )
#[1] 4e

Upvotes: 8

Jeroen Ooms
Jeroen Ooms

Reputation: 32988

Try looking at: rawToChar and charToRaw

Upvotes: 5

Related Questions