Search code examples
stringcomputer-science

What are length prefixed strings and what do they look like when encoded in 8-bit binary?


Here is the problem:

Pascal uses length prefixed strings, where the length of a string is encoded in 8-bit binary and stored before the string. Give the bit string for “BYE!”, encoded in 8-bit ASCII, as it would be encoded in Pascal.

I understand how the string "BYE!" would be encoded in 8-bit ASCII, but I don't understand how this is supposed to look with the length of the string encoded and stored before the string. I also know how to find the decimal equivalent values for each of the characters in the string, but I'm not sure if that is necessary to answer the question.

The string "BYE!" encoded in ASCII is: 'B' = 01000010, 'Y' = 01011001, 'E' = 01000101, '!' = 00100001.

The decimal equivalent for the string "BYE!" is: 'B' = 66, 'Y' = 89, 'E' = 69, '!' = 33.


Solution

  • The length of the string is 4 characters.

    8 bit binary states that the number 4 is represented as 00000100

    Therefore in pascal it should = 00000100 01000010 01011001 01000101 00100001

    8-bit binary for the length of the string is different to 8-bit ascii it wants for the actual string.