Byte[], alternate null chars
-
Hi I am passing data between SQL 2005 and a CLR stored procedure developed in C# via the Service Broker, which uses XML as its transport. It's posted here as the problem is more C# than SQL. The message is read in the C# stored procedure using an SQLDataReader. The value, a string, is retrieved from the reader as an array of bytes.
//the original string sent was "5129"; byte[] mystring = (byte[])myReader[2];
mystring
is populated with the ascii values of each character in my string, but interspersed with nulls ( 0s ), the equivalent ofbyte[] mybytes = {53,0,49,0,50,0,57,0};
I can't seem to encode this back to a string. I've tried straight ASCIIEncoding, going via UTF8 (google?) with the following :-
byte[] temp = { 53, 0, 49, 0, 50, 0 , 57 , 0}; byte[] buf = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, temp); //via utf8 string attempt1 = Encoding.UTF8.GetString(buf, 0, 8); //straight from byte array string attempt2 = System.Text.ASCIIEncoding.ASCII.GetString(temp); //attempt1 & attempt2 both hold : "5\01\02\09\0"
What am I missing? To cut a long story short I want to convert
byte[] temp = { 53, 0, 49, 0, 50, 0 , 57 , 0};
to a string like"5129"
. I am currently hacking it with aReplace("\0" , "")
which is really professional! RegardsKnowledge is hereditary, it will find its way up or down. - Luc Pattyn
so you answer don't be scared of failure The only failure is never to try Things You've Never Done - Passenger -2008 -
Hi I am passing data between SQL 2005 and a CLR stored procedure developed in C# via the Service Broker, which uses XML as its transport. It's posted here as the problem is more C# than SQL. The message is read in the C# stored procedure using an SQLDataReader. The value, a string, is retrieved from the reader as an array of bytes.
//the original string sent was "5129"; byte[] mystring = (byte[])myReader[2];
mystring
is populated with the ascii values of each character in my string, but interspersed with nulls ( 0s ), the equivalent ofbyte[] mybytes = {53,0,49,0,50,0,57,0};
I can't seem to encode this back to a string. I've tried straight ASCIIEncoding, going via UTF8 (google?) with the following :-
byte[] temp = { 53, 0, 49, 0, 50, 0 , 57 , 0}; byte[] buf = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, temp); //via utf8 string attempt1 = Encoding.UTF8.GetString(buf, 0, 8); //straight from byte array string attempt2 = System.Text.ASCIIEncoding.ASCII.GetString(temp); //attempt1 & attempt2 both hold : "5\01\02\09\0"
What am I missing? To cut a long story short I want to convert
byte[] temp = { 53, 0, 49, 0, 50, 0 , 57 , 0};
to a string like"5129"
. I am currently hacking it with aReplace("\0" , "")
which is really professional! RegardsKnowledge is hereditary, it will find its way up or down. - Luc Pattyn
so you answer don't be scared of failure The only failure is never to try Things You've Never Done - Passenger -2008Don't know if I get the problem exactly, but this seems to work:
byte\[\] data = { 53, 0, 49, 0, 50, 0, 57, 0 }; byte\[\] filter\_data = Array.FindAll(data, new Predicate<byte>(delegate(byte b) { return b != 0; })); string s = Encoding.ASCII.GetString(filter\_data);
Standards are great! Everybody should have one!
-
Don't know if I get the problem exactly, but this seems to work:
byte\[\] data = { 53, 0, 49, 0, 50, 0, 57, 0 }; byte\[\] filter\_data = Array.FindAll(data, new Predicate<byte>(delegate(byte b) { return b != 0; })); string s = Encoding.ASCII.GetString(filter\_data);
Standards are great! Everybody should have one!
Thanks - it's similar to my Replace - get rid of the nulls and it works. I suppose I am asking if a byte[] encoded string should contain two bytes for each character? And I suppose I should actually go and try that myself!!
Knowledge is hereditary, it will find its way up or down. Luc Pattyn
and since what every time when i want to add button to this control one add two times posted in C# forum -
Thanks - it's similar to my Replace - get rid of the nulls and it works. I suppose I am asking if a byte[] encoded string should contain two bytes for each character? And I suppose I should actually go and try that myself!!
Knowledge is hereditary, it will find its way up or down. Luc Pattyn
and since what every time when i want to add button to this control one add two times posted in C# forum -
Ah ok. In that case, unicode? This gets the right result...
byte[] data = { 53, 0, 49, 0, 50, 0, 57, 0 };
string s = Encoding.Unicode.GetString(data);Standards are great! Everybody should have one!
Bekjong wrote:
Ah ok. In that case, unicode? This gets the right result...
Yes it does. How embarrasingly simply was that? :-O Thank you.
Knowledge is hereditary, it will find its way up or down. Luc Pattyn
and since what every time when i want to add button to this control one add two times posted in C# forum -
Bekjong wrote:
Ah ok. In that case, unicode? This gets the right result...
Yes it does. How embarrasingly simply was that? :-O Thank you.
Knowledge is hereditary, it will find its way up or down. Luc Pattyn
and since what every time when i want to add button to this control one add two times posted in C# forum