swap bytes

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
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: 6902
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
laozhhaiJohn
Posts: 24
Joined: 20 Mar 2021, 18:18

Re: swap bytes

01 Apr 2021, 00:24

THanks.非常好的办法!最近一个大端模式和小端模式的问题也困扰了我很长时间。

[Mod edit: Added translation.]
Very good solution! A recent problem with the big end mode and the small end mode has also been bothering me for a long time.
gregster
Posts: 9014
Joined: 30 Sep 2013, 06:48

Re: swap bytes

01 Apr 2021, 01:47

@laozhhaiJohn: Since it doesn't seem to be obvious, please use English in the general (English) parts of these forums.
You can use Google Translate, DeepL or something similar to translate your message, but please make an attempt to do so in future posts. Thank you!
laozhhaiJohn
Posts: 24
Joined: 20 Mar 2021, 18:18

Re: swap bytes

01 Apr 2021, 04:38

gregster wrote:
01 Apr 2021, 01:47
@laozhhaiJohn: Since it doesn't seem to be obvious, please use English in the general (English) parts of these forums.
You can use Google Translate, DeepL or something similar to translate your message, but please make an attempt to do so in future posts. Thank you!
ok.My english is poor.sorry.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: FanaticGuru and 146 guests