Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a String for Data Storage

Status
Not open for further replies.

JES1904

Programmer
Jul 7, 2008
36
US
I am working on a project for a Cad program in which I must embed data in the Cad file. The tools for doing so in the cad program allow for embedding boolean, integer, double byte and string values, one at a time. There is a lot of data and the best way to store it would be in a single packet. The best option then appears to be using strings. The string data type in the Cad program is limited to 255 characters. I could enter the data in the string as comma delimited items, but wonder if it could be packed tighter by assembling the data as individual byte packets in a string and storing it that way. My instincts tell me that won't work in in pascal/delphi, because of carriage returns and linefeeds etc., but I am not sure. Can anyone shed some light on this or steer me to some resources / samples that could help?

Thanks, Joe
 
Not entirely sure what you are getting at, but it is entirely possible to work with individual characters, using the byte type and the ord and chr functions.

An app I am working on right now builds strings from bytes.
Here's a snippet..
Code:
// building array from string rxd from serial comms
 for bytes := 0 to 239 do
         begin
           Data[Bytes] := ord(str[5 + bytes]);
           CheckSum := CheckSum + data[bytes];

         end;

// extracting string data from the array
  
   repeat
      for a := 2 to 5 do
      S := S + chr(Data[Index + b + a]);
      inc(b, 6);
   until b = 24;
  NameString[Product] := S;
  Memo.Lines.Add('Product '+ inttostr(Product)+ ' '+S); 
 end;

You can strip off CRs and LFs using the Trim functions.
and Delphi includes the 'char' data type to be honest there isn't much that you cannot do with strings and characters in Delphi, its even possible to simulate 'C' type data storage (very compact) using Packed arrays etc.




Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
I may halve answered my question. I forgot that o character in shortstring sets length of the string and thought it was terminated by CR and/or LF. That would mean that my data could be truncated if those characters occured in the data string.

Thanks for your snippet. I assume that Data is simply an array of bute. I'm going to leave this thread unsolved for a bit to see if anyone else can expand upon the issue.
 
Sggaunt,

How do you get an integer or float into the data array of byte to begin with?
 
If you want to store larger data types, just split the values up using bit manipulation,

Code:
L: integer;
MSB, LSB: byte; // could be an array

MSB := (L and $FF00) >> 8;// mask top four bits and shift into the lower nybble
LSB  := (L and $FF); //in fact you could just assign this the AND is just for completeness.
etc..

floating point is a little more involved, but basically you just store the value as before. Determine the sign and store this as either another byte (0 = negative 1 = positive) or you could use a single bit to indicate the sign masking this from the rest of the value.







Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
JES1904 said:
I forgot that o character in shortstring sets length of the string and thought it was terminated by CR and/or LF. That would mean that my data could be truncated if those characters occured in the data string.

From my understanding, that's only if you were to write the string. Though I might have to play with doing this to be sure.

JES1904 said:
How do you get an integer or float into the data array of byte to begin with?

Couldn't you establish a redefinition of the string field via a pointer to the record type you want to store?

Measurement is not management.
 
me said:
Couldn't you establish a redefinition of the string field via a pointer to the record type you want to store?

Example:
Code:
{$APPTYPE CONSOLE}
program play1; uses sysutils;
  { string encoding of record type }
  type
    string250 = string[250];
    string50 = string[50];
    myrecordtype = packed record
      int01: integer;
      ext01: extended;
      str01: string50;
    end;
    pmyrecordtype = ^myrecordtype;

  var
    mystring: string250;
    myrecptr: pmyrecordtype;
    testfile: file;

  begin
    writeln('myrecordtype length is: ', sizeof(myrecordtype));
    writeln('myrecordtype.int01: ', sizeof(integer));
    writeln('myrecordtype.ext01: ', sizeof(extended));
    writeln('myrecordtype.str01: ', sizeof(string50));
    SetLength(mystring, sizeof(myrecordtype));
    myrecptr := @mystring[1];
    writeln('mystring size is : ', sizeof(mystring));

    myrecptr^.int01 := $1013; // to get CR/LF
    myrecptr^.ext01 := 3.233233;
    myrecptr^.str01 := 'Charles walked to the bank today.';

    assign(testfile, 'MYTEST.FIL');
    rewrite(testfile, 1);
    blockwrite(testfile, mystring, sizeof(mystring));
    close(testfile);

    writeln('trying to write mystring', mystring);
    readln;
  end.

The pleasant surprise to me is that Delphi uses the length byte to determine how many characters to write, so me putting $13$10 in the data didn't stop it from writing to the end of the string record.

Measurement is not management.
 
I also discovered the "Absolute" keyword that allows the declaration of two variable types overlapping in memory.

Var
data array of byte;
Int : Integer absolute data;

aligns the data and int in memory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top