Byte() to String and String to Byte(). How?

T

ThunderMusic

Hi,
I have to go from Byte() to String, do some processing then reconvert the
String to byte() but using ascii format, not unicode.

I currently use a stream to write the char() (BinaryWriter.Write) from the
string (String.ToCharArray), then use Stream.ToArray to convert everything
to byte(). It works most of the time, but it happens that an error tells me
something like "Additional information: Caractère de substitution faible
trouvé non précédé d'un substitut étendu à l'index : 409. Cette entrée peut
ne pas se trouver dans ce codage ou peut ne pas contenir des caractères
Unicode (UTF-16) valides." in french, so it could look something like
"Additional information: weak substitution character not preceded with an
extended substiture at the index: 409. The entry may not be found in this
coding or may not contain valid Unicode (UTF-16) characters." in english.

Can someone tell me what does it mean and what is the solution? is there
another way to do what I'm doing (I hope there is)?

Thanks

ThunderMusic
 
H

Herfried K. Wagner [MVP]

ThunderMusic said:
I have to go from Byte() to String, do some processing then reconvert the
String to byte() but using ascii format, not unicode.

'System.Text.Encoding.<encoding>.{GetString, GetBytes}'.
 
T

ThunderMusic

Hi,

ok, I tried UnicodeEncoding and UTf8Encoding, but it does not work as I
would like it to work. You see, the way it works right now, if I use Unicode
Encoding, the resulting string is 4096 bytes long and if I use UTF8Encoding,
the resulting string is 4594 bytes long starting from a 8195 bytes long byte
array. In this array, each byte is containing a useful character, even if
this character is not between a and z or anything like that, each byte is
important, so I really need something that will convert directly this byte
array into a string where I can use string.indexof and all those things or
something similar to it.

Maybe some of you have other ideas on how I should do this, I explain the
case a little : I connect to a socket, this socket sends me a byte array. In
this byte array, there are some useful infos I have to search for, so in a
string format I use TheString.IndexOf("InfoToSearch", 0) and it should find
"InfoToSeach" and return me the position where it is in the string. If there
is a way to do the same thing using directly the Byte() or a Stream of some
sort, Fine! please thell me, but now, it's the only way I found. :(

Please help!! I really need it.

Thanks
 
J

Jay B. Harlow [MVP - Outlook]

ThunderMusic,
You really need to use the encoding that is being sent to you.

The "easiest" way may be to use System.Text.Encoding.Default as the
encoding, as it uses 8-bit character code points.

| if I use Unicode
| Encoding, the resulting string is 4096 bytes long and if I use
UTF8Encoding,
| the resulting string is 4594 bytes long starting from a 8195 bytes long
byte
That sounds about right, as UnicodeEncoding is UTF-16 which means that each
"character" is stored in 2 bytes. Where as UTF8Encoding is UTF-8 which means
that each "character" is stored in 1, 2, 3, and sometimes 4 bytes.

System.Text.Encoding.Default is your ANSI encoding as set under Windows
Control Panel Regional settings. ANSI is an 8-bit encoding, so each byte
represents a single character. The "danger" of using Encoding.Default is
characters being translated incorrectly...



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"ThunderMusic" <NO.danlat.at.hotmail.com.SPAM> wrote in message
| Hi,
|
| ok, I tried UnicodeEncoding and UTf8Encoding, but it does not work as I
| would like it to work. You see, the way it works right now, if I use
Unicode
| Encoding, the resulting string is 4096 bytes long and if I use
UTF8Encoding,
| the resulting string is 4594 bytes long starting from a 8195 bytes long
byte
| array. In this array, each byte is containing a useful character, even if
| this character is not between a and z or anything like that, each byte is
| important, so I really need something that will convert directly this byte
| array into a string where I can use string.indexof and all those things or
| something similar to it.
|
| Maybe some of you have other ideas on how I should do this, I explain the
| case a little : I connect to a socket, this socket sends me a byte array.
In
| this byte array, there are some useful infos I have to search for, so in a
| string format I use TheString.IndexOf("InfoToSearch", 0) and it should
find
| "InfoToSeach" and return me the position where it is in the string. If
there
| is a way to do the same thing using directly the Byte() or a Stream of
some
| sort, Fine! please thell me, but now, it's the only way I found. :(
|
| Please help!! I really need it.
|
| Thanks
|
| "Herfried K. Wagner [MVP]" <[email protected]> a écrit dans le
| message de [email protected]...
| > "ThunderMusic" <NO.danlat.at.hotmail.com.SPAM> schrieb:
| >> I have to go from Byte() to String, do some processing then reconvert
the
| >> String to byte() but using ascii format, not unicode.
| >
| > 'System.Text.Encoding.<encoding>.{GetString, GetBytes}'.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
 
T

ThunderMusic

ok, thanks I'll try this right away... ;)
Jay B. Harlow said:
ThunderMusic,
You really need to use the encoding that is being sent to you.

The "easiest" way may be to use System.Text.Encoding.Default as the
encoding, as it uses 8-bit character code points.

| if I use Unicode
| Encoding, the resulting string is 4096 bytes long and if I use
UTF8Encoding,
| the resulting string is 4594 bytes long starting from a 8195 bytes long
byte
That sounds about right, as UnicodeEncoding is UTF-16 which means that
each
"character" is stored in 2 bytes. Where as UTF8Encoding is UTF-8 which
means
that each "character" is stored in 1, 2, 3, and sometimes 4 bytes.

System.Text.Encoding.Default is your ANSI encoding as set under Windows
Control Panel Regional settings. ANSI is an 8-bit encoding, so each byte
represents a single character. The "danger" of using Encoding.Default is
characters being translated incorrectly...



--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"ThunderMusic" <NO.danlat.at.hotmail.com.SPAM> wrote in message
| Hi,
|
| ok, I tried UnicodeEncoding and UTf8Encoding, but it does not work as I
| would like it to work. You see, the way it works right now, if I use
Unicode
| Encoding, the resulting string is 4096 bytes long and if I use
UTF8Encoding,
| the resulting string is 4594 bytes long starting from a 8195 bytes long
byte
| array. In this array, each byte is containing a useful character, even
if
| this character is not between a and z or anything like that, each byte
is
| important, so I really need something that will convert directly this
byte
| array into a string where I can use string.indexof and all those things
or
| something similar to it.
|
| Maybe some of you have other ideas on how I should do this, I explain
the
| case a little : I connect to a socket, this socket sends me a byte
array.
In
| this byte array, there are some useful infos I have to search for, so in
a
| string format I use TheString.IndexOf("InfoToSearch", 0) and it should
find
| "InfoToSeach" and return me the position where it is in the string. If
there
| is a way to do the same thing using directly the Byte() or a Stream of
some
| sort, Fine! please thell me, but now, it's the only way I found. :(
|
| Please help!! I really need it.
|
| Thanks
|
| "Herfried K. Wagner [MVP]" <[email protected]> a écrit dans le
| message de [email protected]...
| > "ThunderMusic" <NO.danlat.at.hotmail.com.SPAM> schrieb:
| >> I have to go from Byte() to String, do some processing then reconvert
the
| >> String to byte() but using ascii format, not unicode.
| >
| > 'System.Text.Encoding.<encoding>.{GetString, GetBytes}'.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
 
Top