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

D

David Bargna

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
 
A

Armin Zingler

David Bargna said:
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"
?
 
H

Herfried K. Wagner [MVP]

Hello,

David Bargna said:
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.
 
D

David Bargna

Herfried K. Wagner said:
Hello,



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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top