473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem converting byte() to string and then back to byte()

I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array. However, its
getting scrambled in the conversion. In short, here's the code:

======================================
Dim textConverter As New ASCIIEncoding
Dim sParam As String = "This is my cool param"
Dim bytParam() As Byte

'load the byte array here...

'convert byte() to string
sParam = textConverter.GetString(bytParam)
'convert string to byte()
bytParam = textConverter.GetBytes(sParam)
=====================================

bytParam is not the same as it was before it was converted to a string.
How can I do this and preserve the byte() to exactly what it was when I
started?

Here's bytParams before converting to a string: (copied from the watch
window)
bytParam {Length=32} Byte()
0 27 Byte
-1 163 Byte
-2 198 Byte
-3 42 Byte
-4 46 Byte
-5 191 Byte
-6 236 Byte
-7 250 Byte
-8 163 Byte
-9 211 Byte
-10 222 Byte
-11 98 Byte
-12 31 Byte
-13 86 Byte
-14 223 Byte
-15 64 Byte
-16 164 Byte
-17 197 Byte
-18 1 Byte
-19 180 Byte
-20 102 Byte
-21 129 Byte
-22 246 Byte
-23 148 Byte
-24 204 Byte
-25 35 Byte
-26 8 Byte
-27 15 Byte
-28 36 Byte
-29 31 Byte
-30 1 Byte
-31 253 Byte
and here it is after restoring it back from a string:
bytParam {Length=32} Byte()
0 27 Byte
-1 35 Byte
-2 70 Byte
-3 42 Byte
-4 46 Byte
-5 63 Byte
-6 108 Byte
-7 122 Byte
-8 35 Byte
-9 83 Byte
-10 94 Byte
-11 98 Byte
-12 31 Byte
-13 86 Byte
-14 95 Byte
-15 64 Byte
-16 36 Byte
-17 69 Byte
-18 1 Byte
-19 52 Byte
-20 102 Byte
-21 1 Byte
-22 118 Byte
-23 20 Byte
-24 76 Byte
-25 35 Byte
-26 8 Byte
-27 15 Byte
-28 36 Byte
-29 31 Byte
-30 1 Byte
-31 125 Byte
Thanks!
--
mo*******@nospam.nospam
Nov 21 '05 #1
8 4166
>I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array.


Consider formatting each byte as two hex characters or base64 encode
with the Convert class.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
Hi

I agree with Mattias's suggestion. You may have a try.
Also what is the process of your application.
I assume your client is a winform application, after convert the byte array
as an string, it will be passed as a query
string(URL,http://host/test.aspx?id=querystring), or it will be data in a
form POST?( I assume the server side is an ASPX page)

Which method do you use to pass the data, via webbrowser or httpwebrequest?
Also you may try to use the HttpUtility class which use the UrlEncode and
UrlDecode.

If you still have any concern, please post the information above, so that
we can figure out your scenario correctly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #3
Thanks. I'm looking into your suggestion but need some further help. To
clarify:

I have vb.net 1.1 winforms app and I want to call reports from sql server
reporting services. The problem is that this is confidential data and SSRS
has a number of security issues which I wont discuss here. but in short, I
will assigning the required credentials to the report on the server side
where things are secure. Now all I need to do is encrypt the parameters
being passed to the server. I've tried a number of different ways of
fetching reports including web services, but have been running into major
issues on each one. The most reliable way of getting the report is by
calling a URL that will return a web page with the SSRS report in it.

So here's what I'm doing:

I use one generic page for all reports and pass a dataset as a parameter
which as information such as which report to return and any number of
parameters needed by the report. the steps are:

1) Build the dataset in the smart client
2) convert the dataset to a string like this: Dim str as string = ds.getxml
3) encrypt the string to a byte array: Dim byt() as byte =
MyEncryptMethod(str)
4) convert byt to a string so I can pass it as a querystring in the URL: Dim
sParam as string = SomeMethodToConvertByteToString(byt)
5) Call the url:
System.Diagnostics.Process.Start("http://localhost/wsVIPN/Reports/VIPNReportStandard.aspx?Param="
& sParam)

On the server side in the web page load event

