Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Converting IEEE Format Singles To Internal Longs

Converting IEEE Format Singles To Internal Longs

Scheduled Pinned Locked Moved Visual Basic
performancequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason Teagle
    wrote on last edited by
    #1

    Does anyone know if VB can now (VB6.0) perform a conversion between the internal representation of a single-precision number (held as a long, 4 bytes) and an actual Single? By this I mean, the value 1.0 as a Single = &h3F800000 internally. If I have a Long containing &h3F800000, is it possible to convert to a Single of 1.0, and vice versa (Single -> Long holding internal representation). So far I have had to use a C DLL to do this. Other possible ways to do this might involve using Variants, Byte arrays or some way of accessing the memory used to store the variables (used to be VARSEG and VAROFFSET in QuickBasic) - but I can't find a way. Note that CLng(Single) and CSng(Long) don't do it, since CSng(&h3F800000) = 1065353216.0, which isn't quite 1.0 {:v) Note also that you can't use Any as the type in your own functions {:v( If anyone's wondering why I need this, it's to pull bytes out of a Modbus message and decode them, or to encode them and plug them into a message.

    Richard DeemingR 1 Reply Last reply
    0
    • J Jason Teagle

      Does anyone know if VB can now (VB6.0) perform a conversion between the internal representation of a single-precision number (held as a long, 4 bytes) and an actual Single? By this I mean, the value 1.0 as a Single = &h3F800000 internally. If I have a Long containing &h3F800000, is it possible to convert to a Single of 1.0, and vice versa (Single -> Long holding internal representation). So far I have had to use a C DLL to do this. Other possible ways to do this might involve using Variants, Byte arrays or some way of accessing the memory used to store the variables (used to be VARSEG and VAROFFSET in QuickBasic) - but I can't find a way. Note that CLng(Single) and CSng(Long) don't do it, since CSng(&h3F800000) = 1065353216.0, which isn't quite 1.0 {:v) Note also that you can't use Any as the type in your own functions {:v( If anyone's wondering why I need this, it's to pull bytes out of a Modbus message and decode them, or to encode them and plug them into a message.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      I haven't tested it, but this should work:

      Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

      Public Function Long2Sng(ByVal InVal As Long) As Single
      Dim sRet As Single
      CopyMemory sRet, InVal, 4
      Long2Sng = sRet
      End Function

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      J 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        I haven't tested it, but this should work:

        Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

        Public Function Long2Sng(ByVal InVal As Long) As Single
        Dim sRet As Single
        CopyMemory sRet, InVal, 4
        Long2Sng = sRet
        End Function

        J Offline
        J Offline
        Jason Teagle
        wrote on last edited by
        #3

        I can confirm that it does indeed work - no more C DLLs - hurrah! ;P

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups