I would like to know, what is Motorola integer and how to convert to/from it.
Could somebody help me?
Printable View
I would like to know, what is Motorola integer and how to convert to/from it.
Could somebody help me?
OK, it may seem like it has absolutely nothing to do with the subject, but read on anyway.
Once upon a time, there was a tribe of indians. (See, I told you it may seem like it has nothing to do with anything! Just keep reading :))
That tribe was living normally like any tribe should! Until one day, they wanted to eat some eggs. And they noticed that the eggs have two ends, the little end and the big end.
Which end should be cracked to open the egg?
The tribe divided into two groups. The little-endians/little-indians, who decided to crack the little end. And the big-endians/big-indians, who disagreed and thought the big end is more important.
Today, in the high-tech businesses, there are companies who make processors. I'll take Motorola and Intel as an example.
Motorola are big-endians, and Intel are little-endians.
What does this mean in computers?
Let's say you have a 32-bit integer (Dim My32BitInteger As Long).
Let's say this integer's value, in hex, is ABCDEF12.
You see that the integer is composed of 4 bytes: AB, CD, EF, 12.
The big (high) end is the one which is the leftmost in the hexadecimal representation of the number. (In ABCDEF12, A is the high-order nibble, AB is the high-order byte, ABCD is the high-order word)
The little (low) end is the one which is the rightmost in the hexadecimal representation of the number. (In ABCDEF12, 2 is the low-order nibble, 12 is the low-order byte, EF12 is the low-order word)
Now, let's say this is a piece of the memory model:
...RB RB RB RB RB XX XX XX XX RA RA RA RA RA...
In this "illustration", XX XX XX XX are the 4 bytes in the 32-bit integer (a 32-bit integer is 4 bytes of RAM).
Bytes marked as RB are bytes in the RAM before our integer, and bytes marked as RA are bytes in the RAM after our integer.
With me so far? :rolleyes:
Now, with Intel's memory model (they are little-endians - the low-order bytes come first), ABCDEF12 looks like this:
...RB RB RB RB RB 12 EF CD AB RA RA RA RA RA...
See? They are little (low) endians, and in the number ABCDEF12, the 12 is the lowest byte, so it comes first. EF is the next, then CD, and AB is the highest.
How does it look like on Motorola's memory model?
...RB RB RB RB RB AB CD EF 12 RA RA RA RA RA...
You may think Motorola's memory model is more logical. Well, so do I. :)
Now, to convert integers... I did a little string thing so it's definitely not a very good function, just a lazy one: :rolleyes:
Enjoy! This converts to and from Motorola because it's the exact same action. :rolleyes:Code:Function ConvertEndian(ByVal lNumber As Long) As Long
Dim sHex As String, sTemp As String
' Get the hex number: AABBCCDD
sHex = Right("0000000" & Hex(lNumber), 8)
' Swap the outer-high-order byte with the outer-low-order byte: AABBCCDD -> DDBBCCAA
sTemp = Left(sHex, 2)
Mid(sHex, 1) = Right(sHex, 2)
Mid(sHex, 7) = sTemp
' Swap the inner-high-order-byte with the inner-low-order byte: DDBBCCAA -> DDCCBBAA
sTemp = Mid(sHex, 3, 2)
Mid(sHex, 3) = Mid(sHex, 5, 2)
Mid(sHex, 5) = sTemp
' Return the hex number as a long:
ConvertEndian = CLng("&H" & sHex)
End Function
wow where did you learn all these things man?
And I believe my grand-grand-grand-father belongs to that IndainTribe
I heard the indian story from my father. (Yes, it must be passed father-to-son for centuries to come!) :rolleyes:
All the rest... Ugh.
I think I've been using CopyMemory too much. ;)
hehe :)
Wow!
Ok, thank you for the story, I will try to remember it for other children...
I was reading from file, so I used...
where Dec:Code:For i = 1 To 4
Get #1, , bByte 'Byte buffer
FLS = String(2 - Len(Hex(bByte)), "0") + Hex(bByte) + FLS ' a string-typed var
Next i
FLB = Dec(FLS) ' a currency-typed var, "long" vas too small
1) How can I determine if the bytes are in Intel or in Motorola order (in file/memory)?Code:Function Dec(HexString As String) As Currency
HexString = UCase(HexString)
For i = 0 To Len(HexString) - 1
h = Mid(HexString, Len(HexString) - i, 1)
v = Val(h)
If v = 0 Then
Select Case h
Case "A": v = 10
Case "B": v = 11
Case "C": v = 12
Case "D": v = 13
Case "E": v = 14
Case "F": v = 15
End Select
End If
Dec = Dec + (16 ^ i) * v
Next i
End Function
2) Is a difference in my Dec and your CLng("&H" + HexString)?
PS: Thank you, I have solved my first problem...
Ummm... Any hexadecimal number can be both.Quote:
1) How can I determine if the bytes are in Intel or in Motorola order (in file/memory)?
I mean if someone told you "ABCDEF12", then you can say, "Huh? Do you mean Motorola ABCDEF12, which equals 2882400018 when it's unsigned or negative 1412567278 when it's signed, or do you mean Intel ABCDEF12, which equals 317705643? Make up your mind!"
Do you see what I mean? A hexadecimal number doesn't say anything about its byte order, and you can order it any way you want.
Of course you could just forget about it, memorize the above numbers, say them at parties and be the night's center of attraction. :rolleyes:
Well, your Dec supports higher numbers.Quote:
2) Is a difference in my Dec and your CLng("&H" + HexString)?
That's basically all, I think...
Nice confusion, but why Intel started using the sternformmost ordered bytes? I think there should be a good reason if it was such company...