Hi, I am new user on R. I want to split a vector of hex data such as "00ff8020" "02d00000" "001e0240" "00010096" "00010033" into 16 bits such as: "00ff", "8020", "02d0","0000","001e", "0240", "0001","0096", "0001", "0033" Are there any way to do it? Thanks Frank [[alternative HTML version deleted]]
Duncan Murdoch
2015-Nov-22 16:38 UTC
[R] How to split 32 bits data into 16 bit or 8 bit data
On 22/11/2015 2:04 AM, Frank Wang wrote:> Hi, > > I am new user on R. I want to split a vector of hex data such as "00ff8020" > "02d00000" "001e0240" "00010096" "00010033" > into 16 bits such as: > "00ff", "8020", "02d0","0000","001e", "0240", "0001","0096", "0001", "0033" > > Are there any way to do it?If those are really strings, use substr(). If they are 32 bit integers, you could use x %% (2^16) for the low 16 bits. For the high 16 bits, you'll have to be careful about the sign. Or you could use the bitops package, bitShiftR(x, 16). In either case the results will display by default in decimal; if you want a hex display, use as.hexmode(). Duncan Murdoch
Thank you very much that substr works great on my data. For future users, I did in the following code. z<-matrix(c(substr(tmp,5,8),substr(tmp,1,4)),5,2) dataU16 <- c(t(z)) On Sun, Nov 22, 2015 at 8:38 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 22/11/2015 2:04 AM, Frank Wang wrote: > >> Hi, >> >> I am new user on R. I want to split a vector of hex data such as >> "00ff8020" >> "02d00000" "001e0240" "00010096" "00010033" >> into 16 bits such as: >> "00ff", "8020", "02d0","0000","001e", "0240", "0001","0096", "0001", >> "0033" >> >> Are there any way to do it? >> > > If those are really strings, use substr(). > > If they are 32 bit integers, you could use x %% (2^16) for the low 16 > bits. For the high 16 bits, you'll have to be careful about the sign. Or > you could use the bitops package, bitShiftR(x, 16). In either case the > results will display by default in decimal; if you want a hex display, use > as.hexmode(). > > Duncan Murdoch > >[[alternative HTML version deleted]]