converting double value into bytes 
Author Message
 converting double value into bytes

Hi,

I have following code

Dim TempDbl as Double
Dim TempBuffer(3) as Byte

TempDbl = TempDbl * 4194304
TempBuffer(0) = TempDbl And &HFF
TempBuffer(1) = (TempDbl \ 256) And &HFF
TempBuffer(2) = (TempDbl \ 65536) And &HFF
TempBuffer(3) = (TempDbl \ 16777216) And &HFF

If TempDbl is 1024 then
TempDbl = 1024 * 4194304
then the above value when tried to convert into byte stream the
following statement
TempBuffer(0) = TempDbl And &HFF causes overflow.

How to overcome this? Pls help me

Thanks & Regards
Divakar



Tue, 21 Jun 2011 20:16:29 GMT  
 converting double value into bytes

Quote:

> Hi,

> I have following code

> Dim TempDbl as Double
> Dim TempBuffer(3) as Byte

> TempDbl = TempDbl * 4194304
> TempBuffer(0) = TempDbl And &HFF
> TempBuffer(1) = (TempDbl \ 256) And &HFF
> TempBuffer(2) = (TempDbl \ 65536) And &HFF
> TempBuffer(3) = (TempDbl \ 16777216) And &HFF

> If TempDbl is 1024 then
> TempDbl = 1024 * 4194304
> then the above value when tried to convert into byte stream the
> following statement
> TempBuffer(0) = TempDbl And &HFF causes overflow.

> How to overcome this? Pls help me

Your use of AND forces an implicit conversion to a Long data type,
which can't hold the range of values that a Double can. A Long can
effectively handle only 31 bits of value, because its 32nd bit is
reserved for sign. Your given values force the result to 33 bits,
which will never work.

There are other problems with your code, but it would be pointless to
discuss them because what you're striving for here can't be done.

--
    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com



Tue, 21 Jun 2011 20:38:21 GMT  
 converting double value into bytes

Quote:
> Hi,

> I have following code

> Dim TempDbl as Double
> Dim TempBuffer(3) as Byte

> TempDbl = TempDbl * 4194304
> TempBuffer(0) = TempDbl And &HFF
> TempBuffer(1) = (TempDbl \ 256) And &HFF
> TempBuffer(2) = (TempDbl \ 65536) And &HFF
> TempBuffer(3) = (TempDbl \ 16777216) And &HFF

> If TempDbl is 1024 then
> TempDbl = 1024 * 4194304
> then the above value when tried to convert into byte stream the
> following statement
> TempBuffer(0) = TempDbl And &HFF causes overflow.

> How to overcome this? Pls help me

First, Double uses 8 bytes, not 4. You can use CopyMemory to copy the bytes
to another variable. Example:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Form_Load()
    Dim TempDbl As Double
    Dim TempBuffer(0 To 7) As Byte
    Dim i As Long

    TempDbl = 123
    CopyMemory TempBuffer(0), TempDbl, 8

    For i = 0 To 7
        Debug.Print GetHex(TempBuffer(i), 2)
    Next
End Sub

Public Function GetHex(ByVal i As Long, ByVal HexWidth As Integer) As String
    GetHex = Right("00000000" & Hex(i), HexWidth)
End Function

http://en.wikipedia.org/wiki/Double_precision



Tue, 21 Jun 2011 21:50:43 GMT  
 converting double value into bytes

Quote:

> First, Double uses 8 bytes, not 4. You can use CopyMemory to copy
> the bytes to another variable. Example:

Except that it's clear from context that he doesn't want the literal
bytes, he wants the value of the double, mutiplied by some magic
number.

Since his magic number is 2^22 (shifts the value left by 22 bits),
that leaves just 9 bits of underlying value before he runs out of
Long.

--
    Jim Mack
    MicroDexterity Inc
    www.microdexterity.com



Wed, 22 Jun 2011 00:09:14 GMT  
 converting double value into bytes

