Board index » delphi » How to convert Extended ASCII to Binary ?

How to convert Extended ASCII to Binary ?

I am writing a program (D4) to control a  ham radio through the serial
port using AsyncPro.  The radio allows me to send it a 5 byte poling
code and then immediately responds by sending a one (1) byte response
that indicates the status of the receiver or the transmitter.  

Here's the problem:

I can send the poling code and the radio responds by sending the 1
byte back (a numeric value from 00-255 or 00h-FFh).  However in order
to interpret the status I must be able to display the byte in hex
format.  

Ex: byte value 26 = 1A Hex = 0001 0110 binary
      Smiley Face = 0000 0011 binary

Each bit in the byte gives me an off-on value that I can interpret to
indicate various things about the radio's staus (Squelch, Frequency,
etc).

If, using asyncpro, I place the returned data in an edit box I always
end up displaying something like a smiley face or a male symbol etc.
Apparently the  the byte is being interpreted for display purposes
according to the extended ASCII chart.

What I need to do is either convert the data as received from the
comport to binary format and then display the string of 8 zeros or
ones in the text box or take the character in the edit box and convert
it to binary and then display it in another edit box.

Available procedures in Async pro to retrieve data  include:

procedure TForm1.ApdDataPacket1StringPacket(Sender: TObject; Data:
String);

procedure TForm1.ApdDataPacket1Packet(Sender: TObject; Data: Pointer;
  Size: Integer);

I've been through all my Delphi 4 manuals and the help files and
cannot seem to find an obvious way to do this.

Any help would be appreciated!

Dave Alden   d-al...@concentirc.net

 

Re:How to convert Extended ASCII to Binary ?


Quote
Dave Alden wrote:
> I can send the poling code and the radio responds by sending the 1
> byte back (a numeric value from 00-255 or 00h-FFh).  However in order
> to interpret the status I must be able to display the byte in hex
> format.

> Ex: byte value 26 = 1A Hex = 0001 0110 binary
>       Smiley Face = 0000 0011 binary

> Each bit in the byte gives me an off-on value that I can interpret to
> indicate various things about the radio's staus (Squelch, Frequency,
> etc).

> If, using asyncpro, I place the returned data in an edit box I always
> end up displaying something like a smiley face or a male symbol etc.
> Apparently the  the byte is being interpreted for display purposes
> according to the extended ASCII chart.

> What I need to do is either convert the data as received from the
> comport to binary format and then display the string of 8 zeros or
> ones in the text box or take the character in the edit box and convert
> it to binary and then display it in another edit box.

To get a hex or binary string representation of a byte, use:

Hex: IntToHex(YourByte, 2);  (in unit SysUtils)

Bin (untested):

function ByteToBin(AByte: Byte): string;
begin
  for Bit := 0 to 3 do
  begin
    if (AByte and 1) = 0
      then Result := '0' + Result
      else Result := '1' + Result;
    AByte := AByte shr 1;
  end;
  Result := ' '+Result; // insert Space char separating upper nibble
from lower nibble
  for Bit := 4 to 7 do
  begin
    if (AByte and 1) = 0
      then Result := '0' + Result
      else Result := '1' + Result;
    AByte := AByte shr 1;
  end;
end;

Greetings, Robert
--
Robert Ro?mair
http://home.t-online.de/home/Robert.Rossmair/
____________________________________________
Real Programmers never work 9 to 5. If any real programmers
are around at 9 AM, it's because they were up all night.

Re:How to convert Extended ASCII to Binary ?


JRS:  In article <37a3c626.10887...@news.concentric.net> of Fri, 30
Jul 1999 08:38:37 in news:comp.lang.pascal.delphi.misc, Dave Alden

Quote
<d-al...@concentric.net> wrote:
>I can send the poling code and the radio responds by sending the 1
>byte back (a numeric value from 00-255 or 00h-FFh).  However in order
>to interpret the status I must be able to display the byte in hex
>format.  

>Ex: byte value 26 = 1A Hex = 0001 0110 binary
>      Smiley Face = 0000 0011 binary

>Each bit in the byte gives me an off-on value that I can interpret to
>indicate various things about the radio's staus (Squelch, Frequency,
>etc).

Consider the following, from http://www.merlyn.demon.co.uk/pas-opts.htm,
which was written for Pascal but is in large part applicable to Delphi
(integer is 16 bits here, and string is shortstring) :

