locked
byte to string to byte RRS feed

  • Question

  • how do i convert a 1 byte to string "representation" of the byte?

    for ex : byte b=55; ==>string s="00110111"

    and the opposite

    string s="10101111" ==> byte b=175;

    Saturday, October 22, 2011 10:23 PM

Answers

  • > string s="10101111" ==> byte b=175;
     
     
    var res = Convert.ToByte("10101111", 2);


    > byte b=55; ==>string s="00110111"

    var res = Convert.ToString((byte)55, 2).PadLeft(8, '0');


    • Edited by Malobukv Saturday, October 22, 2011 11:21 PM
    • Proposed as answer by Malobukv Saturday, October 22, 2011 11:22 PM
    • Marked as answer by pelegk1 Saturday, October 22, 2011 11:39 PM
    Saturday, October 22, 2011 11:00 PM
  • 1. I did an example code whcih converts byte to string (of bits): 

     

            private void YourMethod()
            {
                byte myByte = 55;
                bool[] bits = GetBits(myByte).ToArray();
                string strBits = string.Empty;
                foreach (bool bit in bits)
                    strBits += bit ? 1 : 0;
    
                MessageBox.Show(String.Format("From byte {0} we get bits as: {1}.", myByte, strBits));
            }
    
            IEnumerable<bool> GetBits(byte b)
            {
                for (int i = 0; i < 8; i++)
                {
                    yield return (b & 0x80) != 0;
                    b *= 2;
                }
            }
    ---------------------------------

    2. To convert back to byte you can do:
    int res = Convert.ToInt32(strBits, 2);
    



    Mitja
    • Marked as answer by pelegk1 Saturday, October 22, 2011 11:17 PM
    Saturday, October 22, 2011 11:10 PM
  • You can use the Convert class for both:

    byte b = 55;
    string stringRepresentation = Convert.ToString (b, 2);
    byte byteRepresentation = Convert.ToByte (stringRepresentation, 2);
    
    


    HTH
    --mc

    • Marked as answer by pelegk1 Saturday, October 22, 2011 11:24 PM
    Saturday, October 22, 2011 11:22 PM

All replies

  • > string s="10101111" ==> byte b=175;
     
     
    var res = Convert.ToByte("10101111", 2);


    > byte b=55; ==>string s="00110111"

    var res = Convert.ToString((byte)55, 2).PadLeft(8, '0');


    • Edited by Malobukv Saturday, October 22, 2011 11:21 PM
    • Proposed as answer by Malobukv Saturday, October 22, 2011 11:22 PM
    • Marked as answer by pelegk1 Saturday, October 22, 2011 11:39 PM
    Saturday, October 22, 2011 11:00 PM
  • 1. I did an example code whcih converts byte to string (of bits): 

     

            private void YourMethod()
            {
                byte myByte = 55;
                bool[] bits = GetBits(myByte).ToArray();
                string strBits = string.Empty;
                foreach (bool bit in bits)
                    strBits += bit ? 1 : 0;
    
                MessageBox.Show(String.Format("From byte {0} we get bits as: {1}.", myByte, strBits));
            }
    
            IEnumerable<bool> GetBits(byte b)
            {
                for (int i = 0; i < 8; i++)
                {
                    yield return (b & 0x80) != 0;
                    b *= 2;
                }
            }
    ---------------------------------

    2. To convert back to byte you can do:
    int res = Convert.ToInt32(strBits, 2);
    



    Mitja
    • Marked as answer by pelegk1 Saturday, October 22, 2011 11:17 PM
    Saturday, October 22, 2011 11:10 PM
  • Mitja, can you explain why you did b & 0x80?
    Saturday, October 22, 2011 11:17 PM
  • You can use the Convert class for both:

    byte b = 55;
    string stringRepresentation = Convert.ToString (b, 2);
    byte byteRepresentation = Convert.ToByte (stringRepresentation, 2);
    
    


    HTH
    --mc

    • Marked as answer by pelegk1 Saturday, October 22, 2011 11:24 PM
    Saturday, October 22, 2011 11:22 PM
  • Hi pelegk1, did you see my answer above? it does exactly what you wanted.

    Saturday, October 22, 2011 11:39 PM
  • yes 10 alot all of u:)
    Saturday, October 22, 2011 11:40 PM
  • you are welcome
    Saturday, October 22, 2011 11:41 PM