Solved

Binary Integer variable in Delphi?

Posted on 1998-04-04
14
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1361285
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
ID: 1361286
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
ID: 1361287
How are you writing it out?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:TheSwine
ID: 1361288
I beleive they mean : WordBool.
0
 

Author Comment

by:aztec
ID: 1361289
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
ID: 1361290
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
ID: 1361291
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1361292
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
ID: 1361293
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
ID: 1361294
What you need is the type "word", that is an unsigned 16 bits integer.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1361295
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
ID: 1361296
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
ID: 1361297
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
ID: 1361298
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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