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

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

Post by davi » Fri, 26 Sep 2003 23:24:54


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
 
 
 

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

Post by Armin Zing » Sat, 27 Sep 2003 00:13:04

"David Bargna" < XXXX@XXXXX.COM > schrieb

How do you get
"272010101080108011103208701110114010801000"
?




--
Armin

 
 
 

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

Post by Herfried K » Sat, 27 Sep 2003 01:32:31

Hello,

"David Bargna" < XXXX@XXXXX.COM > schrieb:

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

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.yqcomputer.com/
 
 
 

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

Post by davi » Tue, 07 Oct 2003 20:11:56


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
 
 
 

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

Post by Jay B. Har » Wed, 08 Oct 2003 00:02:53

David,
In case you didn't know, the 'Exit Function' in each of your functions are
not doing anything, you can delete them!


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

Consider using OrElse here:
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