Jump to content

[HELP PLZ] Change Endianness


Xtasy
 Share

Recommended Posts

Hey all,

I'm designing an inlude-file for Bitmaps (Read them, write data in). :P

The problem I have is that AutoIt and the most programs uses Big-Endian-Convention,

but the data of Bitmaps is stored with Little Endian-Convention. :D

Does somebody know a way to change the interpreting in relation to the endianness or how can I change hex data's endianness?

For example there is a hex term like this one: 0x 0B CD 1B 8A (4 Byte)

I want to swap the bytes that it looks like this: 0x 8A 1B CD 0B (Changed Endianness)

Important: I don't want to spwap the nymbles, just the bytes! (Nymble-Swap: 0x 01 2A --> 0x 10 A2, here the bytes aren't swapped!)

Pls help me.

Edited by Xtasy
Link to comment
Share on other sites

Does somebody know a way to change the interpreting in relation to the endianness or how can I change hex data's endianness?

Func ByteSwap($bin)
;Return Binary(BitRotate(String($bin), 32, "D"));Return Binary(BitOr(String($bin), 0)) :)
    Return Binary(BitShift(String($bin), 32))
EndFunc
Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Hey all,

I'm designing an inlude-file for Bitmaps (Read them, write data in). :P

The problem I have is that AutoIt and the most programs uses Big-Endian-Convention,

but the data of Bitmaps is stored with Little Endian-Convention. :D

Does somebody know a way to change the interpreting in relation to the endianness or how can I change hex data's endianness?

For example there is a hex term like this one: 0x 0B CD 1B 8A (4 Byte)

I want to swap the bytes that it looks like this: 0x 8A 1B CD 0B (Changed Endianness)

Important: I don't want to spwap the nymbles, just the bytes! (Nymble-Swap: 0x 01 2A --> 0x 10 A2, here the bytes aren't swapped!)

Pls help me.

If you have a hex value like "4Fd7", but which could have any number of bytes then this is one way to do it.

Func SwapBytes($hexval)
    local $result = '', $add0x = false
;strip all ws
    $hexval = StringStripWS($hexval,8)
    
    if StringInStr($hexval,'0x') then 
        $hexval = StringReplace($hexval,'0x','',1)
        $add0x = True
    EndIf
    if Mod(StringLen($hexval),2) = 1 then $hexval = '0' & $hexval
        
    for $n = StringLen($hexval) to 2 step -2
        $result &= StringMid($hexval,$n-1,2)
    Next
    if $add0x then return '0x' & $result
    Return $result
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Func ByteSwap($bin)
;Return Binary(BitRotate(String($bin), 32, "D"));Return Binary(BitOr(String($bin), 0)) :)
    Return Binary(BitShift(String($bin), 32))
EndFunc

That doesn't look right to me. Shouldn't it be something like this?

msgbox(0,"swap bytes for '0x12345678' is", Hex(SwapBytes2(0x12345678)))
;returns hex value
Func SwapBytes2($Binaryval)
    Local $mask = 0xff
    Local $Result = 0x0,$i
    
    for $i = 1 to StringLen(String($Binaryval))/2
        $Result = BitShift($Result,-8)
        $Result = BitOR($Result,BitAND($Binaryval,$mask))
        $Binaryval = BitShift($Binaryval,8)
    Next
    
    return $Result
EndFunc
EndFunc

EDIT:corrected code. NB although it looked wrong to me that didn't mean Siao's code was wrong. I is in fact correct and better than mine.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

That doesn't look right to me. Shouldn't it be something like this?

Have you got proof it doesn't work? Because if you haven't, I don't really care how it looks (though to me it looks pretty tight actually). :D

What mattered to me is that it's faster than any of that string parsing.

"be smart, drink your wine"

Link to comment
Share on other sites

Have you got proof it doesn't work? Because if you haven't, I don't really care how it looks (though to me it looks pretty tight actually). :D

What mattered to me is that it's faster than any of that string parsing.

I was confused by my misunderstanding of the way Bitshift works.

Your code does work Siao and now it doesn't look wrong to me. My apologies, although I didn't go as far as to say it didn't work because I stupidly didn't try it. I accept that the implication was there.

Even worse for me, I see that my code didn't work and I didn't have the sense to try it either, I just translated it from a Delphi function I use. I have now corrected my version, but since yours is fine and shorter and faster I am sorry I made such a mess of everything.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Well, it does look kinda goofy at first glance (and second too), I'll give you that. That's why I like it. :D

The BitShift part isn't that relevant as you can see from the commented line above it, there are alternatives... On its own it doesn't do jack, if you BitShift a 32bit 32 bits you end up the same, just like if you BitOr with 0 all bits stay the same. Such pointless operation does make the following Binary() trigger its built-in endian reverser though, which is the whole point.

If you really wanna truth, Binary(Number(String($bin))) would do the same, and would be even faster (although I could swear I had tried that and it didn't work back when I was writing it a year ago, therefore I had to come up with more extravagant solution :P Maybe it was just a bit too much wine).

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

  • 2 weeks later...

Thank you really much, Siao and martin! :D

But I have one more question:

I know the 'BitRotate'-function converts the binaries to 32bit-integers.

And the problem is that I also have to read some 16bit-words from the bitmap.

If I use your functions, the funtions do this: 0x1000 --> 0x00001000

This is not what I want.

This convertion causes wrong interpretation of my bitmap.

I want this: 0x1000 --> 0x10000000

How can I do this.

Pls help me again.

Edited by Xtasy
Link to comment
Share on other sites

Thank you really much, Siao and martin! :D

But I have one more question:

I know the 'BitRotate'-function converts the binaries to 32bit-integers.

And the problem is that I also have to read some 16bit-words from the bitmap.

If I use your functions, the funtions do this: 0x1000 --> 0x10000000

This is not what I want.

This convertion causes wrong interpretation of my bitmap.

I want this: 0x1000 --> 0x00001000

How can I do this.

Pls help me again.

0x1000 is 0x00001000 so you don't need to do anything to convert it in that case.

(Same as 2 is the same as 002)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Sorry, when you read my post, it was wrong.

I've edited it now

I mean: 0x1000 to 0x10000000

I thought I was confused but now I'm not so sure.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I want this: 0x1000 --> 0x10000000

This has nothing to do with changing endian, no matter how you look at it.

In 32-bit (dword), 0x1000 is 0x00001000, after swapping endian it's 0x00100000

And if it's 16-bit (word), then 0x1000 -> 0x0010, which is still not what you want it to be.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...