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

Hybrid View

  1. #1
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    How to convert Variant to Byte Array?

    Is there a way (beside doing it in a loop value by value) to convert a variant, which contain an array of byte data, to a byte array?
    Leica Geosystems - when it has to be right

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: How to convert Variant to Byte Array?

    Yes. Declare a dynamic Byte array, and use StrConv().
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Mar 2002
    Location
    Izhevsk, Udmurtia, Russia
    Posts
    930

    Re: How to convert Variant to Byte Array?

    Code:
    Dim b, a() As Byte, aa() As Byte
      
      ReDim aa(2) ' Source byte array
      aa(0) = 11
      aa(1) = 2
      
      b = aa ' Store byte array into variant
      
      a = b ' Store variant with byte array into byte array
    With best wishes,
    Vita
    -----------------------
    Russian Software Development Network -- http://www.rsdn.ru

  4. #4
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Re: How to convert Variant to Byte Array?

    Thank you both for reply!

    WizBang, my data is not a string. All values for strconvs' 'conversion' argument seems to change the content of the byte array - right?

    Vi2, using an 'undimed' dynamic array does it :-) (unfortunately I tried with a 'dimed' one before)
    Leica Geosystems - when it has to be right

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: How to convert Variant to Byte Array?

    You can use StrConv() to accomplish your goal, but I'm not sure what you mean by "change the content of the byte array". It can fill the Byte array with the contents of the Variant, but the the array must not be ReDim'd first.

    Obviously the method posted by Vi2 can work, though I seem to recall certain cases where I needed to use StrConv(). Off hand I don't recall the specifics, so I can't say whether it has any bearing on your particular need. I never use Variants (too slow), so perhaps that's what the difference was.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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