Recent

Author Topic: HexStr to byte array  (Read 24939 times)

danielw

  • Newbie
  • Posts: 3
HexStr to byte array
« on: September 21, 2011, 03:30:49 pm »
Sorry for my beginner question, but I want to get a byte array from an hex string.

String like

var
  s: String;
  ba: array[0..4] of Byte;
begin
  s:='05ff37ab';
  //Now i like to get ba calculated from s


danielw

  • Newbie
  • Posts: 3
Re: HexStr to byte array
« Reply #1 on: September 21, 2011, 03:53:27 pm »
ok but how will this help to parse the string into the byte array?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: HexStr to byte array
« Reply #2 on: September 21, 2011, 03:59:11 pm »
You seem to have 4 hex values, so you only need to define 4 bytes like:
Code: [Select]
ba: array[0..3] of Byte;
From there http://delphi.about.com/od/mathematics/a/baseconvert.htm you can find:
Code: [Select]
function HexToInt(HexNum: string): LongInt;
begin
   Result:=StrToInt('$' + HexNum) ;
end;

Which you can convert to integer and somehow bit-slice bytes of it. Simpler would be to use Copy() function to slice the string instead into 4 pieces.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: HexStr to byte array
« Reply #3 on: September 21, 2011, 04:02:22 pm »
Try this (not tested):

Code: [Select]
uses ............., strutils;
....
var
  i, l: integer;
  str: String;
  ba: array [0..3] of Byte;
begin
  str:='05ff37ab';
  l:=length(str) div 2;
  for i:=0 to l-1 do
    ba[i]:=Hex2Dec(MidStr(str, i*2, 2));       

Of course, you can convert your string with Hex2Dec at once to LongWord.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

garlar27

  • Hero Member
  • *****
  • Posts: 652

danielw

  • Newbie
  • Posts: 3
Re: HexStr to byte array
« Reply #5 on: September 21, 2011, 05:06:51 pm »
Thanks, it works.

But now I have the problem to do it the otherway round... I Have my Byte Array and try to convert it into a String (That is hex coded).

I found the function BinToHex, but it seams not to work with a array of byte or a byte only with PChar.

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: HexStr to byte array
« Reply #6 on: September 21, 2011, 05:22:36 pm »
How about:
Code: [Select]
type
THex = packed record
   case integer of
      0: (hexlong : longword);
      1: (hexbyte : packed array[0..3] of byte);
end;

var
str: string;
hexrec : THex;
   :    :
str := '05ff37ab';
hexrec.hexlong := Hex2Dec(str);   

if hexrec.hexbyte[0] = .........
Be aware that - at least on Windows - the sequence of bytes now is reversed hexbyte[0] contains the 'ab' and hexbyte[3] the '05';

The other way around if hexrec is filled with byte values .....
Code: [Select]
str := format('%.8x',[hexrec.hexlong]);
« Last Edit: September 21, 2011, 05:43:05 pm by Arbee »
1.0/2.6.0  XP SP3 & OS X 10.6.8

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: HexStr to byte array
« Reply #7 on: September 21, 2011, 05:43:59 pm »
Humm, i'll try

Code: [Select]
var str: String;
  ba: array [0..3] of Byte;
begin
  ...
  setlength(str,8);
  BinToHex(PChar(@ba), PChar(@str[1]), 4);

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: HexStr to byte array
« Reply #8 on: September 21, 2011, 06:38:50 pm »
absolute helps here:
Code: [Select]
type
  T4Byte = array [0 .. 3] of Byte;
var
  ba: T4Byte;
  shadow: LongWord absolute ba;
begin
  shadow := StrToInt('$' + YourHEXString);
  // ba now contains the bytes of YourHEXString, watch out with endianness
end;
For the other way around, just use HexStr on shadow.

 

TinyPortal © 2005-2018