function IB(J : word) : string ;
var B : byte ;
const OI : array [boolean] of char = '01' ;
S : string [16] = '0123456789abcdef' ;
begin
  for B := 1 to 16 do begin S[B] := OI[integer(J)<0] ; J := J shl 1 end ;
  {or for B := 16 downto 1 do begin S[B] := OI[Odd(J)] ; J := J shr 1 end ; }
  IB := S end ;

To handle the bits of the byte, don't program in old Fortran style; use sets.
After a short initial strangeness, it becomes easy and clear; it should compile
efficiently.

--
 ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
  <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c. FAQqish topics, links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Re:How to convert Extended ASCII to Binary ?


In article <z7HCDQAcauo3E...@merlyn.demon.co.uk>, Dr John Stockton

Quote
<j...@merlyn.demon.co.uk> writes:
>  for B := 1 to 16 do begin S[B] := OI[integer(J)<0] ; J := J shl 1 end ;

If integer is only 16 bits shouldn't that be :-

  for B := 1 to 16 do begin S[B] := OI[smallint(J)<0] ; J := J shl 1 end ;

 . . . J is only a word and "integer" would, in any case, make the result
implementation dependant.

But that's quite a clever trick to make the array index do an effective
"in-line if".

Alan Lloyd
alangll...@aol.com

Re:How to convert Extended ASCII to Binary ?


JRS:  In article <19990801035526.10901.00002...@ngol08.aol.com> of Sun,
1 Aug 1999 07:55:26 in news:comp.lang.pascal.delphi.misc, AlanGLLoyd

Quote
<alangll...@aol.com> wrote:
>In article <z7HCDQAcauo3E...@merlyn.demon.co.uk>, Dr John Stockton
><j...@merlyn.demon.co.uk> writes:

>>  for B := 1 to 16 do begin S[B] := OI[integer(J)<0] ; J := J shl 1 end ;

>If integer is only 16 bits shouldn't that be :-

>  for B := 1 to 16 do begin S[B] := OI[smallint(J)<0] ; J := J shl 1 end ;

> . . . J is only a word and "integer" would, in any case, make the result
>implementation dependant.

Yes, but I wrote "Consider" and "written for Pascal" to cover such.

Quote
>But that's quite a clever trick to make the array index do an effective
>"in-line if".

IMHO, it's always worth watching out for the possibility of using a
lookup table of some sort or other.  Even in cases where full run-time
checks may make it slower, ISTM that it improves the chances of getting
the answer (in non-trivial cases) consistently right - or maybe
consistently wrong, which is often easier to debug than inconsistently!

In BP7,   OI[15 in BitSet(J)]   gives different code of the same length;
but if 15 is replaced by B the code expands a lot.

--
 ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
  Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

Re:How to convert Extended ASCII to Binary ?


Dr John Stockton <j...@merlyn.demon.co.uk> wrote in message
news:z7HCDQAcauo3EwBE@merlyn.demon.co.uk...

Quote
> JRS:  In article <37a3c626.10887...@news.concentric.net> of Fri, 30
> Jul 1999 08:38:37 in news:comp.lang.pascal.delphi.misc, Dave Alden
> <d-al...@concentric.net> wrote:

> >I can send the poling code and the radio responds by sending the 1
> >byte back (a numeric value from 00-255 or 00h-FFh).  However in order
> >to interpret the status I must be able to display the byte in hex
> >format.

> >Ex: byte value 26 = 1A Hex = 0001 0110 binary
> >      Smiley Face = 0000 0011 binary

> >Each bit in the byte gives me an off-on value that I can interpret to
> >indicate various things about the radio's staus (Squelch, Frequency,
> >etc).

> Consider the following, from http://www.merlyn.demon.co.uk/pas-opts.htm,
> which was written for Pascal but is in large part applicable to Delphi
> (integer is 16 bits here, and string is shortstring) :

> function IB(J : word) : string ;
> var B : byte ;
> const OI : array [boolean] of char = '01' ;
> S : string [16] = '0123456789abcdef' ;
> begin
>   for B := 1 to 16 do begin S[B] := OI[integer(J)<0] ; J := J shl 1 end ;
>   {or for B := 16 downto 1 do begin S[B] := OI[Odd(J)] ; J := J shr 1 end
; }
>   IB := S end ;

> To handle the bits of the byte, don't program in old Fortran style; use
sets.
> After a short initial strangeness, it becomes easy and clear; it should
compile
> efficiently.

> --
>  ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00
MIME. ?
>   <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c. FAQqish topics,
links.
>   Timo's TurboPascal <A

HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
Quote
>   <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of

c.l.p.b.

Other Threads