Home All Groups Group Topic Archive Search About

Help with a bit of maths

Author
23 May 2009 6:14 PM
Ivar
Hi all.

A little bit of code below, two Qs here:
What maths (without API) goes on to make the 4 bytes = -455290571
What maths is needed to make the 4 bytes back to the values coded from a
long var of  -455290571
Could someone demo how to make the bytes = -455290571 and Vice-Versa

Thank you

Ivar

Dim SomeBytes(3) As Byte
Dim LongVar As Long
SomeBytes(0) = 53
SomeBytes(1) = 209
SomeBytes(2) = 220
SomeBytes(3) = 228
Dim X As Long
'How to make X = -455290571 ?
Open "C:\MyTestFile" For Binary Access Write As #1
Put #1, , SomeBytes
Close #1
Open "C:\MyTestFile" For Binary Access Read As #1
Get #1, , LongVar ' = -455290571
Close #1
SomeBytes(0) = 0
SomeBytes(1) = 0
SomeBytes(2) = 0
SomeBytes(3) = 0
SomeBytes(0) = LongVar And &HFF
'How to make SomeBytes() = original values?

Author
23 May 2009 8:51 PM
Henning
Show quote Hide quote
"Ivar" <Ivar.ekstromer***@ntlworld.com> skrev i meddelandet
news:U1XRl.81212$iK7.72790@newsfe08.ams2...
> Hi all.
>
> A little bit of code below, two Qs here:
> What maths (without API) goes on to make the 4 bytes = -455290571
> What maths is needed to make the 4 bytes back to the values coded from a
> long var of  -455290571
> Could someone demo how to make the bytes = -455290571 and Vice-Versa
>
> Thank you
>
> Ivar
>
> Dim SomeBytes(3) As Byte
> Dim LongVar As Long
> SomeBytes(0) = 53
> SomeBytes(1) = 209
> SomeBytes(2) = 220
> SomeBytes(3) = 228
> Dim X As Long
> 'How to make X = -455290571 ?
> Open "C:\MyTestFile" For Binary Access Write As #1
> Put #1, , SomeBytes
> Close #1
> Open "C:\MyTestFile" For Binary Access Read As #1
> Get #1, , LongVar ' = -455290571
> Close #1
> SomeBytes(0) = 0
> SomeBytes(1) = 0
> SomeBytes(2) = 0
> SomeBytes(3) = 0
> SomeBytes(0) = LongVar And &HFF
> 'How to make SomeBytes() = original values?
>
>

Bytes to long is easy
Dim txt As String
  txt = "&H" & Hex$(SomeBytes(3)) & Hex$(SomeBytes(2)) & Hex$(SomeBytes(1))
& Hex$(SomeBytes(0))
  Debug.Print txt
  X = 0& + Val(txt)  '&HE4DCD153

The other way around might be done by making it the correct positive DEC
number.

/Henning
Author
23 May 2009 9:32 PM
Mike Williams
"Ivar" <Ivar.ekstromer***@ntlworld.com> wrote in message
news:U1XRl.81212$iK7.72790@newsfe08.ams2...

> A little bit of code below, two Qs here:
> What maths (without API) goes on to make the 4 bytes = -455290571
> What maths is needed to make the 4 bytes back to the values coded from a
> long var of  -455290571
> Could someone demo how to make the bytes = -455290571 and Vice-Versa

You can use rtlMoveMemory to copy the raw data from a Long to four Bytes and
vice versa, but you can also do the same without using the API by setting
the Long and the four Bytes as elements of UDTs and then copying the raw
data over by assigning one UDT to the other. Here is an example:

Mike

Option Explicit
Private Type FourBytes
  dta(0 To 3) As Byte
End Type
Private Type OneLong
  dta As Long
End Type