6) Receive the querystring: Dim sParam As String =
Request.QueryString("Param")
7) Convert it back to a byte() so I can decrypt it
8) Decrypt it: Dim str as string = MyDecryptMethod(byt)
9) Convert the string back to the dataset: ds.ReadXml(str )
10) Now that my params are back in the dataset I can process the report.

I could really use some help and specific code examples on these
conversions. I tried your idea to use UrlEncode, however, I was doing it in
the smart client and could not find the right namespace to get it to
compile.

I hope you can follow this.

Thanks again!

--
mo*******@nospam.nospam
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:iA*************@TK2MSFTNGXA01.phx.gbl...
Hi

I agree with Mattias's suggestion. You may have a try.
Also what is the process of your application.
I assume your client is a winform application, after convert the byte
array
as an string, it will be passed as a query
string(URL,http://host/test.aspx?id=querystring), or it will be data in a
form POST?( I assume the server side is an ASPX page)

Which method do you use to pass the data, via webbrowser or
httpwebrequest?
Also you may try to use the HttpUtility class which use the UrlEncode and
UrlDecode.

If you still have any concern, please post the information above, so that
we can figure out your scenario correctly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #4
Try this:

Dim bytParam1() As Byte = New Byte() {27, 163, 198, 42, 46, 191, 236, 250,
163, 211, 222, 98, 31, 86, 223, 64, 164, 197, 1, 180, 102, 129, 246, 148,
204, 35, 8, 15, 36, 31, 1, 253}

Dim sParam As String = System.Text.Encoding.Default.GetString(bytParam1)

Dim bytParam2() As Byte = System.Text.Encoding.Default.GetBytes(sParam)

Console.WriteLine("bytParam1.Length = {0}", bytParam1.Length)

Console.WriteLine("bytParam2.Length = {0}", bytParam2.Length)

Console.WriteLine()

Console.WriteLine("Position bytParam1 bytParam2")

Console.WriteLine("-------- --------- ---------")

For _i As Integer = 0 To bytParam1.Length - 1
Console.WriteLine("{0,2} {1,3} {2,3}", _i, bytParam1(_i),
bytParam2(_i))
Next

What you were doing was using ASCII encoding instead of ANSI encoding. Not,
in your results, that each original value that was greater than 127 appeared
to have had 128 subtracted from it. The ASCII character set runs from 0 to
127 whereas the ANSI character set runs from 0 to 256.

If you need to use a DBCS then you need to use the appropriate one of the
other encoding formats.
"moondaddy" <mo*******@nospam.nospam> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
Thanks. I'm looking into your suggestion but need some further help. To
clarify:

I have vb.net 1.1 winforms app and I want to call reports from sql server
reporting services. The problem is that this is confidential data and
SSRS has a number of security issues which I wont discuss here. but in
short, I will assigning the required credentials to the report on the
server side where things are secure. Now all I need to do is encrypt the
parameters being passed to the server. I've tried a number of different
ways of fetching reports including web services, but have been running
into major issues on each one. The most reliable way of getting the
report is by calling a URL that will return a web page with the SSRS
report in it.

So here's what I'm doing:

I use one generic page for all reports and pass a dataset as a parameter
which as information such as which report to return and any number of
parameters needed by the report. the steps are:

1) Build the dataset in the smart client
2) convert the dataset to a string like this: Dim str as string =
ds.getxml
3) encrypt the string to a byte array: Dim byt() as byte =
MyEncryptMethod(str)
4) convert byt to a string so I can pass it as a querystring in the URL:
Dim sParam as string = SomeMethodToConvertByteToString(byt)
5) Call the url:
System.Diagnostics.Process.Start("http://localhost/wsVIPN/Reports/VIPNReportStandard.aspx?Param="
& sParam)

On the server side in the web page load event

6) Receive the querystring: Dim sParam As String =
Request.QueryString("Param")
7) Convert it back to a byte() so I can decrypt it
8) Decrypt it: Dim str as string = MyDecryptMethod(byt)
9) Convert the string back to the dataset: ds.ReadXml(str )
10) Now that my params are back in the dataset I can process the report.

I could really use some help and specific code examples on these
conversions. I tried your idea to use UrlEncode, however, I was doing it
in the smart client and could not find the right namespace to get it to
compile.

I hope you can follow this.

Thanks again!

--
mo*******@nospam.nospam
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:iA*************@TK2MSFTNGXA01.phx.gbl...
Hi

