Solved

Binary notation

Posted on 2001-09-12
8
246 Views
Last Modified: 2011-09-20
How do I express value as binary? I thought it was %00000001 (for 1) but it's not!

var i: byte;

i := ....

0
Comment
Question by:pede
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Expert Comment

by:mpoots
ID: 6477359
This function :

function ByteToBin(aByte: Byte): string;
var liCount : integer;
begin
  Result := '';
  for liCount := 0 to 7 do
  begin
    if aByte and 1 <> 0 then
      Result := '1' + Result
    else
      Result := '0' + Result;
    aByte := aByte shr 1;
  end;
end;


Will result '0000001' if you call

showMessage(ByteToBin(1));

Is that what you looked for?

Marcel
0
 
LVL 1

Expert Comment

by:mpoots
ID: 6477362
Or the other way around:

function BinToByte(aString: string): byte;
var liCount : integer; i : double;
begin
  Result      := 0;
  for liCount := 1 to Length(aString) do
  begin
    Result := Result shl 1;
    if aString[liCount] <> '0' then
      Inc(Result);
  end;
end;


Marcel (Holland)
0
 
LVL 6

Accepted Solution

by:
edey earned 25 total points
ID: 6477421
the environment doesn't support refrencing constants in binary directly, though you cant do something like this:

var
   tmp : integer;
begin
     asm
        mov tmp,00000001b
     end;
     caption := intToStr(tmp);


and your caption will read '1'

GL
Mike
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6478766
Hi,
 one more functions...

Function ByteToBit(b : byte) : String;
Var
 z : byte;
Begin
 Result := '';
 For z := 1 to 8 do
 Begin
  IF b AND 128 = 128 then Result := Result + '1'
                     else Result := Result + '0';
  b := b SHL 1;
 End;
End;

Function WordToBit(w : word) : string;
Var
 z : byte;
Begin
 Result := '';
 For z := 1 to 16 do
 Begin
  IF w AND 32768 = 32768 then Result := Result + '1'
                         else Result := Result + '0';
  w := w SHL 1;
 End;
End;

Function DWordToBit(d : dword) : string;
Var
 z : byte;
Begin
 Result := '';
 For z := 1 to 32 do
 Begin
  IF d AND $80000000 = $80000000 then Result := Result + '1'
                                 else Result := Result + '0';
  d := d SHL 1;
 End;
End;

Function BitsToInt(s : string) : Integer;
Var
 z : byte;
Begin
 Result := 0;
 For z := 1 to Length(s) do
 Begin
  IF s[z] = '1' then Result := Result +1;
  IF z <> Length(s) then Result  :=  Result  SHL 1;
 End;
End;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:bullen
ID: 6478887
Not prety but it works

...
function bStr2Int(bs : String):Int64;
begin
  result := 0;
  while length(bs) > 0 do begin
    result := 2*result;
    if bs[1]='1' then result := result + 1;
    bs := copy(bs,2,length(bs));
  end;
end;
...
i := bStr2Int('0000001101');  // 13
...

bullen
0
 
LVL 1

Author Comment

by:pede
ID: 6479500
>the environment doesn't support refrencing constants in binary directly
Though I HATE the answer, this was what I was looking for.
0
 
LVL 6

Expert Comment

by:edey
ID: 6480135
Glad to be of service, though i too wish there were a better solution.  You could go with some sort of string manipulation, as mentioned above, but you take a huge performance hit if you have to do it often :(


GL
Mike
0
 
LVL 1

Author Comment

by:pede
ID: 6480156
I got over it ;o) I could have made the string conversions at startup, but string manipulation was simply too ugly for me, so I just made it in a completely different way.

Thanks for the comments.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 64
Convert a string into a TDateTime 5 52
TEMBEDDEDWB how can i change its user agent ? 8 50
Firemonkey android show image from resource ? 1 33
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

862 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

24 Experts available now in Live!

Get 1:1 Help Now