Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

Binary Integer variable in Delphi?

Hello.. a 3rd party sort routine (DLL) I am trying to call from my Delphi program, has a field that requires to be passed to it a 2-byte BINARY integer value. Does such a variable type exist in Delphi?

Regards,
  Shawn Halfpenny
  drumme59@sprint.ca
0
aztec
Asked:
aztec
  • 6
  • 4
  • 2
  • +2
1 Solution
 
julio011597Commented:
WORD is the built-in assembler 2-byte integer type. But, i cannot understand what you mean with "Binary".

Regards.
0
 
aztecAuthor Commented:
By binary, I mean that when the variables value is written to a file, it looks like garbage characters. ie. it doesn't write the value as plain ascii, it is unreadable.

Regards,
   Shawn
0
 
julio011597Commented:
How are you writing it out?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TheSwineCommented:
I beleive they mean : WordBool.
0
 
aztecAuthor Commented:
I just tried Wordbool...I try to assign it an integer value and it doesn't compile right. It gives a "Incompatible Types - WordBool and Integer".
 
   OK, here is exactly what I'm trying to do: I'm trying to use a 3rd party sort routine (a Watcom C DLL) which will sort variable length records. It requires that the format of the record be as such:
  - a 2-byte unsigned integer field called an RDW (Record Descriptor Word) which indicates the length of the whole record in bytes.
  - then the data, whose length is dictated by the above RDW.

A sample record from my file looks like this:
16this is the data

I create the data file like so:

(inrec contains the string 'this is the data', outbuf is declared as an array of char[0..99]. Reclen is declared as type 'Word')

    reclen:=length(inrec);
    hinrec:=IntToStr(reclen)+ inrec;
    StrPCopy(outbuf, hinrec);
    writertn:=_lwrite(MyFileHandle, outbuf, reclen+2);

..maybe the problem is where I convert the reclen value to a string with the 'IntToStr' function?? Maybe this 'de-binary-izes' it?? I don't know how else to do this...


..It generated an error within the sort utility. I asked the creator and he said the RDW field must be a 2-byte binary integer field. So if you were looking at this record in NotePad, for instance, the first 2 bytes would be garbage-looking (unreadable) characters. He said I'm getting an error because of the "endian-ness" of my computer (an Intel-based) machine. He said it's a 'Little-Endian' machine and because of that the RDW value must be passed in binary format, or else the sort routine will reverse the bit order of the RDW and get it all screwed up. Speaking of screwed up, he's got ME all screwed up with what he's talking about!

Regards,
   Shawn Halfpenny
   drumme59@sprint.ca

0
 
julio011597Commented:
Now it makes sense.

An integer is _already_ a binary number, at least from an inner point of view. The point is that low-endian machines consider the first byte of a number to be the high-order one, while high-endian machines consider it to be the low-order one - or was it the opposite? anyway, doesn't matter here, since all you have to do is swap the two bytes before calling the DLL function.

And... your hinrec needs that number as a number (i.e., not converted to its decimal representation, so they talk about binary representation), so your code should become something like:

--//--
reclen := length(inrec);
hinrec := (PChar(@reclen)+1)^ + PChar(@reclen)^ + inrec;
StrPCopy(outbuf, hinrec);
writertn :=_lwrite(MyFileHandle, outbuf, reclen+2);
--//--

BTW, reclen+2 does not take into account the null-terminator... just to be sure you are aware of it.

HTH.

P.S. if this is what you need, as i guess, will you mind rejecting TheSwine's answer to let me submit this as the answer? Thanks.
0
 
aztecAuthor Commented:
Julio...
  Thanks for the explanation and the code but it doesn't seem to produce any output to my file. The line:

hinrec := (PChar(@reclen)+1)^ + PChar(@reclen)^ + inrec;

..assigns a null string '' to hinrec... even though reclen contains a number (16), and inrec contains a string 'this is the data'. I have Reclen declared as a variable of type Word. So when I run the resultant file thru the sort DLL it stops on the first record and says it's too short...only 2 bytes (..no doubt a CR/LF combination). When I look at the resultant file, it's empty. Yet - curiously enough - when the file size is 165 bytes..just around what it should be. What's happenning here?

Regards,
   Shawn Halfpenny
0
 
julio011597Commented:
Humm, yes that's right too - i didn't think of it.

To be clear, 16 is 0x0010 which, swapped, becomes 0x1000... but the 0x00 (i.e. char #0) is considered a string terminator as far as null-terminated strings are involved, so that both the assignments to hinrec and to outbuf don't work as expected.

The final fix - i hope :)

--//--
reclen := length(inrec);
outbuf[0] := (PChar(@reclen)+1)^;
outbuf[1] := PChar(@reclen)^;
StrPCopy(outbuf + 2, inrec);
writertn :=_lwrite(MyFileHandle, outbuf, reclen+2);
--//--
0
 
aztecAuthor Commented:
I reject the Swine's answer and choose Julio's answer. Please Give him an 'A'

Cheers
   Shawn Halfpenny
0
 
extremeCommented:
What you need is the type "word", that is an unsigned 16 bits integer.
0
 
julio011597Commented:
Thanks a lot 'extreme'... i someway didn't receive last comment notification so didn't submit the answer, and you've been *very* professional.

Even if 50x4 points are not so important, i guess i'm going to notify EE stuff about this... hope you don't mind.
0
 
linda101698Commented:
aztec,
Please help out by rejecting extreme's  answer as step 1.  Then add a comment requesting that julio post an answer so you can award him the points.  The feature where you add a comment when you reject an answer does not seem to be working.

Linda Gardner
Customer Service @ Experts Exchange
0
 
extremeCommented:
Sorry Julio, I don't readed the last comments, I readed the first ones and I saw that there was no answer to the question, so I went down and answered fast before leaving the net. Again, sorry, don't was my intention inconvenience you.
0
 
julio011597Commented:
Never mind extreme.

As a general suggestion, have a look to a question history - if any - before answering a question; actually, my very first comment already was "use WORD", so there had to be something more.

See you around.

-julio
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now