Recent

Author Topic: if string is hex  (Read 37728 times)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4724
  • I like bugs.
Re: if string is hex
« Reply #15 on: September 05, 2010, 09:03:51 am »
i tesed it. i can't see any problem.
...
result is false.

Yes but your code always returned false. If you have fixed it, good.

'and not (char(s) = '$')' does not look good.
Do you mean 'and (s[1] <> '$')' ?

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: if string is hex
« Reply #16 on: September 06, 2010, 04:57:14 pm »

Yes but your code always returned false. If you have fixed it, good.

'and not (char(s) = '$')' does not look good.
Do you mean 'and (s[1] <> '$')' ?

Juha


this is my working function
Code: [Select]
function TFrmMain.IsStrHex(s: string): Boolean;
var
  i: integer;
  s2 : string;
begin

  Result := True;
  s2 := Copy(s,1,1);
  if s2 <> '$' then begin
       Result := False;
  end
  else
  begin
  for i := 1 to length(s) do
    if not (char(s[i]) in ['0'..'9']) and not (char(s[i]) in ['A'..'F']) and not (char(s[i]) = '$') then
    begin
      Result := False;
      exit;
    end;
  end;
end;                   

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: if string is hex
« Reply #17 on: September 06, 2010, 06:29:26 pm »
I wonder if:
- $$$$$
- $$345$$3344$
- $af
are hex strings...
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: if string is hex
« Reply #18 on: September 07, 2010, 06:27:29 am »
I wonder if:
- $$$$$
- $$345$$3344$
- $af
are hex strings...


i think it is ok now :)
Code: [Select]
function TForm1.IsStrHex(s: string): Boolean;
var
  i,k: integer;
  s2,s3 : string;
begin
  Result := True;
  k:= 1;
  while k <= length(s)  do begin
    s3 := Copy(s,k,3);
    s2 := Copy(s3,1,1);
    if s2 <> '$' then begin
      Result := False;
      Break;
    end
    else
    begin
    for i := 1 to length(s3) do
      if not (char(s3[i]) in ['0'..'9']) and not (char(s3[i]) in ['A'..'F']) and not (char(s3[i]) = '$') then
      begin
        Result := False;
        exit;
      end;
    end;
  k := k+3;
  end;
end;               

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4724
  • I like bugs.
Re: if string is hex
« Reply #19 on: September 07, 2010, 09:11:17 am »
Quote
i think it is ok now  :)

Not yet. It is getting worse actually.  :)

Where does the number "3" come from? The input string can be any length.

As this is a good exercise function I give you a hint:
Only the first char can be "$". Test it outside the loop. Then test the rest of the string in a loop.

You could also support the other notations "0x123ABC" and "123ABCH" as an exercise. Depends on your program if you really need them.

Another hint for performance: Copy() function is expensive as it requires a memory allocation. You can test a single char without copying like "if s[1] = '$' then...

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: if string is hex
« Reply #20 on: September 07, 2010, 01:51:45 pm »
Quote
i think it is ok now  :)

Not yet. It is getting worse actually.  :)

Where does the number "3" come from? The input string can be any length.

As this is a good exercise function I give you a hint:
Only the first char can be "$". Test it outside the loop. Then test the rest of the string in a loop.

You could also support the other notations "0x123ABC" and "123ABCH" as an exercise. Depends on your program if you really need them.

Another hint for performance: Copy() function is expensive as it requires a memory allocation. You can test a single char without copying like "if s[1] = '$' then...

Juha


i have only one type hex number like '$12' or '$12$ab$ab'
i don't know what does  "0x123ABC" mean.
i will work for "123ABCH" type.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4724
  • I like bugs.
Re: if string is hex
« Reply #21 on: September 07, 2010, 10:30:07 pm »
i have only one type hex number like '$12' or '$12$ab$ab'
Then you are creating a new notation system. That is ok if you just want to experiment.
... but your code was still not correct.

Quote
i don't know what does  "0x123ABC" mean.
It is the notation for hex numbers used in C and other C-like languages.

Quote
i will work for "123ABCH" type.
This is used in some assembly languages. Not as common as the other notations.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: if string is hex
« Reply #22 on: September 07, 2010, 11:41:12 pm »

this is new function
Code: [Select]

function TForm1.IsStrHex(s: string): Boolean;
var
  i,k,j: integer;
  s2,s3 : string;
