0

I have a question and I would be very grateful if someone can answer it for me.

In some programming languages like Csharp or Python, strings can be converted to "byte arrays". I don't understand how this can be possible because internally for a computer everything is bytes, what we call strings or texts are just graphical representations for humans of the same bytes. So if everything is bytes for the computer, why is there the possibility of converting strings to "byte array" in python or csharp??? for the machine would this be the same as converting bytes to bytes?

Thank you in advance for the answers...

  • Check out [this](https://stackoverflow.com/a/22231106/12326283) thread dealing with the use cases of `bytearray` in Python. – gmdev Jul 04 '21 at 00:31
  • A character can be more than a byte... they can be up to 4 bytes. So the complexity with characters becomes not from converting a byte to its string representation, its what format it is in and decoding that format – TheGeneral Jul 04 '21 at 00:47
  • The opposite is very useful, so why not the opposite of the opposite ? ;) – aybe Jul 04 '21 at 02:44

1 Answers1

0

Because all over the world the ASCII (or UTF or Unicode) character encoding standards are used. Each character on the back side is equal to some byte value.

ASCII table: enter image description here

Example in c#:

       static void Main(string[] args)
        {
            var myString = "stackoverflow";
            var myStringBytes = Encoding.ASCII.GetBytes(myString);
            var result = string.Join(", ", myStringBytes);
            Console.WriteLine(result);
            Console.ReadKey();
        }

result: {115, 116, 97, 99, 107, 111, 118, 101, 114, 102, 108, 111, 119} because, "s" char's ascii code equal to 115, "t" char's ascii code equal to 116 etc.

ASCII standart

Mansur Kurtov
  • 726
  • 1
  • 4
  • 12