Private Sub Command1_Click()
Dim a As FourBytes, b As OneLong, c As FourBytes
'
a.dta(0) = 53
a.dta(1) = 209
a.dta(2) = 220
a.dta(3) = 228
LSet b = a
Print b.dta
'
b.dta = -455290571
LSet a = b
Print a.dta(0)
Print a.dta(1)
Print a.dta(2)
Print a.dta(3)
'
End Sub
Author
23 May 2009 10:40 PM
Henning
Show quote Hide quote
"Mike Williams" <M***@WhiskyAndCoke.com> skrev i meddelandet
news:ONYwX4%232JHA.3476@TK2MSFTNGP05.phx.gbl...
> "Ivar" <Ivar.ekstromer***@ntlworld.com> wrote in message
> news:U1XRl.81212$iK7.72790@newsfe08.ams2...
>
>> A little bit of code below, two Qs here:
>> What maths (without API) goes on to make the 4 bytes = -455290571
>> What maths is needed to make the 4 bytes back to the values coded from a
>> long var of  -455290571
>> Could someone demo how to make the bytes = -455290571 and Vice-Versa
>
> You can use rtlMoveMemory to copy the raw data from a Long to four Bytes
> and vice versa, but you can also do the same without using the API by
> setting the Long and the four Bytes as elements of UDTs and then copying
> the raw data over by assigning one UDT to the other. Here is an example:
>
> Mike
>
> Option Explicit
> Private Type FourBytes
>  dta(0 To 3) As Byte
> End Type
> Private Type OneLong
>  dta As Long
> End Type
>
> Private Sub Command1_Click()
> Dim a As FourBytes, b As OneLong, c As FourBytes
> '
> a.dta(0) = 53
> a.dta(1) = 209
> a.dta(2) = 220
> a.dta(3) = 228
> LSet b = a
> Print b.dta
> '
> b.dta = -455290571
> LSet a = b
> Print a.dta(0)
> Print a.dta(1)
> Print a.dta(2)
> Print a.dta(3)
> '
> End Sub
>
>

Nice to learn something useful.
Thx!

/Henning
Author
24 May 2009 6:09 AM
Ivar
Thanks to Mike and Henning for the replies so far.
This is not so much a how to do Q, as a how is it done Q.
VB is full of functions, then there's all those APIs, what wonderful things
that do magic. But! How do they do them. What happens behind the function.
Have you ever wrote some complex function that works just as you want it
after a lot of trial and error, only to find out later that an API exists
that does exactly the same thing? In that respect you would already
understand how the function works. With this particular Q I already know how
to do what I want by using some APIs, but how does it work?
With the two responses I have had so far they both call other functions
(Hex,Lset etc) but what i'm looking for is the maths behind the function,
example would be the following code snippet:

Dim R As Long, G As Long, B As Long
Dim TheColourA As Long
Dim TheColourB As Long
R = 50
G = 100
B = 150
TheColourA = RGB(R, G, B)

TheColourB = R + (G * 256)
TheColourB = TheColourB + ((B * 256) * 256)

TheColourA and TheColourB end up as the same value, ColourA is the result of
a VB function, TheColourB is the result of a maths calculation. The point of
my Q is to try to understand the maths that goes on.
So, just using the basic maths operators, can anyone duplicate the maths
that happens in the original Q.

Thanks for reading.

Ivar
Author
24 May 2009 11:11 AM
Jim Mack
Ivar wrote:
Show quoteHide quote
> Thanks to Mike and Henning for the replies so far.
> This is not so much a how to do Q, as a how is it done Q.
> VB is full of functions, then there's all those APIs, what
> wonderful things that do magic. But! How do they do them. What
> happens behind the function. Have you ever wrote some complex
> function that works just as you want it after a lot of trial and
> error, only to find out later that an API exists that does exactly
> the same thing? In that respect you would already understand how
> the function works. With this particular Q I already know how to do
> what I want by using some APIs, but how does it work? With the two
> responses I have had so far they both call other functions
> (Hex,Lset etc) but what i'm looking for is the maths behind the
> function, example would be the following code snippet:
>
> Dim R As Long, G As Long, B As Long
> Dim TheColourA As Long
> Dim TheColourB As Long
> R = 50
> G = 100
> B = 150
> TheColourA = RGB(R, G, B)
>
> TheColourB = R + (G * 256)
> TheColourB = TheColourB + ((B * 256) * 256)
>
> TheColourA and TheColourB end up as the same value, ColourA is the
> result of a VB function, TheColourB is the result of a maths
> calculation. The point of my Q is to try to understand the maths
> that goes on. So, just using the basic maths operators, can anyone
> duplicate the maths that happens in the original Q.

