Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Little Endian to Big Endian Character Conversion??

Status
Not open for further replies.

mattscotney

Programmer
Jul 31, 2002
57
AU
Hi all,

Does anyone know how to convert little endian characters to big endian characters?

Example:

Private Sub Command1_Click()

'GET THE TEXT FROM THE TEXT BOX
'(CURRENTLY IN LITTLE ENDIAN FORMAT)

encodedText = text1.text

'CONVERT THE TEXT TO BIG ENDIAN
?????????????HELP??????????????

'WRITE TO FILE
Dim hFile%
hFile = FreeFile
Open "C:\Documents and Settings\matt\Desktop\demo.txt"
For Output As #hFile
Print #hFile, encodedText
Close #hFile

End Sub


Thanks in advance,
Matt Scotney
 

Is this what you are looking for???

Asc Function


Returns anInteger representing thecharacter code corresponding to the first letter in a string.

Syntax

Asc(string)

The required stringargument is any validstring expression. If the string contains no characters, arun-time error occurs.

Remarks

The range for returns is 0 – 255 on non-DBCS systems, but –32768 – 32767 onDBCS systems.

Note The AscB function is used with byte data contained in a string. Instead of returning the character code for the first character, AscB returns the first byte. The AscW function returns theUnicode character code except on platforms where Unicode is not supported, in which case, the behavior is identical to the Asc function.
 
You can see how this works in the MS Kb Article: Q265793 [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
I think you guys are a little off track. What you are explaining is how to convert text to LITTLE ENDIAN (unicode) characters. I already convert the text to LITTLE ENDIAN characters using the AscW function.

I require the characters to be converted to BIG ENDIAN characters either from inputed text or from LITTLE ENDIAN.

Thanks,
Matt Scotney
 
Converting to and from Big and Little Endian is a matter of changing the order of the bytes. Lets consider that you have a Long Integer - 4 bytes long.

Byte3 Byte2 Byte1 Byte0

Little Endian means that the low order byte comes at the lowest memory address. In memory it would be in the following order:
B0 B1 B2 B3

Big Endian reverse that order, also know as printing order: B3 B2 B1 B0.

A rather straightforward reversal function would something the following:

Function ReverseEndian (ByVal InNumb as Long) as Long

Dim TheBytes(3) as Byte
Dim Idx as Integer
Dim RevNumb as Long

' Separate Number into Individual Bytes
For Idx = 0 to 2
TheBytes(Idx) = InNumb Mod 256
InNumb = InNumb / 256
Next Idx
TheBytes(3) = InNumb Mod 256

' Recombine in Reverse Order
RevNumb = TheBytes(0)
RevNumb = (RevNumb * 256) + TheBytes(1)
RevNumb = (RevNumb * 256) + TheBytes(2)
RevNumb = (RevNumb * 256) + TheBytes(3)

ReverseEndian = RevNumb

End Function Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hmmmmmmmmmmm,

At least CajunCenturion understands the bug / little end thinggy. AFAIK, the whole endian is N/A or the quoted text string. In effect the original question appears to be (an extremely) poor man's encrytion scheme?

The effect would be akin to having the Text box:

The quick brown fox jumped over hte lazy brown dog

Be 'encoded' as

god nworb yzal eht revo depmuj xof nworb kciuq ehT


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Usually in the context of strings, Little Endian and Big Endian refer to the order of bytes in a DBCS set.

To convert text1.text from one to the other try this:

Code:
Dim be As Byte
Dim le As Byte
Dim a As Long
Dim myChar As String

For a = 1 To Len(Text1.Text)
myChar = Mid(Text1.Text, a, 1)
le = AscW(myChar) And &HFF
be = Asc(myChar \ &H100)
myChar = ChrW((&H100 * le) + be)
Mid(Text1.Text, a, 1) = myChar
End Sub

It simply steps through the code character by character, separating the two bytes, re-ordering them, translating back into characters and substituting in the original string. Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Johnwm,

I get a type mismatch error with the following line:
be = Asc(myChar \ &H100)

I have included below the whole code which I am using:

Private Sub Command1_Click()

Dim be As Byte
Dim le As Byte
Dim a As Long
Dim myChar As String

'GET THE TEXT FROM THE TEXT BOX
encodedText = Text1.Text

'CONVERT THE TEXT TO BIG ENDIAN
For a = 1 To Len(encodedText)
myChar = Mid(encodedText, a, 1)
le = AscW(myChar) And &HFF
be = Asc(myChar \ &H100)
myChar = ChrW((&H100 * le) + be)
Mid(encodedText, a, 1) = myChar
Next a

'WRITE TO FILE
Dim hFile%
hFile = FreeFile
Open "C:\demo.txt" For Output As #hFile
Print #hFile, encodedText
Close #hFile

End Sub

Thanks in advance,
Matt Scotney
 
Sorry - rushing it !

be = Asc(myChar \ &H100)

should read

be = ascw(myChar) \ &h100 Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top