Forum > General

if string is hex

(1/6) > >>

ahmetnurideniz:
hi,
i need a one function. if StringIsHex.
my deveice send me string data. The data contein sametime decimal sometime hexedecimal. how can i understand is data hex?
for examle:

var a : string;
begin
a := '98967F'
end;

now function result should be is the data is hex.

var a : string;
begin
end;

now function result should be is the data is not hex.

typo:
You could test whether the value is a string or an integer, so you distinguish between number and hex. Or you could test whether the string contains only values among [0123456789ABCDEF] or not, so you distinguish between strings and hexadecimal strings, although this may fail for a password. But you can test the password to know if it is a valid one.

ahmetnurideniz:
thanks i write this function it is working.

--- Code: ---function TFrmMain.IsStrHex(temizhex: string): Boolean;
var
i,j,test : integer;

const Digits:array[0..16] of char='0123456789ABCDEF ';
begin
// Girilen Hexe decimal değeri ters çevirir     Ahmet Nuri DENİZ 29.08.2010
test:=0;
for i := 1 to Length(temizhex) do
begin
for j := 0 to 16 do
begin
if temizhex[i] = Digits[j] then test := test + 1;
end;
end;
if test = Length(temizhex)  then Result := True
else
Result := False;
end;

--- End code ---

fabienwang:
a simpler version :)

--- Code: ---function TFrmMain.IsHex(s: string): boolean;
var
i: integer;
begin
Result := True;
for i := 1 to length(s) do
if not (char(s[i]) in ['0'..'9']) and not (char(s[i]) in ['A'..'F']) then
begin
Result := False;
exit;
end;
end;
--- End code ---

typo:
But it can still be confused with a password or even an integer. Maybe you need more information about the data from the device.

This function should be called MaybeHex instead of IsHex.