Quote:
> Your use of AND forces an implicit conversion to a Long data type,
> which can't hold the range of values that a Double can. A Long can
> effectively handle only 31 bits of value, because its 32nd bit is
> reserved for sign. Your given values force the result to 33 bits,
> which will never work.

> There are other problems with your code, but it would be pointless to
> discuss them because what you're striving for here can't be done.

The OP question was not clear. I see now that 4194304 = &H400000. In that
case the OP could use a byte array instead and shift it, example:

Option Explicit

Private Sub Form_Load()

    Dim b(0 To 15) As Byte
    Dim i As Long

    b(0) = 5
    b(1) = &H81
    b(2) = &H43

    ShiftLeft b

    For i = 0 To UBound(b)
        Debug.Print GetHex(b(i), 2)
    Next

End Sub

Private Sub ShiftLeft(ByRef b() As Byte)
    Dim i As Long

    For i = UBound(b) To 1 Step -1
        ' Get the last bit from the previous byte
        If b(i - 1) >= &H80 Then
            ' Shift the byte, and set bit 0 to 1
            b(i) = (b(i) * 2&) And &HFF Or 1
        Else
            ' Shift the byte
            b(i) = (b(i) * 2&) And &HFF
        End If
    Next
    ' Last byte to process
    b(0) = (b(0) * 2&) And &HFF

End Sub

Public Function GetHex(ByVal i As Long, ByVal HexWidth As Integer) As String
    GetHex = Right("00000000" & Hex(i), HexWidth)
End Function

To the OP, you need to call ShiftLeft 22 times to accomplish what you want.



Wed, 22 Jun 2011 00:49:45 GMT  
 converting double value into bytes

Quote:

> The OP question was not clear. I see now that 4194304 = &H400000.
> In that case the OP could use a byte array instead and shift it,
> example:

> To the OP, you need to call ShiftLeft 22 times to accomplish what
> you want.

Sometimes we miss the forest for the trees. A left shift of 22 places
can be reduced to moving two whole byte positions higher (a shift of
16 bits), followed by a left shift of 6 bits.

Or, with suitable temps, moving 3 byte positions upwards (24 bits),
then shifting right two bits.

Either way, the lowest 22 bits (two bytes and change) will always be
zero, and the original upper 22 bits will be discarded. Whatever this
is for, it doesn't have much resolution in the end.

--
        Jim



Wed, 22 Jun 2011 01:05:20 GMT  
 converting double value into bytes

Quote:

>> The OP question was not clear. I see now that 4194304 = &H400000.
>> In that case the OP could use a byte array instead and shift it,
>> example:

>> To the OP, you need to call ShiftLeft 22 times to accomplish what
>> you want.

> Sometimes we miss the forest for the trees. A left shift of 22 places
> can be reduced to moving two whole byte positions higher (a shift of
> 16 bits), followed by a left shift of 6 bits.

> Or, with suitable temps, moving 3 byte positions upwards (24 bits),
> then shifting right two bits.

> Either way, the lowest 22 bits (two bytes and change) will always be
> zero, and the original upper 22 bits will be discarded. Whatever this
> is for, it doesn't have much resolution in the end.

I know that there are other ways to make the required shift, but I have a
feeling that the OP needs to use other than 22 bit shifts as well. Some
devices use bit fields to specify configuration options, so the OP probably
have to shift the bits in different values as well.


Wed, 22 Jun 2011 01:36:57 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Convert byte array to double? VB5.0

2. converting floating point dtaa from byte array to DOUBLE

3. how to use asp convert BIG5 ( Double bytes code ) to unicode (wml page )

4. Convert Byte Array To Double

5. Convert Byte Array to Double.

6. Convert Byte Array to Double using CopyMemory

7. Need to convert a 16 byte array of bytes to 4 byte integer

8. find double byte characters in a single byte string

9. Converting bytes stored in string to Single value

10. Read/Convert Byte Values from file

11. converting string containing binary double to double

12. How to convert 4 byte date to 8 byte date

 

 
Powered by phpBB® Forum Software