Page 1 of 1

Divide 4 bytes variable into individual bytes (VBS)

Posted: Fri 16. Dec 2011, 17:17
by howard
I have an virtual variable which is 4 bytes unsigned type
I have to put this value in a char array before transmitting via PCAN

Code: Select all

	Set TrimValue = Signals("Trim Value (Set)")
          
	Data(2) =  TrimValue.Value/16777216      '2^24 i used this because i think right shift does not work in            'PCAN
	Data(3) =  TrimValue.Value/65536           '2^16
	Data(4) =  TrimValue.Value/256              '2^8
        Data(2) =  TrimValue.Value & &HFF
But Data(2) keeps overflowing.

Please can anyone help me resolve this issue.

Re: Regarding PCAN Explorer Macro

Posted: Mon 19. Dec 2011, 10:28
by M.Maidhof
Hi,

to cut a 4byte value into bytes, you should use the integer division "\"

Code: Select all

Data(2) = (TrimValue.Value \ 16777216) And &HFF
Data(3) = (TrimValue.Value \ 65536) And &HFF
Data(4) = (TrimValue.Value \ 256) And &HFF
Data[2] was used a second time, sure you want do that, or is it just a typo?

Data(2) = TrimValue.Value & &HFF will not work in that way, it will add 255 to an existing value:
example: TrimValue.Value = 10 , the result of your line will be 10255 than.

here is a better solution:

Code: Select all

Data(2) = TrimValue.Value And &HFF
best regards

Michael