You're assuming that it's essentially a maths question, but it isn't.
You can apply some basic arithmetic and arrive at the result you want,
but you can approach it from other directions and do the same, as
shown.

In other words, this is a domain-shifting problem that permits an
arithmetic solution -- among others. And in this case, the nature of
2's complement representation makes arithmetic an inferior solution.

--
   Jim Mack
   Twisted tees at http://www.cafepress.com/2050inc
   "We sew confusion"
Author
24 May 2009 11:59 AM
Henning
Show quote Hide quote
"Ivar" <Ivar.ekstromer***@ntlworld.com> skrev i meddelandet
news:aw5Sl.187055$i%2.111800@newsfe15.ams2...
> Thanks to Mike and Henning for the replies so far.
> This is not so much a how to do Q, as a how is it done Q.
> VB is full of functions, then there's all those APIs, what wonderful
> things that do magic. But! How do they do them. What happens behind the
> function. Have you ever wrote some complex function that works just as you
> want it after a lot of trial and error, only to find out later that an API
> exists that does exactly the same thing? In that respect you would already
> understand how the function works. With this particular Q I already know
> how to do what I want by using some APIs, but how does it work?
> With the two responses I have had so far they both call other functions
> (Hex,Lset etc) but what i'm looking for is the maths behind the function,
> example would be the following code snippet:
>
> Dim R As Long, G As Long, B As Long
> Dim TheColourA As Long
> Dim TheColourB As Long
> R = 50
> G = 100
> B = 150
> TheColourA = RGB(R, G, B)
>
> TheColourB = R + (G * 256)
> TheColourB = TheColourB + ((B * 256) * 256)
>
> TheColourA and TheColourB end up as the same value, ColourA is the result
> of a VB function, TheColourB is the result of a maths calculation. The
> point of my Q is to try to understand the maths that goes on.
> So, just using the basic maths operators, can anyone duplicate the maths
> that happens in the original Q.
>
> Thanks for reading.
>
> Ivar
>

I'm not sure what your'e asking, but this is the case. I also guess you
already know this.

A long is made of 4 bytes, 8 bits each. Unfortunately there is no
UnsignedLong in VB, and that confuses things a little. In the original post
you have the 4 bytes and the resulting negative long value, because the
highest bit is the signbit. Below I therefor convert the Long to a Dec data
type to keep the value positive.

  Dim txt As String
  Dim lArr(4) As Byte
  Dim tmpLong As Long
  Dim tmpArr(4) As Byte
  Dim myDec

  'the byte values and the hex value they represent
  lArr(3) = 228 '&HE4
  lArr(2) = 220 '&HDC
  lArr(1) = 209 '&HD1
  lArr(0) = 53  '&H35

  'the byte's hex values are rearranged in a string (&HE4DCD135)
  txt = "&H" & Hex$(lArr(3)) & Hex$(lArr(2)) & Hex$(lArr(1)) & Hex$(lArr(0))
  Debug.Print txt

  'because the highest bit in &HE4 is a '1' the Long result is negative
  myLong = 0& + Val(txt)  '&HE4DCD135
  Debug.Print CStr(myLong)


  'now reconstruct the Long value to (the same bitmap) but positive Dec