I agree with Mattias's suggestion. You may have a try.
Also what is the process of your application.
I assume your client is a winform application, after convert the byte
array
as an string, it will be passed as a query
string(URL,http://host/test.aspx?id=querystring), or it will be data in
a
form POST?( I assume the server side is an ASPX page)

Which method do you use to pass the data, via webbrowser or
httpwebrequest?
Also you may try to use the HttpUtility class which use the UrlEncode and
UrlDecode.

If you still have any concern, please post the information above, so that
we can figure out your scenario correctly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.


Nov 21 '05 #5
Dim sParam As String = System.Text.Encoding.Default.GetString(bytParam1)


That's inappropriate for data that is being sent from a client to a
server, because the default ANSI codepage can be different on
different machines, so there's no guarantee that this will roundtrip
properly. You will also get problems with non printable characters.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #6
I think it's best to leave that point up to the original poster.

The point I was making was that it was his use of ASCII encoding that was
causing his problem.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ut**************@TK2MSFTNGP15.phx.gbl...
Dim sParam As String = System.Text.Encoding.Default.GetString(bytParam1)


That's inappropriate for data that is being sent from a client to a
server, because the default ANSI codepage can be different on
different machines, so there's no guarantee that this will roundtrip
properly. You will also get problems with non printable characters.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #7
Thanks to all. Both of you are brining things to light that I was unaware
of and will also prevent the solution from working correctly (which is what
it's all about). So I need a different way to skin-the-cat. I haven't
gotten into serializing things yet, but this might be the time. Since all
my parameters are wrapped into a dataset. Do you think it would work to
have a simple class that just contained an encrypted copy of this dataset?
then serialize the class into a string and pass it in the URL. And then
reverse engineer everything on the server? I'll start working on this, but
would like some feedback if possible.

Thanks again!

--
mo*******@nospam.nospam
"Stephany Young" <noone@localhost> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I think it's best to leave that point up to the original poster.

The point I was making was that it was his use of ASCII encoding that was
causing his problem.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ut**************@TK2MSFTNGP15.phx.gbl...
Dim sParam As String =
System.Text.Encoding.Default.GetString(bytParam1)


That's inappropriate for data that is being sent from a client to a
server, because the default ANSI codepage can be different on
different machines, so there's no guarantee that this will roundtrip
properly. You will also get problems with non printable characters.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 21 '05 #8
Hi
We need to add reference to System.Web.dll to use the HttpUtility class.
Here is my test about how to pass byte array from winform to asp.net
[Winform side]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bts(127) As Byte
For i As Integer = 0 To 127
bts(i) = i
Next
Dim str As String = HttpUtility.UrlEncode(bts)
Process.Start("http://localhost/WebSideServer/Default.aspx?name=" &
str)
End Sub

[Asp.net side]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write("Start dump Byte
Array============================================= <BR>")
For Each b As Byte In
HttpUtility.UrlDecodeToBytes(Request.QueryString(" name"))
Response.Write(b.ToString() & "<BR>")
Next
Response.Write("End dump Byte
Array============================================= ")
End Sub

Also use QueryString approach has its limitatio you may pay attention.
What is the limit on QueryString / GET / URL parameters?
http://www.aspfaq.com/show.asp?id=2222

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
5
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
7
by: john | last post by:
On my form i have a message box called txtItemDesc that displays the french phrase qualité Père Noël. Now then when i run this code on that text box: Dim chrArr() As Char chrArr =...
3
by: Joshua Russell | last post by:
Hi, I've got a program (see source below) that makes a file and fills it with random binary values (from 0 to 255). The source below works, however the program creates files at a rate of about...
6
by: clintp | last post by:
I have a byte array that contains 8-bit ascii characters. I'm not particular about the codepage used to display them, but I have to preserve the position in the string with something and be able...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: Tiraman :-\) | last post by:
Hi Everyone, i have the following problem in my client-server Application My server take array and serialize it into the memorystream Dim ns As NetworkStream = client.GetStream() Dim writer...
21
by: Mike | last post by:
I am having a problem getting AND to work. I have a MySQL database field defined as varbinary(8) with X00 in it. Then I have define statements with X01 in it. define('CERTACCESS_MEDALS', x01); ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.