swap bytes

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 5939
Joined: 19 Dec 2016, 01:58
Location: UK

swap bytes

10 Apr 2018, 22:04

- I've been preparing some function libraries to share. I thought I'd share this one particular function now.
- I had written a function to convert between RGB/BGR, ARGB/BGRA.
- I had also written a function to check if a colour had the an RGB value equal to its BGR value, i.e. the red and blue values matched. For use with an AutoHotkey converter.
- I decided however, that instead of a function for every occasion, I would try to write a general byte swap function.

Code: Select all

;MsgBox, % vBGR := Format("{:06X}", JEE_SwapBytesInt("0x" (vRGB:="AABBCC"), 1432)) ;CCBBAA
;MsgBox, % vBGR := Format("0x{:06X}", JEE_SwapBytesInt(vRGB:=0xAABBCC, 1432)) ;0xCCBBAA
;MsgBox, % vBGR := Format("0x{:06X}", JEE_SwapBytesInt(vRGB:=0xAABBCCDD, 1432)) ;0xAADDCCBB
;MsgBox, % vBGRA := Format("0x{:08X}", JEE_SwapBytesInt(vARGB:=0xAABBCCDD, 4321)) ;0xDDCCBBAA
;MsgBox, % Format("0x{:08X}", JEE_SwapBytesInt(0xAABBCCDDEE, 43)) ;0xEEDD
;MsgBox, % Format("0x{:08X}", JEE_SwapBytesInt(0xDDEE, 43)) ;0xEEDD
;MsgBox, % Format("0x{:08X}", JEE_SwapBytesInt(0xAABBCCDD, 1004)) ;0xAA0000DD
;MsgBox, % Format("0x{:08X}", JEE_SwapBytesInt(0xAABBCCDD, 1122)) ;0xAAAABBBB
;MsgBox, % Format("0x{:08X}", JEE_SwapBytesInt(0xAABBCCDD, 1212)) ;0xAABBAABB
;vOrder should be a number of between 0 and 4 characters
;vOrder should contain any of the digits 0/1/2/3/4, any number of times
;note: a vOrder of 43 is equivalent to a vOrder of "0043"
JEE_SwapBytesInt(vNum, vOrder)
{
	if (StrLen(vOrder) > 4)
		return
	Loop, % (StrLen(vOrder) - 4)
		vNum := "0" vNum
	vNum2 := 0
	Loop, Parse, vOrder
		if (A_LoopField = 0)
			vNum2 <<= 8
		else
			vNum2 <<= 8, vNum2 |= 0xFF & (vNum >> 8*(4-A_LoopField))
	return vNum2
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: robodesign and 26 guests