Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Byte[], alternate null chars

Byte[], alternate null chars

Scheduled Pinned Locked Moved C#
databasecsharpdotnetdata-structuresxml
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Malcolm Smart
    wrote on last edited by
    #1

    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 of

    byte[] 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 a Replace("\0" , "") which is really professional! Regards

    Knowledge 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

    B 1 Reply Last reply
    0
    • M Malcolm Smart

      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 of

      byte[] 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 a Replace("\0" , "") which is really professional! Regards

      Knowledge 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

      B Offline
      B Offline
      Bekjong
      wrote on last edited by
      #2

      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!

      M 1 Reply Last reply
      0
      • B Bekjong

        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!

        M Offline
        M Offline
        Malcolm Smart
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • M Malcolm Smart

          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

          B Offline
          B Offline
          Bekjong
          wrote on last edited by
          #4

          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!

          M 1 Reply Last reply
          0
          • B Bekjong

            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!

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #5

            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

            B 1 Reply Last reply
            0
            • M Malcolm Smart

              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

              B Offline
              B Offline
              Bekjong
              wrote on last edited by
              #6

              Glad to help. Not so emarrasing, I've spent most of my day looking for an off-by-one bug.

              Standards are great! Everybody should have one!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups