1

I'm working on an audio library in C# and am working on the WAVE file format ATM. According to the very informative website that I am using, WAVE using Pascal style strings. I have been Googling for over 20 minutes and have come up only with a C to Pascal converter.

I know how Pascal and C strings compare (C code):

char *cStr = "Test"; // C - produces {'T', 'e', 's', 't', '\0'}
char pascal[] = {4, 'T', 'e', 's', 't'}; // Pascal

If it helps, I am using System.IO.BinaryReader to parse the file.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
  • 1
    Given the information above, what's the question? :) Imagine ending a Pascal string with '\0' as well, and the the "C string" is just the pointer to the 2nd char in the Pascal string... (if "unsafe") –  Jun 07 '12 at 04:50
  • You *provided the key difference in the post*: so what is the question except for "write the code for me"? –  Jun 07 '12 at 04:52
  • @pst I don't understand how to convert them. – Cole Tobin Jun 07 '12 at 04:52
  • Well, what have you tried? Did it work? If not, why not? –  Jun 07 '12 at 04:53
  • 1
    That Pascal info is awfully old, and outdated since mid nineties. It's for the UCSD like stringtype used by Turbo Pascal. Most modern Pascal compilers still support it, but that is out of compatibility, they hardly use it – Marco van de Voort Jun 08 '12 at 07:16
  • @MarcovandeVoort all i care about is that that is how WAVE uses them. – Cole Tobin Jun 08 '12 at 07:21

2 Answers2

3

Well if you are using BinaryReader this should be pretty easy.

var size = rd.ReadByte();
var body = rd.ReadBytes(size);
var text = System.Text.Encoding.ASCII.GetString(body);
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
1

The basic idea behind a Pascal string is that the first byte holds the string length, and subsequent bytes hold the string data (limiting strings to 255 characters).

I would work with a byte array and use the string to byte conversion methods in C# to populate it. See the accepted answer of the question below to understand how to convert a C# string to a byte array, and remember to offset everything 1 array position to allow room for the 0th byte, holding the string length, and remember to populate the 0th byte with the actual string length. Check your input string length to make sure it's not over 255 characters :-)

How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Seems like the size should be 7-bit variable length. Limiting a string to 255 seems rather arbitrary. – ChaosPandion Jun 07 '12 at 04:54
  • @ChaosPandion there were rumors of expanding it to 2 bytes. Pascal not using variable-length and C using that format also seems to be the worst 2-byte mistake ever made. – Cole Tobin Jun 07 '12 at 04:56
  • @ChaosPandion: Seems like it should, maybe, but it's not. The format was defined a long time ago, when the intent was to use Pascal as a teaching language for structured programming. – Eric J. Jun 07 '12 at 05:15
  • Actually not. It was used in UCSD which was a bytecode interpreter on microcomputers. Probably for VM efficiency. They are called UCSD strings formally, since various Pascal compilers support many stringtypes (Delphi up to 20) – Marco van de Voort Jun 08 '12 at 08:00