Private Function r_8(value As UInt8, endian As Integer) As UInt8
if (endian)=1 then
return Bitwise.BitOr(Bitwise.ShiftLeft(value,8), Bitwise.ShiftRight(value,8))
else
return value
end if
End Function
translate to this this in Xojo:
return CType(Bitwise.BitOr(Bitwise.ShiftLeft(Bitwise.BitAnd(value, 0xFFU), 8), Bitwise.ShiftRight(Bitwise.BitAnd(value, 0xFF00U),8)), UInt16)
Value is an unsigned 8 bit integer. Shifting it left or right 8 bits zeros it.
It looks to me like C++ code was intended to swap the bytes in case the endian(ism) was incorrect.
but…
This function either returns 0 or the original value depending on endian(ism).
As Brian already pointed out, the original C code is quite some nonsense.
It takes and returns a “uint8_t” type, which reduces the value to 8 bit. The proposed solutions here, though, deal with 16 bit values, which seem to make sense, but they’re far from result that the original C code would produce.
Effectively, if endian is non-zero, the C function always returns 0. So, I guess that whoever wrote this code never really tested it in a case where endian-swapping is necessary.
So, the correct Xojo version of this code would be:
function r_8 (v as Integer, endian as Integer) as Integer
if endian <> 0 then
return 0
else
return v and &hFF
end
end function
Unless…
Maybe the C programmer found that the r_8 function didn’t work right, so he added this line to his code to “fix” it:
#define uint8_t int
I wouldn’t be the first time I’d see something like this
IMO, whoever wrote this code has very (too) little understanding of programming in C. Is this some hardware related code, by any chance?
Besides, the original code is plain C, nothing that requires C++.
[quote=36281:@Thomas Polson]My best guess for Xojo is the following:
…
if (endian)=1 then[/quote]
Careful there!
In C, any non-zero value means true!
Therefore, your “if endian = 1” is not correct, as the user of the C code might as well pass -1 for “endian” and it would still run into the “return (value << 8) | (value >> 8);” line.