begin
  Result := True;
  k:= 1;
  j := 0;
  while k <= length(s)  do begin
    s3 := s[k];
    if s3 = '$' then begin
      while s3 = '$' do begin
        k := k+1 ;
        s3 := s[k];
        j := j+1;
        if j > 1 then begin
          Result := False;  // this line for '$$'
          Break;
        end;
      end
    end
    else
    begin

    end;
    for i := 1 to length(s3) do
      if not (char(s3[i]) in ['0'..'9']) and not (char(s3[i]) in ['A'..'F']) then
      begin
        Result := False;
        exit;
      end;
      k := k+1;
  end;
end;
           

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4724
  • I like bugs.
Re: if string is hex
« Reply #23 on: September 08, 2010, 08:38:47 am »
It still doesn't work.
It says 'AB$C' is a valid hex number while it is not.
'ABC$' gives a range check error!

The code is overly complicated. There are 3 loops while 1 would be enough. There are redundant tests for the same condition (if s3 = '$' , while s3 = '$'). Etc...
BTW, "s[k]" returns a char and thus "s3 := s[k]" is not a clever thing.

Please rethink what you want to do. Don't just add more loops to fix the function.
If you want to support the standard notation then test for the prefix '$' first and then check the other characters in a loop.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: if string is hex
« Reply #24 on: September 09, 2010, 02:43:55 am »
It still doesn't work.
It says 'AB$C' is a valid hex number while it is not.
'ABC$' gives a range check error!

The code is overly complicated. There are 3 loops while 1 would be enough. There are redundant tests for the same condition (if s3 = '$' , while s3 = '$'). Etc...
BTW, "s[k]" returns a char and thus "s3 := s[k]" is not a clever thing.

Please rethink what you want to do. Don't just add more loops to fix the function.
If you want to support the standard notation then test for the prefix '$' first and then check the other characters in a loop.

Juha

i can solve 'AB$C' but i can't find any way 1 loops.
i will think how can i do that.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: if string is hex
« Reply #25 on: September 09, 2010, 03:28:14 am »
Code: [Select]
function IsHex(s: string): boolean;
var
  i: integer;
begin
  Result := False;
  if s[1] <> '$' then Exit;
  Result := True;
  for i := 2 to Length(s) do
    if not (s[i] in ['0'..'9']) and not (s[i] in ['A'..'F']) then
      Result := False;
end;
     

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: if string is hex
« Reply #26 on: September 09, 2010, 06:52:57 am »
Code: [Select]
function IsHex(s: string): boolean;
var
  i: integer;
begin
  Result := False;
  if (length(s) < 2) or (s[1] <> '$') then Exit;
  for i := 2 to Length(s) do
    if not (s[i] in ['0'..'9','A'..'F','a'..'f']) then Exit;
  Result := true
end;
     
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4724
  • I like bugs.
Re: if string is hex
« Reply #27 on: September 09, 2010, 09:00:29 am »
>   if s[1] <> '$' then Exit;

Typo's version chokes on empty strings.  ;D

Wikipedia has lots of info about maths and algorithms. I was reading about binary search which is a well defined task but it has more tricky corner cases than this IsHex function. See TStringList.Find for implementation. It is short but still difficult to make right.
I copy a text from http://en.wikipedia.org/wiki/Binary_search. Funny ...

---
When Jon Bentley assigned it as a problem in a course for professional programmers, he found that an astounding ninety percent failed to code a binary search correctly after several hours of working on it, and another study shows that accurate code for it is only found in five out of twenty textbooks (Kruse, 1999). Furthermore, Bentley's own implementation of binary search, published in his 1986 book Programming Pearls, contains an error that remained undetected for over twenty years.
---

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: if string is hex
« Reply #28 on: September 09, 2010, 08:36:44 pm »
Quote
Typo's version chokes on empty strings.

All depends on what you expect to get from the device.

Code: [Select]
{$B-}
if (Length(s) < 2) or (s[1] <> '$') then Exit;

Eny has provided the most elegant solution. But I would not include 'a'..'f'.
« Last Edit: September 09, 2010, 09:10:07 pm by typo »

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: if string is hex
« Reply #29 on: September 10, 2010, 10:34:07 am »
All depends on what you expect to get from the device.
Exactly!
Like, is an empty string a valid hex string? Is '$'? etc.
TS has to decide what works best.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

 

TinyPortal © 2005-2018