string >> byte array >> string representation >> byte array >> string !!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Bargna

    string >> byte array >> string representation >> byte array >> string !!

    Hi

    I have a problem, I have a string which needs to be converted to a
    byte array, then have the string representation of this array stored
    in an AD attribute. This string attribute then has to be read and the
    string representation of the byte array has to be converted back to
    the original byte array and converted back to the original string -
    confused yet?

    in psuedo

    1.str="Hello World"
    2.convert str to byte()
    3.convert byte() to str2 = 272010101080108 011103208701110 114010801000
    4.store str2
    5.read str2
    6.convert str2 to byte()
    7.convert byte() to str

    I am getting stuck on step 6, converting the representation of the
    byte array back to the original byte array

    any help woould be grately appreciated.

    cheers
    David
  • Armin Zingler

    #2
    Re: string >> byte array >> string representation >> byte array >> string !!

    "David Bargna" <david@vista.fr ee-online.co.uk> schrieb[color=blue]
    > I have a problem, I have a string which needs to be converted to a
    > byte array, then have the string representation of this array
    > stored in an AD attribute. This string attribute then has to be read
    > and the string representation of the byte array has to be converted
    > back to the original byte array and converted back to the original
    > string - confused yet?
    >
    > in psuedo
    >
    > 1.str="Hello World"
    > 2.convert str to byte()
    > 3.convert byte() to str2 = 272010101080108 011103208701110 114010801000[/color]

    How do you get
    "27201010108010 801110320870111 0114010801000"
    ?

    [color=blue]
    > 4.store str2
    > 5.read str2
    > 6.convert str2 to byte()
    > 7.convert byte() to str
    >
    > I am getting stuck on step 6, converting the representation of the
    > byte array back to the original byte array[/color]


    --
    Armin

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: string &gt;&gt; byte array &gt;&gt; string representation &gt;&gt; byte array &gt;&gt; string !!

      Hello,

      "David Bargna" <david@vista.fr ee-online.co.uk> schrieb:[color=blue]
      >
      > in psuedo
      >
      > 1.str="Hello World"
      > 2.convert str to byte()
      > 3.convert byte() to str2 = 272010101080108 011103208701110 114010801000
      > 4.store str2
      > 5.read str2
      > 6.convert str2 to byte()
      > 7.convert byte() to str
      >
      > I am getting stuck on step 6, converting the representation of the
      > byte array back to the original byte array[/color]

      Have a look at the 'GetString' method of the 'Encoding' class.

      --
      Herfried K. Wagner
      MVP ยท VB Classic, VB.NET
      Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



      Comment

      • David Bargna

        #4
        Re: string &gt;&gt; byte array &gt;&gt; string representation &gt;&gt; byte array &gt;&gt; string !!

        "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> wrote in message news:<OramzL4gD HA.2124@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
        > Hello,
        >
        > "David Bargna" <david@vista.fr ee-online.co.uk> schrieb:[color=green]
        > >
        > > in psuedo
        > >
        > > 1.str="Hello World"
        > > 2.convert str to byte()
        > > 3.convert byte() to str2 = 272010101080108 011103208701110 114010801000
        > > 4.store str2
        > > 5.read str2
        > > 6.convert str2 to byte()
        > > 7.convert byte() to str
        > >
        > > I am getting stuck on step 6, converting the representation of the
        > > byte array back to the original byte array[/color]
        >
        > Have a look at the 'GetString' method of the 'Encoding' class.[/color]

        thanks guys, I've solved the problem using

        Private Function hashext14(ByVal ext14 As String) As String
        If ext14 = "" Then Return Nothing : Exit Function
        Dim UE As New UnicodeEncoding ()
        Dim by As Byte() = UE.GetBytes(ext 14)
        Return Convert.ToBase6 4String(by)
        End Function

        Private Function unhashext14(ByV al hash As String) As String
        On Error GoTo Err
        If hash = "" Or hash = Nothing Then Return Nothing : Exit Function
        Dim b As Byte
        Dim q = Convert.FromBas e64String(hash)
        Dim ret As String
        For Each b In q
        If b <> 0 Then

        ret = ret + Chr(b)
        End If
        Next
        Return ret
        Err:
        Return Nothing : Exit Function

        End Function

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: string &gt;&gt; byte array &gt;&gt; string representation &gt;&gt; byte array &gt;&gt; string !!

          David,
          In case you didn't know, the 'Exit Function' in each of your functions are
          not doing anything, you can delete them!
          [color=blue]
          > If ext14 = "" Then Return Nothing : Exit Function
          > If hash = "" Or hash = Nothing Then Return Nothing : Exit Function
          > Return Nothing : Exit Function[/color]

          The Return statement itself will exit the function, so the " : Exit
          Function" is dead code!

          Consider using OrElse here:[color=blue]
          > If hash = "" Or hash = Nothing Then Return Nothing[/color]
          If hash Is Nothing OrElse hash = "" Then Return Nothing

          To check for Nothing you need (should) use the Is operator, the OrElse will
          short circuit and not do the second part of the comparison if not needed.

          Which is important in statements such as:
          If hash Is Nothing OrElse hash.Length = 0 Then Return Nothing

          As if hash is nothing, the hash.Length function will get a
          NullReferenceEx ception. The OrElse won't bother checking hash.Length when
          hash is Nothing.

          Hope this helps
          Jay

          "David Bargna" <david@vista.fr ee-online.co.uk> wrote in message
          news:89846ed1.0 310060311.283aa db2@posting.goo gle.com...[color=blue]
          > "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> wrote in message[/color]
          news:<OramzL4gD HA.2124@TK2MSFT NGP12.phx.gbl>. ..[color=blue][color=green]
          > > Hello,
          > >
          > > "David Bargna" <david@vista.fr ee-online.co.uk> schrieb:[color=darkred]
          > > >
          > > > in psuedo
          > > >
          > > > 1.str="Hello World"
          > > > 2.convert str to byte()
          > > > 3.convert byte() to str2 = 272010101080108 011103208701110 114010801000
          > > > 4.store str2
          > > > 5.read str2
          > > > 6.convert str2 to byte()
          > > > 7.convert byte() to str
          > > >
          > > > I am getting stuck on step 6, converting the representation of the
          > > > byte array back to the original byte array[/color]
          > >
          > > Have a look at the 'GetString' method of the 'Encoding' class.[/color]
          >
          > thanks guys, I've solved the problem using
          >
          > Private Function hashext14(ByVal ext14 As String) As String
          > If ext14 = "" Then Return Nothing : Exit Function
          > Dim UE As New UnicodeEncoding ()
          > Dim by As Byte() = UE.GetBytes(ext 14)
          > Return Convert.ToBase6 4String(by)
          > End Function
          >
          > Private Function unhashext14(ByV al hash As String) As String
          > On Error GoTo Err
          > If hash = "" Or hash = Nothing Then Return Nothing : Exit Function
          > Dim b As Byte
          > Dim q = Convert.FromBas e64String(hash)
          > Dim ret As String
          > For Each b In q
          > If b <> 0 Then
          >
          > ret = ret + Chr(b)
          > End If
          > Next
          > Return ret
          > Err:
          > Return Nothing : Exit Function
          >
          > End Function[/color]


          Comment

          Working...