Solved

Binary Integer variable in Delphi?

Posted on 1998-04-04
14
481 Views
Last Modified: 2010-04-06
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
Comment
Question by:aztec
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
WORD is the built-in assembler 2-byte integer type. But, i cannot understand what you mean with "Binary".

Regards.
0
 

Author Comment

by:aztec
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
How are you writing it out?
0
 
LVL 1

Expert Comment

by:TheSwine
Comment Utility
I beleive they mean : WordBool.
0
 

Author Comment

by:aztec
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 

Author Comment

by:aztec
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 

Author Comment

by:aztec
Comment Utility
I reject the Swine's answer and choose Julio's answer. Please Give him an 'A'

Cheers
   Shawn Halfpenny
0
 
LVL 1

Accepted Solution

by:
extreme earned 50 total points
Comment Utility
What you need is the type "word", that is an unsigned 16 bits integer.
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 
LVL 7

Expert Comment

by:linda101698
Comment Utility
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
 
LVL 1

Expert Comment

by:extreme
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now