Skip to content
Back to formatted view

Raw Message

Message-ID: <51B8597C.1030908@gmail.com>
Date: 2013-06-12T11:20:28Z
From: Duncan Murdoch
Subject: Bytes to Numeric/Float conversion
In-Reply-To: <CAPcYNaRc=uWSkkohJSMCfaD+7bNeDmB5vmnw6DNMRcPEJvr68Q@mail.gmail.com>

On 13-06-12 6:54 AM, Bikash Agrawal wrote:
> Actually I am using rJava, running some map-reduce using R.
> The data send is in bytes, but actually it is floating-point number.
>
> On R side, I need to convert this byte into Float. But I couldn't find any
> function.
> I am try using rawToChar() which convert into character. And later on
> as.double to convert it.
>
> But it is not giving the exact value.
> Could any one help me out.
>

If you want exact transfer from bytes, use raw connections.  For example:

 > con <- rawConnection(raw(0), "r+")
 > writeBin(pi, con)
 > rawConnectionValue(con)
[1] 18 2d 44 54 fb 21 09 40
 > seek(con,0)
[1] 8
 > readBin(con, "numeric")
[1] 3.141593

Depending on how you created the bytes, you may need to do some fiddling 
with the optional parameters size and endian of readBin to read them 
properly.

Duncan Murdoch