Binary to String Encoding 
Author Message
 Binary to String Encoding

I am trying to convert the data from a Byte Array to a string. I used to
have this working fine under VB6, but I am having some data problem with
.NET.

The VB6 Source is :
    Dim sNumber As String, bNumber() As Byte
    bNumber = rs("Number").Value
    sNumber = bNumber

The VB.NET Source is :
sNumber = BinaryToString(rdr.Item("Number"))

Private Function BinaryToString(ByVal bNumber as Byte()) As String
    Dim enc as System.Text.ASCIIEncoding
    ' Have also tried System.Text.UTF8Encoding and UnicodeEncoding
    Return enc.GetString(bNumber)
End Function

************
Sample data would be the following
Binary Data To Convert: 0x467C3F6E3F773F163F3F3F233F3F083F
Result in VB6: F|n-w Y??#??T
Result in VB.NET: F|?n?w? ???#???

Basically all of the characters that are not converted seem to be 128-255.

Any help would be appreciated,

Thanks,

Aric Levin
SoundDogs.com



Mon, 31 Jan 2005 01:43:39 GMT  
 Binary to String Encoding
Aric,

Quote:
>Private Function BinaryToString(ByVal bNumber as Byte()) As String
>    Dim enc as System.Text.ASCIIEncoding
>    ' Have also tried System.Text.UTF8Encoding and UnicodeEncoding
>    Return enc.GetString(bNumber)
>End Function

ASCIIEncoding tosses the upper bit of the bytes being converted.  UTF8/Unicode
attempt to make sense out of the bytes as if they were direct representations of
a string in the corresponding encoding.

Try this instead:

   Return = Encode(Encoding.GetEncoding("iso-8859-1").GetString(bNumber));

Quote:
>SoundDogs.com

Checking it twice, will recommend my son (musician) to take a look. ;-)

--

*--------={ Fine Art for .NET }=--------*

*---------------------------------------*

       Turn on, tune in, download.



Mon, 31 Jan 2005 02:04:48 GMT  
 Binary to String Encoding
Still gives me same result.

Thanks,

Aric



Quote:
> Aric,

> >Private Function BinaryToString(ByVal bNumber as Byte()) As String
> >    Dim enc as System.Text.ASCIIEncoding
> >    ' Have also tried System.Text.UTF8Encoding and UnicodeEncoding
> >    Return enc.GetString(bNumber)
> >End Function

> ASCIIEncoding tosses the upper bit of the bytes being converted.
UTF8/Unicode
> attempt to make sense out of the bytes as if they were direct
representations of
> a string in the corresponding encoding.

> Try this instead:

>    Return = Encode(Encoding.GetEncoding("iso-8859-1").GetString(bNumber));

> >SoundDogs.com

> Checking it twice, will recommend my son (musician) to take a look. ;-)

> --

> *--------={ Fine Art for .NET }=--------*

> *---------------------------------------*

>        Turn on, tune in, download.




Mon, 31 Jan 2005 02:27:21 GMT  
 Binary to String Encoding
Aric,

Quote:
>>    Return = Encode(Encoding.GetEncoding("iso-8859-1").GetString(bNumber));

Ooops, that code which I cut and pasted from a project has everything wrapped in
an extra Encode - which I'm sure you removed.

Anyway, try the code below.  You will see that you can convert all 256 byte
values in an array to a string and back again. Nothing is lost.

using System;
using System.Text;

namespace BytesToString
{
   class Class1
   {
      static void Main(string[] args)
      {
         byte[] bytes = new byte[256];
         string s;
         int i;

         for(i = 0; i < 256; i++ )
            bytes[i] = (byte)i;

         s = Encoding.GetEncoding("iso-8859-1").GetString(bytes);
         bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(s);

         for(i = 0; i < 256; i++ )
            Console.Write("{0} ", bytes[i]);
         Console.ReadLine();
      }
   }

Quote:
}

--

*--------={ Fine Art for .NET }=--------*

*---------------------------------------*

       Turn on, tune in, download.



Mon, 31 Jan 2005 03:02:25 GMT  
 Binary to String Encoding
Still doesn't work
I tried a few different variations and they are all giving me the same
result.

Wanted Result: F|n-w Y??#??T
Result I get: F|?n?w? ???#???

If you see the characters that differ are the Unicode characters or bit 8
characters. All of the first 7 bit characters are fine.

Thanks,

Aric



Quote:
> Aric,

> >>    Return =

Encode(Encoding.GetEncoding("iso-8859-1").GetString(bNumber));
Quote:

> Ooops, that code which I cut and pasted from a project has everything
wrapped in
> an extra Encode - which I'm sure you removed.

> Anyway, try the code below.  You will see that you can convert all 256
byte
> values in an array to a string and back again. Nothing is lost.

> using System;
> using System.Text;

