Re: Copying string to byte array

From: Jim Mack (jmack_at_mdxi.nospam.com)
Date: 01/13/05


Date: Wed, 12 Jan 2005 20:16:47 -0500

Sam Hobbs wrote:
> "Jim Mack" <jmack@mdxi.nospam.com> wrote in message
> news:eXxw1CP%23EHA.1264@TK2MSFTNGP12.phx.gbl...
>>
>> The byte data does not exist "as-is" in the string to begin with,
>> since VB strings are Unicode. Any byte data will have been converted
>> to Unicode using the code page in effect at the time. If this is
>> the same code page used at the time it is converted back, then the
>> operation is transparent.
>>
>> If a different code page is in effect, then the (VB6) user can
>> specify the encoding code page as an extra argument to StrConv,
>> again making the operation transparent. If it is different and
>> unknown, then the conversion cannot be guaranteed transparent.
>
>
> The MSDN refers to binary data in a string, and I assumed that it is
> refering to essentially the same thing as what I mean. If so, then it
> seems to be misunderstood by everyone else. So perhaps I am using the
> term differently than the MSDN does, but regardlous, code pages are
> not relevant. When I say binary, I mean data that has no relevance
> whatsoever to characters. Binary data is not even fictional
> characters, so they have no correspondence whatsoever with code pages
> or anything related to code pages.
>
> I clarified what I meant by binary data in another post.

VB strings cannot contain arbitrary binary data. This is the reason
(stated by MS) that byte arrays were introduced into the language.

Don't even bother trying, it's a well-travelled road that leads to
misery. :-)

Bottom line: for arbitrary binary data, use byte arrays and never let it
touch a VB string -- not for file I/O, not for API parameters -- never.

-- 
        Jim


Relevant Pages

  • Re: Dangerous behavior of CString
    ... If I'm reading a data file or serial port or something, if the raw data are multibyte but the compilation is Unicode or vice-versa, then sometimes the converting constructors in CString are convenient. ... I did not actually write code like this; in fact I was pretty careful always to use the _T macro with any literal strings. ... But it does the conversion using the current 8-bit code page, which is not what I want. ...
    (microsoft.public.vc.mfc)
  • Re: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... strings, the user can control the length by the data they process; ... >> The computer world is more complex than it's ever been (eg Unicode) ... The Pascal `Char' type can be this size (unlike C, ... > Note that ansi->wide conversion is codepage sensitive. ...
    (comp.lang.pascal.misc)
  • Re: Copying string to byte array
    ... > VB strings are Unicode. ... > Unicode using the code page in effect at the time. ... > conversion cannot be guaranteed transparent. ... The MSDN refers to binary data in a string, and I assumed that it is ...
    (microsoft.public.vb.general.discussion)
  • Re: Want Input boxes to accept unicode strings on Standard Window
    ... strings with _T ... pattern) but these blow up immediately. ... as a "massive effort" or, in one case, "we need a complete rewrite in Unicode and can't ... the process a couple of times the conversion thing is pretty academic. ...
    (microsoft.public.vc.mfc)
  • Dangerous behavior of CString
    ... On initial compilation under Unicode, there were several hundred errors, and it took me a couple of days to get rid of them. ... I then started to test my app with strings from different languages, and was surprised to find that in some places the strings were displayed correctly, but in others they were not. ... Thus the implicit conversion constructor prevents the compiler form telling me that my code is not as I intended. ...
    (microsoft.public.vc.mfc)