Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed converting ftBytes to string

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
0
0
GB
hi

I'm reading a memo field from Sybase and would like to store it in a varchar field in SQLServer. I know that the field contains a hexadecimal number but they're stored as binary on the Sybase end. When I read this field in the field.datatype is ftBytes. How can I convert this hexadecimal number (stored as bytes) in to a string?

I have this so far (found on the web) but it is just returned #0 for each converted byte:

Code:
:
    else if Field.DataType in [ftBytes, ftVarBytes] then
    begin
      result := VariantToString(field.value);
    end
    else
:

function TFormLoader.VariantToString(const vArray: variant): string;
  var Cnt: integer;

    function VarToString(const V: variant): string;
      var Vt: integer;
    begin
      Vt := VarType(V);
      case Vt of
        {02}varSmallint,
        {03}varInteger : result := Inttostr(integer(V));
        {04}varSingle,
        {05}varDouble,
        {06}varCurrency : result := Floattostr(Double(V));
        {07}varDate : ;//result := usgFormatDateTime(TDateTime(V));
        {08}varOleStr : result := WideString(V);
        {0B}varBoolean : ;//result := TF[Boolean(V)];
        {0C}varVariant : result := VariantToString(Variant(V));
        {11}varByte : result := char(byte(V));       //<---- This is the one which gets used
        {100}varString : result := String(V);
        {2000}varArray : result := VariantToString(Variant(V));
      end; {case }
    end;

begin
  result := '';
  if (vartype(vArray) and VarArray)=0
    then result := VarToString(vArray)
    else  for Cnt := VarArrayLowBound(vArray, 1) to
             VarArrayHighBound(vArray, 1) do
                result := result+VarToString(vArray[Cnt]);
end;

Grateful for any help on this one!

Cheers, guys
Lou
 
Try something like this.

Haven't tested it though...

Code:
function TForm1.ByteToHex(InByte:Byte): String;
const 
   Digits:array[0..15] of char='0123456789ABCDEF';
begin
   Result := Digits[InByte shr 4]+digits[InByte and $0F];
end;

function TForm1.HexToString(H: String): String;
var 
   I : Integer;
begin
  Result := '';
  for I := 1 to length (H) div 2 do
     Result:= Result + Char(StrToInt('$'+Copy(H,(I-1)*2+1,2)));
end;



var
   MyHexString:  String;
begin
   MyHexString := HexToString(ByToHex(<DBfieldName>.AsByte));
   ...
end;
 
hi majlumbo

I'm at home now but I will try it first thing in the morning. Many thanks for your help.

Lou
 
I would have thought that using the Format function with a format specifier of %x would be simpler.

For example
Code:
MyHexString := Format( '%x', [ <DBfieldName>.AsByte) ] );
The format specifier can include a width so that if you wanted an 8 character hexadecimal number padded with leading zeros you would code
Code:
MyHexString := Format( '%.8x', [ <DBfieldName>.AsByte) ] );

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top