> namespace BytesToString
> {
>    class Class1
>    {
>       static void Main(string[] args)
>       {
>          byte[] bytes = new byte[256];
>          string s;
>          int i;

>          for(i = 0; i < 256; i++ )
>             bytes[i] = (byte)i;

>          s = Encoding.GetEncoding("iso-8859-1").GetString(bytes);
>          bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(s);

>          for(i = 0; i < 256; i++ )
>             Console.Write("{0} ", bytes[i]);
>          Console.ReadLine();
>       }
>    }
> }

> --

> *--------={ Fine Art for .NET }=--------*

> *---------------------------------------*

>        Turn on, tune in, download.




Mon, 31 Jan 2005 04:51:49 GMT  
 Binary to String Encoding

Quote:

>Wanted Result: F|n-w Y??#??T
>Result I get: F|?n?w? ???#???

>If you see the characters that differ are the Unicode characters or bit 8
>characters. All of the first 7 bit characters are fine.

I wasn't sure I understood what you were trying to do, now I am sure I don't!

I probably should have stayed out of this one since I never did much with VB
after VB4 and I don't recall what conversions occur in the following code of
yours:

Quote:
>The VB6 Source is :
>    Dim sNumber As String, bNumber() As Byte
>    bNumber = rs("Number").Value
>    sNumber = bNumber

What does the line 'sNumber = bNumber' do, exactly, in the way of conversion?
If you can tell me that I can probably come up with the code require to do the
conversion in vb.net.

One thing that puzzles me, however, is your requirement that the string have a
given appearance.  Is it going to be displayed somewhere?  Do end-users actually
see that string?  My guess is: not.  If you're using the string internally, for
some reason, I think I've demonstrated that bytes can be converted to a string
and back again useing the 8859-1 encoding.  Why isn't that good enough?

--

*--------={ Fine Art for .NET }=--------*

*---------------------------------------*

       Turn on, tune in, download.



Mon, 31 Jan 2005 05:37:30 GMT  
 Binary to String Encoding
Basically it converts the Binary into a String. That is the easy way out to
do it in VB6.
Should be something similar to this:

Dim s As String, i As Integer
    For i = 0 To (Len(byteArray) - 1)
    s = s + Chr$(byteArray(i))
Next

