swap bytes

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6841
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
}
Last edited by jeeswg on 02 Oct 2019, 13:20, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6841
Joined: 19 Dec 2016, 01:58
Location: UK

Re: swap bytes

02 Oct 2019, 13:19

- A NumSplit function might be a better idea. It takes the integer and creates an array with one item per byte.
- A 'NumJoin' function wouldn't be necessary, you could just use Format. Cheers.

Code: Select all

;q:: ;test number split / number join
;note: NumSplit creates an array of 8 items, bytes in LE order, by default

;RGB to/from BGR:
vNum := 0xAABBCC
oNum := NumSplit(vNum)
MsgBox, % Format("0x{:02X}{:02X}{:02X}", oNum*) ;0xCCBBAA (key 1: last, key 2: 2nd-to-last, key 3: 3rd-to-last)

;get nth byte:
vNum := 0xAABBCC
oNum := NumSplit(vNum)
MsgBox, % Format("0x{:X}", oNum[1]) ;0xCC (last)
MsgBox, % Format("0x{:X}", oNum[2]) ;0xBB (2nd-to-last)
MsgBox, % Format("0x{:X}", oNum[3]) ;0xAA (3rd-to-last)

;get nth byte (big endian):
vNum := 0xAABBCC
oNum := NumSplit(vNum, 3, "BE")
MsgBox, % Format("0x{:X}", oNum[1]) ;0xAA
MsgBox, % Format("0x{:X}", oNum[2]) ;0xBB
MsgBox, % Format("0x{:X}", oNum[3]) ;0xCC

;get nth byte (big endian):
vNum := 0xAABBCC
oNum := NumSplit(vNum,, "BE")
MsgBox, % Format("0x{:X}", oNum[6]) ;0xAA
MsgBox, % Format("0x{:X}", oNum[7]) ;0xBB
MsgBox, % Format("0x{:X}", oNum[8]) ;0xCC
return

;==================================================

;creates an array containing vCount items
NumSplit(vNum, vCount:=8, vOrder:="LE")
{
	local
	VarSetCapacity(vData, 8)
	NumPut(vNum, &vData, 0, "Int64")
	oArray := []
	if (vOrder = "LE") || (vOrder = "") ;start from leftmost bytes
	{
		Loop % vCount
			oArray.Push(NumGet(&vData, A_Index-1, "UChar"))
	}
	else if (vOrder = "BE") ;start from rightmost bytes
	{
		Loop % vCount
			oArray.Push(NumGet(&vData, vCount-A_Index, "UChar"))
	}
	return oArray
}

;==================================================
- [EDIT:] Perhaps I might consider a further parameter, to split the number into n bits/bytes. I.e. numbers corresponding to 1/2/4 bits, 1/2/4 bytes.
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: lovehack and 53 guests