CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Typecast

  1. #1
    Join Date
    Jul 2001
    Posts
    15

    Typecast

    i want to typecase a string into a double variable in VB ie i have retrieved 8 bytes from a data file in an string but it represents a double value so i want to now typecast the same to double keeping the byte code same as that of string.Please tell me how to do this


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Typecast


    private Declare Sub RtlMoveMemory Lib "kernel32" (Des as Any, Src as Any, byval Length as Long)

    private Sub Form_Load()



    Dim i as Double
    Dim s as string
    Dim b(7) as Byte
    Dim k as Long

    s = "12345678" ' here replace with ur string with 8 bytes.
    for k = 0 to 7
    b(k) = Asc(mid(s, i + 1, 1))
    next k

    RtlMoveMemory i, VarPtr(b(0)), 8

    Debug.print i

    End Sub





    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

  3. #3
    Join Date
    Jul 2001
    Posts
    15

    Re: Typecast

    Thanks a lot for the reply...
    Please tell me where I can find more info about "RtlMoveMemory "
    My program needs to be compatible with both windows95 and 2000.Will this API serve my purpose?
    Can I do typecasting without using any API's?




  4. #4
    Join Date
    Apr 2000
    Posts
    737

    Re: Typecast

    rtlmovememory is undocumented. and you are not support to use it in VB, because it might crash the program if use wrongly.

    The code doesn't actually do casting, it move the data from one location of memory to another location. if need casting vb got a lot of functions, e.g.

    dim s as string
    dim l as long

    s = "12"
    l = clng(s) 'l = 12




    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

  5. #5
    Join Date
    Apr 2000
    Posts
    737

    Re: Typecast

    sorry on the term, it is copy data from one location to another, not Move, and this API should work for both platform.


    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

  6. #6
    Join Date
    Jul 2001
    Posts
    15

    Re: Typecast

    "Clng" is a conversion function.I need to typecast a string to double and not convert.
    We need to keep the byte code same so that say "3" in string will not be 3 in integer rather it would be the ascii value of 3


  7. #7
    Join Date
    Feb 2000
    Posts
    137

    Re: Typecast


    private Type Str8Byte
    Value as string
    End Type
    private Type MyDbl
    Value as Double
    End Type

    public Function TypeCast(sTmp as string) as double
    Dim s8Tmp as Str8Byte
    Dim dTmp as MyDbl
    s8Tmp.Value = sTmp
    LSet dTmp = s8Tmp
    TypeCast = dTmp.Value
    End Function




    If your string has more than 8 bytes, you will get a type mismatch on the LSet command.

    Hope this helps.
    Spectre



  8. #8
    Join Date
    Jul 2001
    Posts
    15

    Re: Typecast

    Thanks a lot for your reply.....
    Can you please give me the code to typecast a double into a string variable?


  9. #9
    Join Date
    Apr 2000
    Posts
    737

    Re: Typecast


    private Declare Sub RtlMoveMemory Lib "kernel32" (Des as Any, Src as Any, byval Length as Long)

    private Sub Form_Load()



    Dim i as Double
    Dim s as string
    Dim b(7) as Byte
    Dim k as Long


    s = ""
    i = 123
    RtlMoveMemory b(0), i, 8

    for k = 0 to 7
    s = s & Chr(b(k))
    next k


    Debug.print s

    End Sub






    HTH

    cksiow
    http://vblib.virtualave.net - share our codes

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

Featured