Basically the question marks (F|?n?w? ???#???) in the result that I am
getting are supposed to be the 8th bit of the ASCII characters and they are
not displayed (F|n-w Y??#??T). Try to see the difference. 3rd Character is
? instead of , 5th character is ? instead of - ...
After I get the String it has to be decrypted. I have a COM Object for
Encryption/Decryption that will handle this.
Thanks a lot,

Aric



Quote:

> >Wanted Result: F|n-w Y??#??T
> >Result I get: F|?n?w? ???#???

> >If you see the characters that differ are the Unicode characters or bit 8
> >characters. All of the first 7 bit characters are fine.

> I wasn't sure I understood what you were trying to do, now I am sure I
don't!

> I probably should have stayed out of this one since I never did much with
VB
> after VB4 and I don't recall what conversions occur in the following code
of
> yours:

> >The VB6 Source is :
> >    Dim sNumber As String, bNumber() As Byte
> >    bNumber = rs("Number").Value
> >    sNumber = bNumber

> What does the line 'sNumber = bNumber' do, exactly, in the way of
conversion?
> If you can tell me that I can probably come up with the code require to do
the
> conversion in vb.net.

> One thing that puzzles me, however, is your requirement that the string
have a
> given appearance.  Is it going to be displayed somewhere?  Do end-users
actually
> see that string?  My guess is: not.  If you're using the string
internally, for
> some reason, I think I've demonstrated that bytes can be converted to a
string
> and back again useing the 8859-1 encoding.  Why isn't that good enough?

> --

> *--------={ Fine Art for .NET }=--------*

> *---------------------------------------*

>        Turn on, tune in, download.




Mon, 31 Jan 2005 05:46:12 GMT  
 Binary to String Encoding
If you are still using
   Binary Data To Convert: 0x46 7C 3F 6E 3F 77 3F 16 3F 3F 3F 23 3F 3F 08 3F

Then PLEASE note all those '3F' in the binary data
actually ARE ASCII '?' characters!

Please check the content of you Byte() array...
I guess it is NOT a problem with conversion,
if you use codepages like
    Encoding.GetEncoding(1252)

http://www.microsoft.com/globaldev/reference/WinCP.asp

http://www.microsoft.com/globaldev/reference/iso.asp

--
  NETMaster  (Thomas Scheidegger)
  http://www.cetus-links.org/oo_CSharp.html

Quote:

> Still doesn't work
> I tried a few different variations and they are all giving me the same
> result.

> Wanted Result: F|n-w Y??#??T
> Result I get: F|?n?w? ???#???
> If you see the characters that differ are the Unicode characters or bit 8
> characters. All of the first 7 bit characters are fine.



Mon, 31 Jan 2005 06:02:49 GMT  
 Binary to String Encoding
I posted a sample of VB.NET calling a C++ DLL.  The DLL
passes back a byte array that needs to be converted to a
string in VB.NET so it gives you a working example.

*****************************************
*                                       *
*   TestUDTPassing VB.NET source code   *
*                                       *
*****************************************

Imports System.Text
Imports System.Runtime.InteropServices
Imports System.ArgumentException

Module TestUDTPassing

    Sub Main()
        Dim intResults As Integer
        Dim TestPassing As New clsTestPassing()

        ' Load some data
        TestPassing.intAccessStatus = &H11223344
        TestPassing.strUserName = "patrick"
        intResults = TestPassing.ReadTestPassing()
        Console.WriteLine("Access Status = " & Hex
(TestPassing.intAccessStatus))
        Console.WriteLine("User Name <" &
TestPassing.strUserName & ">")
    End Sub

End Module

<StructLayout(LayoutKind.Sequential)> Public Structure
DATA_TestPassing
    Public AccessStatus As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)>
Public UserName() As Byte
End Structure

Public Class clsTestPassing
    Dim DataEncode As New System.Text.ASCIIEncoding()
    Dim m_strUserName As String
    Dim REC_TestPassing As New DATA_TestPassing()

    Private Const SIZE_TestPassing_UserName As Integer = 16
    Private Const ASIS_TestPassing_UserName As Integer = 20

    Public Sub New()
        ReDim REC_TestPassing.UserName(20)
        Console.WriteLine("New invoked")
    End Sub

    Public Property intAccessStatus() As Integer

        Get
            Return REC_TestPassing.AccessStatus
        End Get

        Set(ByVal Value As Integer)
            REC_TestPassing.AccessStatus = Value
        End Set

    End Property

    Public Property strUserName() As String

        Get
            Return m_strUserName
        End Get

        Set(ByVal Value As String)
            If (Value.Length > SIZE_TestPassing_UserName)
Then
                m_strUserName = Value.Substring(0,
SIZE_TestPassing_UserName)
            Else
                m_strUserName = Value
            End If
        End Set

    End Property

    Private Sub UnloadTestPassing()
        Dim intPosition As Integer

        m_strUserName = DataEncode.GetString
(REC_TestPassing.UserName)
        intPosition = m_strUserName.IndexOf(Chr(0))
        If intPosition = 0 Then
            m_strUserName = ""
        Else
            m_strUserName = m_strUserName.Substring(0,
intPosition)
        End If
    End Sub

    Private Sub LoadTestPassing()
        Encoding.ASCII.GetBytes(m_strUserName.ToCharArray,
0, m_strUserName.Length, REC_TestPassing.UserName, 0)
        REC_TestPassing.UserName(m_strUserName.Length) = 0
    End Sub

    Private Declare Function AccessTestPassing _
          Lib "c:\Pat\VS6\VC6
\TestUDTPassing\debug\TestUDTPassing" _
            (ByVal intAccessType As Integer, _
             ByRef REC_TestPassing As DATA_TestPassing) _
        As Integer

    Public Function ReadTestPassing() As Integer
        Dim intAccessType As Integer
        Dim intResults As Integer

        LoadTestPassing()
        intAccessType = &HAABBCCDD
        intResults = AccessTestPassing(intAccessType, _
                                       REC_TestPassing)
        UnloadTestPassing()
    End Function

End Class

******************************************
*                                        *
*   TestUDTPassing DLL DEF source code   *
*                                        *
******************************************

LIBRARY TestUDTPassing

EXPORTS

******************************************
*                                        *
*   TestUDTPassing DLL CPP source code   *
*                                        *
******************************************

/*

  TestUDTPassing.cpp

  This module is the DLL that interfaces VB.NET/C++
  to test passing a UDT with a string

  Modification History

     DATE     PROGRAMMER             Modification
  ----------  ---------------------  ----------------------
-----------
  2002/05/06  Patrick Ireland        Original
  2002/08/12  Patrick Ireland        Update documentation

*/

/**********************************************************
**********
*                                                          
         *
*  System
includes                                                  *
*                                                          
         *
***********************************************************
*********/

#include <windows.h>
#include <stdio.h>

/**********************************************************
**********
*                                                          
         *
*  Program
definitions                                              *

Quote:
>-----Original Message-----
>I am trying to convert the data from a Byte Array to a
string. I used to
>have this working fine under VB6, but I am having some
data problem with
>..NET.

>The VB6 Source is :
>    Dim sNumber As String, bNumber() As Byte
>    bNumber = rs("Number").Value
>    sNumber = bNumber

>The VB.NET Source is :
>sNumber = BinaryToString(rdr.Item("Number"))

>Private Function BinaryToString(ByVal bNumber as Byte())
As String
>    Dim enc as System.Text.ASCIIEncoding
>    ' Have also tried System.Text.UTF8Encoding and
UnicodeEncoding
>    Return enc.GetString(bNumber)
>End Function

>************
>Sample data would be the following
>Binary Data To Convert: 0x467C3F6E3F773F163F3F3F233F3F083F
>Result in VB6: F|n-w Y??#??T
>Result in VB.NET: F|?n?w? ???#???

>Basically all of the characters that are not converted
seem to be 128-255.

>Any help would be appreciated,

>Thanks,

>Aric Levin
>SoundDogs.com

>.



Mon, 31 Jan 2005 07:38:58 GMT  
 Binary to String Encoding

Quote:
> Basically the question marks (F|?n?w? ???#???) in the result that I am
> getting are supposed to be the 8th bit of the ASCII characters and they
are
> not displayed (F|n-w Y??#??T).

Please note, that there is no 8th bit in ASCII. ASCII is a 7-bit encoding.
If you need an 8-bit encoding use for example iso-8859-1 as already
suggested.

--
Zoltan



Mon, 31 Jan 2005 18:42:28 GMT  
 Binary to String Encoding
Try this

Public Function ByteArrayToString(ByVal bytArray() As Byte) As String
   Dim UTF8 As New UTF8Encoding()
   Return UTF8.GetString(bytArray)
 End Function


Quote:
> I am trying to convert the data from a Byte Array to a string. I used to
> have this working fine under VB6, but I am having some data problem with
> .NET.

> The VB6 Source is :
>     Dim sNumber As String, bNumber() As Byte
>     bNumber = rs("Number").Value
>     sNumber = bNumber

> The VB.NET Source is :
> sNumber = BinaryToString(rdr.Item("Number"))

> Private Function BinaryToString(ByVal bNumber as Byte()) As String
>     Dim enc as System.Text.ASCIIEncoding
>     ' Have also tried System.Text.UTF8Encoding and UnicodeEncoding
>     Return enc.GetString(bNumber)
> End Function

> ************
> Sample data would be the following
> Binary Data To Convert: 0x467C3F6E3F773F163F3F3F233F3F083F
> Result in VB6: F|n-w Y??#??T
> Result in VB.NET: F|?n?w? ???#???

> Basically all of the characters that are not converted seem to be 128-255.

> Any help would be appreciated,

> Thanks,

> Aric Levin
> SoundDogs.com



Tue, 01 Feb 2005 02:39:11 GMT  
 Binary to String Encoding


Quote:
> I am trying to convert the data from a Byte Array to a string. I used to
> have this working fine under VB6, but I am having some data problem with
> .NET.

> The VB6 Source is :
>     Dim sNumber As String, bNumber() As Byte
>     bNumber = rs("Number").Value
>     sNumber = bNumber

> The VB.NET Source is :
> sNumber = BinaryToString(rdr.Item("Number"))

> Private Function BinaryToString(ByVal bNumber as Byte()) As String
>     Dim enc as System.Text.ASCIIEncoding
>     ' Have also tried System.Text.UTF8Encoding and UnicodeEncoding
>     Return enc.GetString(bNumber)
> End Function

> ************
> Sample data would be the following
> Binary Data To Convert: 0x467C3F6E3F773F163F3F3F233F3F083F
> Result in VB6: F|n-w Y??#??T
> Result in VB.NET: F|?n?w? ???#???

> Basically all of the characters that are not converted seem to be 128-255.

Note what Thomas Scheidegger has said in another posting - your sample byte
array data  does not have any high bits set (nothing in the range 128-155),
so I don't think what's in your post is actually the correct data to test
with.

That being said, you might want to change the encoder declaration from:

     Dim enc as System.Text.ASCIIEncoding

to:

     Dim enc As System.Text.Encoding = System.Text.Encoding.Default

I have seen the Default encode work well with bytes with the high bit set,
but then for all I know the default on my machine might well be the
iso-8859-1 code page that Zane Thomas has been suggesting you use.

Quote:

> Any help would be appreciated,

> Thanks,

> Aric Levin
> SoundDogs.com

--
MikeB


Tue, 01 Feb 2005 07:20:12 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Binary Data - Big Endian Encoding

2. encoding binary files as text (UUencode/MIME?)

3. how to url encode a binary file for posting to webserver

4. Function to encode binary file and send by VB (mail)

5. Function to encode binary file and send by VB (mail)

6. Function to encode binary file and send by VB (mail)

7. BinaryRead - howto convert binary string back to string array

8. binary string to ascii string??

9. How to check if a string is Base64 Encoded

10. handling UTF-8 encoded strings

11. xmldoc.xml & string = bstr / Problem with encoding

12. how can i encode the string

 

 
Powered by phpBB® Forum Software