value
  myDec = CDec(4294967296#) + myLong

  'next represents shifting 24 bits right.
  'the Int is needed to avoid rounding up. The '\' divide can't be used due
to overflow.
  tmpLong = 0& + Int(myDec / 16777216#)
  tmpArr(3) = tmpLong
  Debug.Print tmpArr(3)

  'now (remove) the highbyte value
  myDec = myDec - 16777216# * tmpLong

  'now shift right 16 bits, and now we can use the '\' divide
  tmpLong = 0& + myDec \ 65536
  tmpArr(2) = tmpLong
  Debug.Print tmpArr(2)

  'now get rid of the next to highest byte
  myDec = myDec - 65536 * tmpLong

  'the next to lowest byte shift right 8 bits
  tmpLong = 0& + myDec \ 256
  tmpArr(1) = tmpLong
  Debug.Print tmpArr(1)

  'keep only the rest as the lowest byte
  myDec = myDec - 256 * tmpLong
  tmpArr(0) = myDec
  Debug.Print tmpArr(0)

Shifting / dividing is easier seen in binary, comma for clarity.

Hex E4, DC, D1, 35
Bin 11100100, 11011100, 11010001, 00110101
Multiply by 256 is the same as shift left 8 bits
Divide by 256 is the same as shift right 8 bits
Shifting right 24 bits "moves" the highest byte to the lowest.
Shifted 00000000, 00000000, 00000000, 11100100

/Henning
Author
24 May 2009 4:38 PM
Ivar
Now that's the kind'a answer I was looking for :-)
It's a bit of a shame that VB dosn't have a 32bit unsigned datatype, it
would have made this a bit easier. With the aid of your explaination I have
managed to do these calcs only using maths operators without the VB
functions as statments (Hex$ etc)
Now the point of the original Q has been answered.
Not What or how to do, but how does the function or statement do what it
does.

Many thanks

Ivar
Author
24 May 2009 10:00 PM
Henning
Show quote Hide quote
"Ivar" <Ivar.ekstromer***@ntlworld.com> skrev i meddelandet
news:qKeSl.73408$qP7.53152@newsfe04.ams2...
> Now that's the kind'a answer I was looking for :-)
> It's a bit of a shame that VB dosn't have a 32bit unsigned datatype, it
> would have made this a bit easier. With the aid of your explaination I
> have managed to do these calcs only using maths operators without the VB
> functions as statments (Hex$ etc)
> Now the point of the original Q has been answered.
> Not What or how to do, but how does the function or statement do what it
> does.
>
> Many thanks
>
> Ivar
>
Thx, glad it helped.
And yes, one of the things I miss most in VB is unsigned types.

/Henning
Author
26 May 2009 5:14 AM
Bill McCarthy
Show quote Hide quote
"Henning" <computer_h***@coldmail.com> wrote in message
news:ObopEsL3JHA.1432@TK2MSFTNGP02.phx.gbl...
>
> "Ivar" <Ivar.ekstromer***@ntlworld.com> skrev i meddelandet
> news:qKeSl.73408$qP7.53152@newsfe04.ams2...
>> Now that's the kind'a answer I was looking for :-)
>> It's a bit of a shame that VB dosn't have a 32bit unsigned datatype, it
>> would have made this a bit easier. With the aid of your explaination I
>> have managed to do these calcs only using maths operators without the VB
>> functions as statments (Hex$ etc)
>> Now the point of the original Q has been answered.
>> Not What or how to do, but how does the function or statement do what it
>> does.
>>
>> Many thanks
>>
>> Ivar
>>
> Thx, glad it helped.
> And yes, one of the things I miss most in VB is unsigned types.
>

Which of course are in modern day VB ;)
7.1 I think it was .
Author
26 May 2009 9:32 AM
Cor Ligthert[MVP]
> 7.1 I think it was .

Not earlier then 8

Cor
Author
26 May 2009 9:45 AM
Mike Williams
"Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> wrote in message
news:e8GnFUe3JHA.1712@TK2MSFTNGP03.phx.gbl...

>> 7.1 I think it was .
>
> Not earlier then 8

.. . . and here comes Ligthert, the other disgusting little dotnet
evangelist, the pervert with his head on upside down.

Mike
Author
26 May 2009 10:08 AM
Henning
"Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> skrev i meddelandet
news:e8GnFUe3JHA.1712@TK2MSFTNGP03.phx.gbl...
>> 7.1 I think it was .
>
> Not earlier then 8
> Cor

Sorry, but your'e both too late again. That is already explained in another
threed (Bloat...).

/Henning
Author
26 May 2009 9:42 AM
Mike Williams
"Bill McCarthy" <Bill McCarthy is a Liar and an Identity Thief> wrote in
message news:OmkNLGc3JHA.4872@TK2MSFTNGP04.phx.gbl...

> Which of course are in modern day VB ;)
> 7.1 I think it was .

Piss off, McCarthy. Stop spewing out your evangelistic dotnet drivel here.
And while you're at it get rid of the disgusting lies in your fake email
address, lies which I have edited in this response to make them more
truthful. You really are a disgusting little turd.

Mike