Here's a simple way of converting DWORD to BYTES. You can change the MSB also (Most Significant Bit) using the MSB parameter flag (setting to 1) when calling the MKDWORD function.
The example takes in an RGB DWORD value as input and outputs bytes1-4 and various debug tests to validate the program so that it works as it should. I'll be writing the program equivalents in c+, purebasic and AGK.
I have used my original value2base function instead of the recursion version value2baser previously posted as it's not quite right and haven't yet got round to fixing it.
Rem Project: DWORD_to_BYTES
Rem Created: Tuesday, August 03, 2021
Rem ***** Main Source File *****
sync on : sync 30 : sync
dw as DWORD
dw=rgb(254,128,64)
print "DWORD (rgb(254,128,64)): ";dw
bn$=bin$(dw)
print "MSB-> ";bn$;" <-LSB"
b1=dword2byte(dw,1)
b2=dword2byte(dw,2)
b3=dword2byte(dw,3)
b4=dword2byte(dw,4)
print "byte 1: ";b1;", byte 2: ";b2;", byte 3: ";b3;", byte 4: ";b4
print "red ";rgbr(dw);", green ";rgbg(dw);", blue ";rgbb(dw);", alpha ";rgba(dw)
print "alpha ";rgba(dw);", red ";rgbr(dw);", green ";rgbg(dw);", blue ";rgbb(dw)
a1$=value2base(b1,2)
a2$=value2base(b2,2)
a3$=value2base(b3,2)
a4$=value2base(b4,2)
print b1;" -> ";a1$;", ";b2;" -> ";a2$;", ";b3;" -> ";a3$;", ";b4;" -> ";a4$
d1=bin2dec(a1$)
d2=bin2dec(a2$)
d3=bin2dec(a3$)
d4=bin2dec(a4$)
print a1$;" -> ";d1;", ";a2$;" -> ";d2;", ";a3$;" -> ";d3;", ";a4$;" -> ";d4
mdw as dword
mdw=mkdword(b1,b2,b3,b4,0)
print b1;" + ";b2;" + ";b3;" + ";b4;" -> ";mdw
mdw=mkdword(b4,b3,b2,b1,1)
print b4;" + ";b3;" + ";b2;"+ ";b1;" -> ";mdw
sync
wait key
end
// uses relative string end position based on number of chars to copy from start position e.g. spos = 8 , charnum = 8
// i.e. it calculates the string end position based on start position and the number of chars to copy
function Substr(s$, spos, chars)
l = len(s$)
epos = spos + chars-1 // end position equals the start char position plus number of chars minus 1
for c = 1 to l
if c=spos
r$=mid$(s$,c)
else
if c>spos and c<=epos
r$=r$+mid$(s$,c)
endif
endif
next c
endfunction r$
function value2base(v,base)
local b$, t$, rmd, v2
repeat
rmd = v mod base
v2 = v / base
`print v; ", ";v2; ", ";rmd;", base= ";base // debug only
v = v2
if base = 16
if rmd >=10 and rmd<=15
if rmd=10 then t$="A"
if rmd=11 then t$="B"
if rmd=12 then t$="C"
if rmd=13 then t$="D"
if rmd=14 then t$="E"
if rmd=15 then t$="F"
else
t$=str$(rmd)
endif
else
t$=str$(rmd)
endif
b$=b$+t$
until v<=0
for l=len(b$) to 1 step -1
res$=res$+mid$(b$,l)
next l
res$=padlchar$(res$,"0",8)
endfunction res$
function bin2dec(src$)
dim binary$(7)
for i=len(src$) to 1 step -1
binary$(len(src$)-i)=mid$(src$,i)
bn$=bn$+mid$(src$,i,1)
next i
decval=0
for b=0 to 7
if binary$(b)="1"
inc decval,2^b
endif
next b
undim binary$()
endfunction decval
// dw = input DWORD, byt = byte 1,2,3 or 4
// could put this in an array - keep simple for now
function Dword2Byte(dw as dword, byt)
if byt <1 or byt >4 then exitfunction 0
t$=bin$(dw)
tlen=len(t$)
b$=right$(t$,tlen-1)
if byt = 1
bn$=right$(b$,8)
else
if byt = 2
bn$=Substr(b$,17,8)
else
if byt = 3
bn$=Substr(b$,9,8)
else
if byt = 4
bn$=left$(b$,8)
endif
endif
endif
endif
b=bin2dec(bn$)
endfunction b
function padlchar$(src$,char$,num)
local t$
t$=""
for c=1 to num-len(src$)
t$=t$+char$
next
ret$=t$+src$
endfunction ret$
function padrchar$(src$,char$,num)
local t$
t$=""
for c=1 to num-len(src$)
t$=t$+char$
next
ret$=src$+t$
endfunction ret$
function mkdword(byte1,byte2,byte3,byte4,msb)
if msb <0 or msb>1 then exitfunction
if msb=0
dw = (byte1) || (byte2 << 8) || (byte3 << 16) || (byte4 << 24)
else
if msb=1
dw = (byte4) || (byte3 << 8) || (byte2 << 16) || (byte1 << 24)
endif
endif
endfunction dw
[update 2 - fixed recursion version of function value2baser -choose whatever function you prefer]
Rem Project: DWORD_to_BYTES
Rem Created: Tuesday, August 03, 2021
Rem Updated: Wednesday, August 04, 2021
Rem ***** Main Source File *****
sync on : sync 30 : sync
dw as DWORD
dw=rgb(254,128,64)
print "DWORD (rgb(254,128,64)): ";dw
bn$=bin$(dw)
print "MSB-> ";bn$;" <-LSB"
b1=dword2byte(dw,1)
b2=dword2byte(dw,2)
b3=dword2byte(dw,3)
b4=dword2byte(dw,4)
print "byte 1: ";b1;", byte 2: ";b2;", byte 3: ";b3;", byte 4: ";b4
print "red ";rgbr(dw);", green ";rgbg(dw);", blue ";rgbb(dw);", alpha ";rgba(dw)
print "alpha ";rgba(dw);", red ";rgbr(dw);", green ";rgbg(dw);", blue ";rgbb(dw)
`a1$=value2base(b1,2)
`a2$=value2base(b2,2)
`a3$=value2base(b3,2)
`a4$=value2base(b4,2)
a1$=value2baser(b1,2)
a2$=value2baser(b2,2)
a3$=value2baser(b3,2)
a4$=value2baser(b4,2)
print b1;" -> ";a1$;", ";b2;" -> ";a2$;", ";b3;" -> ";a3$;", ";b4;" -> ";a4$
d1=bin2dec(a1$)
d2=bin2dec(a2$)
d3=bin2dec(a3$)
d4=bin2dec(a4$)
print a1$;" -> ";d1;", ";a2$;" -> ";d2;", ";a3$;" -> ";d3;", ";a4$;" -> ";d4
mdw as dword
mdw=mkdword(b1,b2,b3,b4,0)
print b1;" + ";b2;" + ";b3;" + ";b4;" -> ";mdw
mdw=mkdword(b4,b3,b2,b1,1)
print b4;" + ";b3;" + ";b2;"+ ";b1;" -> ";mdw
sync
wait key
end
// uses relative string end position based on number of chars to copy from start position e.g. spos = 8 , charnum = 8
// i.e. it calculates the string end position based on start position and the number of chars to copy
function Substr(s$, spos, chars)
l = len(s$)
epos = spos + chars-1 // end position equals the start char position plus number of chars minus 1
for c = 1 to l
if c=spos
r$=mid$(s$,c)
else
if c>spos and c<=epos
r$=r$+mid$(s$,c)
endif
endif
next c
endfunction r$
function value2base(v,base)
local b$, t$, rmd, v2
repeat
rmd = v mod base
v2 = v / base
print v; ", ";v2; ", ";rmd;", base= ";base // debug only
v = v2
if base = 16
if rmd >=10 and rmd<=15
if rmd=10 then t$="A"
if rmd=11 then t$="B"
if rmd=12 then t$="C"
if rmd=13 then t$="D"
if rmd=14 then t$="E"
if rmd=15 then t$="F"
else
t$=str$(rmd)
endif
else
t$=str$(rmd)
endif
b$=b$+t$
until v<=0
for l=len(b$) to 1 step -1
res$=res$+mid$(b$,l)
next l
res$=padlchar$(res$,"0",8)
endfunction res$
function bin2dec(src$)
if len(src$) <=0 or len(src$)>8 then exitfunction 0
dim binary$(7)
for i=len(src$) to 1 step -1
binary$(len(src$)-i)=mid$(src$,i)
bn$=bn$+mid$(src$,i,1)
next i
decval=0
for b=0 to 7
if binary$(b)="1"
inc decval,2^b
endif
next b
undim binary$()
endfunction decval
// dw = input DWORD, byt = byte 1,2,3 or 4
// could put this in an array - keep simple for now
function Dword2Byte(dw as dword, byt)
if byt <1 or byt >4 then exitfunction 0
t$=bin$(dw)
tlen=len(t$)
b$=right$(t$,tlen-1)
if byt = 1
bn$=right$(b$,8)
else
if byt = 2
bn$=Substr(b$,17,8)
else
if byt = 3
bn$=Substr(b$,9,8)
else
if byt = 4
bn$=left$(b$,8)
endif
endif
endif
endif
b=bin2dec(bn$)
endfunction b
function padlchar$(src$,char$,num)
local t$
t$=""
for c=1 to num-len(src$)
t$=t$+char$
next
ret$=t$+src$
endfunction ret$
function padrchar$(src$,char$,num)
local t$
t$=""
for c=1 to num-len(src$)
t$=t$+char$
next
ret$=src$+t$
endfunction ret$
function mkdword(byte1,byte2,byte3,byte4,msb)
if msb <0 or msb>1 then exitfunction
if msb=0
dw = (byte1) || (byte2 << 8) || (byte3 << 16) || (byte4 << 24)
else
if msb=1
dw = (byte4) || (byte3 << 8) || (byte2 << 16) || (byte1 << 24)
endif
endif
endfunction dw
function value2baser(v,base)
a$=value2basermain$(v,base,"")
for l=len(a$) to 1 step -1
result$=result$+mid$(a$,l)
next l
result$=padlchar$(result$,"0",8)
endfunction result$
function value2basermain$(v,base,res$)
if v > 0
rmd = v mod base
v2 = v / base
`print v; ", ";v2; ", ";rmd
v = v2
if base = 16
if rmd >=10 and rmd <=15
if rmd=10 then t$="A"
if rmd=11 then t$="B"
if rmd=12 then t$="C"
if rmd=13 then t$="D"
if rmd=14 then t$="E"
if rmd=15 then t$="F"
else
t$=str$(rmd)
endif
else
t$=str$(rmd)
endif
res$=res$+t$
res$=value2basermain$(v,base,res$)
endif
endfunction res$
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others