[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

need some good Hex to string / String to Hex functions

Posted on 2010-09-15
5
Medium Priority
?
341 Views
Last Modified: 2012-05-10
I have a form with two memos and two butons (hex to text & text to hex)

I need two functions which can take hex and convert to text and visa versa

bonus is you add exceptions (error code checking)

thanks
0
Comment
Question by:Looking_4_Answers
[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
  • 3
  • 2
5 Comments
 

Author Comment

by:Looking_4_Answers
ID: 33682797
example, would be:

http://www.dolcevie.com/js/converter.html


without the colons  (maybe spaces instead)
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33682945
Here is already the one that I talked you about. It can manage a lot of formatting options. You might want to simplify this : drop the C definition and Indentation functionalities - they are useful with C code generation.

It is also based on a pointer on Char and a number of characters. This is more versatile as it can be used to convert about anything, but you might prefer a simple String parameter for input.

Other parameters : Col & Hewawidth controls the grouping of hexa bytes in columns and lines.
HexaWidth will be the Nb of bytes that are rendered together without spaces between. Ex : 4 will show display the hexa values for 32 bits.
Col is the number of bytes before a line ret is added. Col must be a multiple of HexaWidth, except if Col =0 (no line ret)

Ex : Col = 6, HexaWidth =2 will return something like for a buffer of 9 bytes:
A0B6 F43D 76A3
98D6 3F

The same with Col=0 and HexaWidth=1 (the simple one byte separated hexa formatting)
A0 B6 F4 3D 76 A3 98 D6 3F

Function BufToHexStr(N:Cardinal;Buf:pCharTab;Col:Byte=0;HexaWidth:Byte=0;CDef:Boolean=False;Indent:Byte=0):String;
Var
 i:Integer;
 Hdr,Ftr,NewL,IdtStr:String;
begin
 if Buf=nil Then Exit;
 IdtStr:=DupeString(' ',Indent);
 NewL:=CRLF+IdtStr;
 if CDef Then
  begin
   Result:='{ ';
   Hdr:='0x';
   Ftr:=' ,';
  end Else
  begin
   Result:='';
   Ftr:=' ';
   Hdr:='';
  end;
 if HexaWidth>0 Then while (Col MOD HexaWidth)>0 do Dec(HexaWidth);
 For i:=0 to N-1 do
  begin
   if (i>0) And (HexaWidth>0) And ((i Mod HexaWidth)=0) Then Result:=Result+Ftr;
   if (Col>0) And (i>0) And ((i Mod Col)=0) Then
    begin
     Result:=Result+NewL;
     if CDef Then Result:=Result+'  ';
    end;
   Result:=Result+Hdr+HexaChar(Buf^[i]);
  end;
 if CDef Then Result:=Result+NewL+'}';
end;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33683200
And this is the reverse, it can ignore any characters that is not hexa digit and consider those as separators
Note that the hexa digits don't have to be paired if separated, for ex.
9B 06 3F = 9B 6 3F = 9B06,3F

I added the same exception mechanism for you as in the Bin convertion, as I'm sure your next question is how try this one conversion and if it fails call the String To Hexa ;o)

I also simplified BufToHexStr and removed any reference to other functions I have
Function HexaStrToBuf(Src:ANSIString;ThrowException:Boolean=False):ANSIString;
begin
 For i:=1 to Length(Src) do
  if Not (Src[i] IN ['0'..'9','A'..'F']) Then 
   begin
    if ThrowException And Not (Src[i] In [' ',#13,#10]) Then Raise Exception.Create('Invalid Hexa character');
    Text[i]:=' ';
   end;
 P:=1;
 Result:='';
 While P<=Length(Src) do if Src[P]=' ' Then Inc(P) Else
  begin
   Result:=Result+Char(StrToInt('$'+Trim(Copy(Src,P,2))));
   Inc(P,2);
  end;
end;

Function BufToHexStr(Src:ANSIString;Col:Byte=0;HexaWidth:Byte=0):ANSIString;
Var
 i:Integer;
begin
 Result:='';
 if HexaWidth>0 Then while (Col MOD HexaWidth)>0 do Dec(HexaWidth);
 For i:=0 to Length(Src)-1 do
  begin
   if (i>0) And (HexaWidth>0) And ((i Mod HexaWidth)=0) Then Result:=Result+' ';
   if (Col>0) And (i>0) And ((i Mod Col)=0) 
    Then Result:=Result+#13#10;
   Result:=Result+Format('%2.2x',[Byte(Src[i+1])]);
  end;
end;

Open in new window

0
 

Author Comment

by:Looking_4_Answers
ID: 33685012
kool, i will test it - thanks
0
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 33763333
are you done testing ? do you need something else ?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

656 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