Results 1 to 8 of 8

Thread: [RESOLVED] Convert Floating point to HexA Decimal?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    35

    Resolved [RESOLVED] Convert Floating point to HexA Decimal?

    Hi! i am really lost here, tried different way to get it to work, so far i can only manage to convert bytes to floats but not vice versa,
    The way i am looking for would be something along these lines.

    let's say i have a text box i call Text1.text when i type the number 1 here
    i would love for it to be converted to this 000080F3 that is the reversed order of float 1 i think it was in hexaDecimal, but no matter what ive tried i never manage to get it right.

    I know how to hex reverse it but to first convert it to this 3f800000

    This tool here does exactly what i want to achive , is this not possible in VB?

    http://babbage.cs.qc.edu/IEEE-754/Decimal.html

    I type for example 2 i get 40000000 in hexadecimal
    any simple way to do this in vb?

    thank you for any leads on this.

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Convert Floating point to HexA Decimal?

    does this help?
    Code:
    Option Explicit
    
    Private Type tSingle
      Value As Single
    End Type
    
    Private Type tLong
      Value As Long
    End Type
    
    Private Sub Form_Load()
      Dim f As tSingle
      Dim i As tLong
      
      f.Value = 2!
      LSet i = f
      Debug.Print Hex(i.Value)
      
      Debug.Print "exponant "; (i.Value And &H7F800000) / &H800000 - 127
    End Sub
    W o t . S i g

  3. #3

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    35

    Re: Convert Floating point to HexA Decimal?

    OMG! you dont know how many hours since 2 days ago ive tried to get it right, i used Clng and Cbyte and soo on and it was driving me nuts.
    i dident have the same code you hade here mine was far to more complex but dident do squat.

    Thank you soo much for this help works exactly as i was trying to do it!
    so for me to split this up to this get when i type 1 i get this ok 3f800000
    whats a simple way to do this &H3F, &H00, &H00, &H00

    i need those inbetween &H do i do something like "&H" & XX & "&H" and so on?

    You guys rock thanx so far, the hardest part you solved just need those to be inbtween!

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Convert Floating point to HexA Decimal?

    If you are happy with using LSet and associated UDTs then you could use something like the following to replace the tLong type
    Code:
    Private Type t4Bytes
      Byt0 as Byte
      Byt1 as Byte
      Byt2 as Byte
      Byt3 as Byte
    End Type
    There are APIs you can use also, check out RtlMoveMemory (aka CopyMemory) and vbaCopyBytes.
    W o t . S i g

  5. #5

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    35

    Re: Convert Floating point to HexA Decimal?

    Thank you again! by the way i ended up using an old string split code i hade here but it dident work too well since i hade to add in a second process for that to do it after i recived the resulting hexadecimals soo was looking around for another alternative,
    RTlmovMemory was the one i was searching for yes a while back i remember.

    Yeah thank you for the help and the tip's!
    You helped me aloot here! hope i can help someone here on these great forums.

    Problem as good as solved here!
    will try your code below aswell.

    /Marcus

  6. #6

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    35

    Re: [RESOLVED] Convert Floating point to HexA Decimal?

    Finaly completely solved this!
    here's my resulting code i needed and that got solved thanks to Milk incase anyone else is looking for something like this i decided i wanted to post my final resulting code here.

    Code:
    Private Sub Command1_Click()
    
    '------------------------------
    Dim strBuff As String
      
      Dim Str1 As String
      Dim Str2 As String
      Dim Str3 As String
      Dim Str4 As String
    '------------------------------
    
      Dim F As tSingle
      Dim i As tLong
    
      On Error GoTo Hell
      F.Value = InputF.Text ' InputF short Textbox name for Input Float i gave the Input box!
      
      LSet i = F
    
    strBuff = Right$("0000000" & Hex(i.Value), 8)
    
        
     Str1 = Mid$(strBuff, 7, 2)
     Str2 = Mid$(strBuff, 5, 2)
     Str3 = Mid$(strBuff, 3, 2)
     Str4 = Mid$(strBuff, 1, 2)
    '----------------------------------------
    
    OutPutX.Text = "&H" & Str1 & "," & " " & "&H" & Str2 & "," & " " & "&H" & Str3 & "," & " " & "&H" & Str4 'Outoput final result to a textbox called OutputX
    Hell:
    
    End Sub
    Working perfect now! thanks again Milk and Logophobic!

    /Marcus
    Last edited by Marcus09; Oct 16th, 2011 at 06:01 PM.

  7. #7
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Convert Floating point to HexA Decimal?

    Code:
    MyBytes = StrReverse(xFloat)
    This will not give you a correct result. You seem to be confused by the endianness (byte order) of the data on disk.

    For example, the value "3F800000" is written to disk as "00 00 80 3F". This is not simply the reverse of the first string. It is the reverse order of the bytes, with each byte being two hex digits. Reversing the string gives you "00 00 08 F3" which is not what you want.

    Use this instead:
    Code:
     xFloat = Hex(i.Value)
     strBuff = "00000000"
     Mid(strBuff, 9 - Len(xFloat)) = xFloat
    
    ' The three lines above could also be written as:
    ' strBuff = Right$("0000000" & Hex(i.Value), 8)
    
     Str1 = Mid$(strBuff, 7, 2)
     Str2 = Mid$(strBuff, 5, 2)
     Str3 = Mid$(strBuff, 3, 2)
     Str4 = Mid$(strBuff, 1, 2)

  8. #8

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    35

    Re: [RESOLVED] Convert Floating point to HexA Decimal?

    Quote Originally Posted by Logophobic View Post
    Code:
    MyBytes = StrReverse(xFloat)
    This will not give you a correct result. You seem to be confused by the endianness (byte order) of the data on disk.

    For example, the value "3F800000" is written to disk as "00 00 80 3F". This is not simply the reverse of the first string. It is the reverse order of the bytes, with each byte being two hex digits. Reversing the string gives you "00 00 08 F3" which is not what you want.

    Use this instead:
    Code:
     xFloat = Hex(i.Value)
     strBuff = "00000000"
     Mid(strBuff, 9 - Len(xFloat)) = xFloat
    
    ' The three lines above could also be written as:
    ' strBuff = Right$("0000000" & Hex(i.Value), 8)
    
     Str1 = Mid$(strBuff, 7, 2)
     Str2 = Mid$(strBuff, 5, 2)
     Str3 = Mid$(strBuff, 3, 2)
     Str4 = Mid$(strBuff, 1, 2)



    You are absolutly right here, i am making a memory managing tool here and i just tried it out this partial code was supposed to be just a smal part of this tool, so i saw this thread again and thought, what i dont know what i want, lol, so i tried it out and yes indeed you were right here it was done wrongfully.

    So thank you aswell Logophobic very much here, now i wont have to get confused ones am getting to the stage where i am going to use it.

    I will edit that post above for the new methode to use, even less code for this which is great!

    Thanks Milk and Logophobic helping me solve this finaly!

    /Marcus
    Last edited by Marcus09; Oct 16th, 2011 at 05:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width