How do I store an integer as a hex value into a file?

I am using VB6 and have a requirement to store an integer value into a hex 2-byte field in a file, with the bytes reversed. For example, an integer of 512 needs to be stored in the file as 0020. I know how to convert the value of 512 into a hex string with the ascii characters "200" but I don't know how to store this as a 2-byte hex field with a value of 0020. Any assistance would be greatly appreciated.
dombrov55Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

J-MikCommented:
Dombrov55,

Where are you storing this to? If the field is a 2-byte numeric field, then hex or not, you should be able to send 512 directly into it, without the need to convert it to ASCII. By converting 512 into "200", you've turned it into a 3-byte character field.
0
JR2003Commented:
Option Explicit

Private Sub Command1_Click()

    Dim sBinString As String
    Dim iResultLong As Long
    Dim sHexResult As String
   
    sBinString = ConvertToBinaryReversedString(512)
    iResultLong = ConvertBinToDecimal(sBinString)
    sHexResult = Hex(iResultLong)
    MsgBox sHexResult
End Sub

Private Function ConvertToBinaryReversedString(ByVal InInt As Long) As String

    Dim sBin As String
    Dim i As Long
    i = 1
    sBin = String(16, "0")
    While InInt > 0
        If InInt And 1 Then
            Mid$(sBin, i, 1) = "1"
        Else
            Mid$(sBin, i, 1) = "0"
        End If
        InInt = InInt / 2
        i = i + 1
    Wend
    ConvertToBinaryReversedString = sBin
   
End Function

Private Function ConvertBinToDecimal(ByVal InBinString As String) As Long

    Dim iResult As Long
    Dim i As Long
    For i = 1 To Len(InBinString)
        If Mid(InBinString, i, 1) = "1" Then
            iResult = iResult + 2 ^ (Len(InBinString) - i)
        End If
    Next i
    ConvertBinToDecimal = iResult
End Function
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Erick37Commented:
You can use the API rtlMoveMemory (CopyMemory) to reverse the byte order of the integer.  This example flips the bytes of the input integer and saves the result to a second integer.  You can either use the resulting integer as a number, or format it as text.

Option Explicit

Private Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()

    Dim nIn As Integer
    Dim nOut As Integer
   
    nIn = 512
   
    'Flip the bytes of the integers
    Call MoveMemory(ByVal VarPtr(nOut), ByVal VarPtr(nIn) + 1, 1)
    Call MoveMemory(ByVal VarPtr(nOut) + 1, ByVal VarPtr(nIn), 1)
   
    'Test the results
    Debug.Print Format(Hex(nIn), "0000")
    Debug.Print Format(Hex(nOut), "0000")

End Sub
0
dombrov55Author Commented:
Thanks for the very prompt responses but I think I need to clarify what I am asking for. I will have numeric input, string format, that needs to be converted to hex and stored in reverse order. So my input could be "1000", which is hex 03E8 but it needs to be stored as E803, but not  as "3E8" or "E803". The data field in the file is a 2-byte field and the application reading this data (not my application) expects it in this format.

0
Erick37Commented:
If your input is a string, then first convert it to integer data type before flipping the order (integers are 2 bytes).  Since the output datafield expects 2 bytes you must save the data as integer data type (not string because the string "E803" requires 4 bytes).

Your result in HEX is &HE803, which is stored internally as an integer with the value of -6141.

Option Explicit

Private Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Command1_Click()

    Dim sInput As String
    Dim nIn As Integer
    Dim nOut As Integer
   
    'Convert your string input to an integer first
    sInput = "1000"
    nIn = CInt(sInput)
   
    'Flip the bytes of the integers
    Call MoveMemory(ByVal VarPtr(nOut), ByVal VarPtr(nIn) + 1, 1)
    Call MoveMemory(ByVal VarPtr(nOut) + 1, ByVal VarPtr(nIn), 1)
   
    'The result is in nOut (integer = 2 bytes)
    Debug.Print "nOut = " & nOut
    Debug.Print "nOut in HEX = " & Format(Hex(nOut), "0000")


End Sub
0
onemorecokeCommented:
Just do:

sHex=format$(mid$(hex$(512),3,2),"00") & format$(mid$(hex$(512),1,2),"00")

It will work perfectly!

onemorecoke
0
dombrov55Author Commented:
Thanks very much Erik, I'm getting very close as it does give me the result I want, sort of.  However, the integer field in my data record ends up getting stored a 03E8, although I know a -6141 is really  FFFFE803. Do I need one more step that has to do with signed our unsigned, or do I And off those high-order bits from nOut?

0
Erick37Commented:
How are you saving/writing the data?
FFFFE803 is a Long (4 byte) data type representation of the result.  If you save the result in an Integer (2 byte) then it will be E803.
0
EDDYKTCommented:
Private Declare Function LoByte Lib "TLBINF32" Alias "lobyte" (ByVal Word As Integer) As Byte
Private Declare Function HiByte Lib "TLBINF32" Alias "hibyte" (ByVal Word As Integer) As Byte
Private Declare Function loword Lib "TLBINF32" (ByVal DWord As Long) As Integer
Private Declare Function hiword Lib "TLBINF32" (ByVal DWord As Long) As Integer

Private Sub Form_Load()

    Value = 512
    Debug.Print LoByte(loword(Value))
    Debug.Print HiByte(loword(Value))
    Debug.Print LoByte(hiword(Value))
    Debug.Print HiByte(hiword(Value))
End Sub
0
dombrov55Author Commented:
Erik,

I'm storing as an integer (2 bytes), however, when I I look at the file after it's been stored, the value appears as 03E8, correct number of bytes, but the bytes are reversed from how I need them. I'm using a Put statement to write the record and have Opened the file As Random.
0
Erick37Commented:
Right,  When you save data in binary (using Put #) it is stored on Intel/Windows machines in byte reversed order called Little-Endian.  So your data will appear in the file as already byte reversed.  When you read it back using Get, it is unflipped for you.

If the application that is reading the data needs this byte reversed order, then you need not flip the bytes before using the Put statement.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dombrov55Author Commented:
Thanks again Erik, that was it! Turns out what I needed was embarrassingly easy, all I needed was to assign the integer to the field on my record and do the Put #.  I should have mentioned at the beginning that I was using the Put #.  I had previously tried assigning the integer to the record and doing output with the Print # statement but in that case I believe it stored the actual integer, not the hex value as required. I did learn something here and appreciate the responses from everyone, even picked up on some other code I might be able to use somewhere else.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.