469,950 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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 = 272010101080108011103208701110114010801000
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
Nov 20 '05 #1
4 13145
"David Bargna" <da***@vista.free-online.co.uk> schrieb
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 = 272010101080108011103208701110114010801000
How do you get
"272010101080108011103208701110114010801000"
?

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

--
Armin

Nov 20 '05 #2
Hello,

"David Bargna" <da***@vista.free-online.co.uk> schrieb:

in psuedo

1.str="Hello World"
2.convert str to byte()
3.convert byte() to str2 = 272010101080108011103208701110114010801000
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


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

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #3
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message news:<Or**************@TK2MSFTNGP12.phx.gbl>...
Hello,

"David Bargna" <da***@vista.free-online.co.uk> schrieb:

in psuedo

1.str="Hello World"
2.convert str to byte()
3.convert byte() to str2 = 272010101080108011103208701110114010801000
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


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


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(ext14)
Return Convert.ToBase64String(by)
End Function

Private Function unhashext14(ByVal 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.FromBase64String(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
Nov 20 '05 #4
David,
In case you didn't know, the 'Exit Function' in each of your functions are
not doing anything, you can delete them!
If ext14 = "" Then Return Nothing : Exit Function
If hash = "" Or hash = Nothing Then Return Nothing : Exit Function
Return Nothing : Exit Function
The Return statement itself will exit the function, so the " : Exit
Function" is dead code!

Consider using OrElse here: If hash = "" Or hash = Nothing Then Return Nothing 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
NullReferenceException. The OrElse won't bother checking hash.Length when
hash is Nothing.

Hope this helps
Jay

"David Bargna" <da***@vista.free-online.co.uk> wrote in message
news:89**************************@posting.google.c om... "Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message

news:<Or**************@TK2MSFTNGP12.phx.gbl>...
Hello,

"David Bargna" <da***@vista.free-online.co.uk> schrieb:

in psuedo

1.str="Hello World"
2.convert str to byte()
3.convert byte() to str2 = 272010101080108011103208701110114010801000
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


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


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(ext14)
Return Convert.ToBase64String(by)
End Function

Private Function unhashext14(ByVal 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.FromBase64String(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

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Darrel | last post: by
1 post views Thread by Eugene Anthony | last post: by
10 posts views Thread by Shafik | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.