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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary -> HexBinary with VFP7

Status
Not open for further replies.

JRB-Bldr

Programmer
May 17, 2001
3,281
US
I did a search in the forum for a means to convert Binary to HexBinary.

And I found that it would be easy to do with STRCONV(<whatever>,15), but that function conversion option is not available to VFP7.

So has anyone have an alternative that can work in VFP7?

Your advice and suggestions would be most appreciated.

Thanks,
JRB-Bldr
 
As an example Strconv("@ABC",15) does return "40414243" os 0x40 = 64 = ASC("@") and 0x41 = 65 = ASC("A") etc,etc.

I don't know if you really want this, but if, then maybe look into Home()+"Tools". Is Hexedit a part of VFP7, too?

In the end it uses RIGHT(TRANSFORM(zzz, "@0"),2) to create the hex representation for each byte, eg Right(TRANSFORM(Asc("@"), "@0"),2) returns "40".

In fact HexEdit builds an array of 256 elements via that Transform() expression, so the array elements are filled from "00" to "FF" and uses that for faster conversion.

Then it's just a lookup in that array via aHex(ASC(char)).

Bye, Olaf.



 
In Craig Boyd's Blog about MD5 Hash encryption he says:
The hash is returned as a series of binary characters. However, it is more common to see hashes in a hexBinary format. This can be accomplished in Visual FoxPro by taking the return of the Hash() function and sending it in as a parameter to the STRCONV() function. For example:

?STRCONV(Hash("Some String"), 15) && hexBinary Hash


I have confirmed with my recipient of the Authentication String that we need to supply them with HexBinary instead of the Binary.

And since the STRCONV() conversion option that Craig suggests above is not applicable in VFP7, I am looking for an alternative.

I have VFP9 here so I will be able to compare the results of using the STRCONV() method and whatever VFP7 method we come up with. Unfortunately I cannot rely on using the VFP9 when I execute the utility.

Do you think that the HexEdit method will produce the same results?

Thanks,
JRB-Bldr
 
Yes, then you actually want the conversion of 1 binary byte to two hexdigits as done by Hexedit, as in

For lnPosition = 1 To Len(lcYourString)
? RIGHT(TRANSFORM(Substr(lcYourstring,n,1), "@0"),2)
Endfor

You could also store the binary value in a Q() field, though. Browsing that also displays the binary content hexadecimal.

Bye, Olaf.
 
I just tried to compare the results of the 2 conversion methods.

In VFP7:
cTest = '25'
?TRANSFORM(cTest,'@0')
result = 25 (numeric)

cTest = '25'
?RIGHT(TRANSFORM(Substr(cTest,1,1), "@0"),2) + RIGHT(TRANSFORM(Substr(cTest,2,1), "@0"),2)
result = 25 (numeric)

nTest = 25
?TRANSFORM(SUBSTR(ALLTRIM(STR(nTest)),1,1),'@0') + TRANSFORM(SUBSTR(ALLTRIM(STR(nTest)),2,1),'@0')
result = 25 (numeric)

nTest = 25
?TRANSFORM(nTest,'@0')
result = 0x0000019 (numeric)

In VFP9:
cTest = '25'
?STRCONV(cTest,15)
result = 3235 (numeric)

nTest = 25
?STRCONV(nTest,15)
Invalid Operation

So from this test, the 2 methods do not appear to be interchangeable.

Is my testing methodology in error?
Or is there a non-equivalent difference between the 2 methods?

Thanks,
JRB-Bldr
 
Fist of all the input must be string, strconv() is a string function.

The result of Transform is NOT numeric, neither in VFP9 nor in VFP7, that can't be true, you didn't test the output. Also the result of Strconv() is always a string, not numeric. The string may contain numbers, yes, but that's simply because the digits '0' to '9' are among hex digits.

I think you're judging the type from what you see on screen. For example, the output of Transform is 0x...., and that's a string, even though it evaluates to a number.

Simply see the help reference on Strconv() and Transform() both functions only return strings. Transform takes anything as input, Strconv() only takes string as input, that's the outset without any tests.

Just the VFP9 part:
Code:
 cTest = '25'
 result = STRCONV(cTest,15)
 ? result, Vartype(result)

It looks numeric, but it's the string '3235' 0x32 is 50 and chr(50) is '2', 0x35 is 53 and chr(53) is '5', that's what you would expect. Choose other input strings and you'll see. Better choose chr(26), not '25', as input, you'll see it converts to '1A'.

Bye, Olaf.
 
Another sample to compare Strconv() with Transform(..'@O'):

Code:
lcInput = chr(0x9a)+chr(0xbc)+chr(0xde)+chr(0xf0)
? 'Input: ',lcInput
? 'length:',Len(lcInput)

lcResult1 = strconv(lcInput,15)
?
? 'Result: ',lcResult1
? 'Vartype of result:', Vartype(lcResult1)
? 'length:', Len(lcResult1)

lcResult2=''
For lnPosition = 1 To Len(lcInput)
  lcResult2 = lcResult2 + Right(TRANSFORM(Asc(Substr(lcInput,lnPosition,1)), "@0"),2)
EndFor

?
? 'Result: ',lcResult2
? 'Vartype of result:', Vartype(lcResult2)
? 'length:', Len(lcResult2)

It's the same output. I forgot the ASC() within the transform() expression. But I gave it initially as example with the input of the string '@': Right(TRANSFORM(Asc("@"), "@0"),2).

Bye, Olaf.
 
binary field:

Code:
lcInput = chr(0x9a)+chr(0xbc)+chr(0xde)+chr(0xf0)
create cursor curHex(qResult Q(4))
insert into curHex values (lcInput)
browse

Much simpler, isn't it?

Bye, Olaf.
 
Olaf - when I said that the result was (numeric) it was due to
?TYPE(STRCONV(cTest,15))
resulting in a "N"

Interesting.....
When I do the following.
cTest = '25'
?TYPE(TRANSFORM(cTest,'@0'))
I get "N"

But if I do
cTest = '25'
result = TRANSFORM(cTest,'@0')
?TYPE('Result')
I get "C"

Strange and leading me to mis-state the type of the results.

create cursor curHex(qResult Q(4))
I definitely like the simplicity of it, but it does not appear to work in VFP7 - no varbinary field type.

Thanks,
JRB-Bldr







 
Type is the wrong functions to check the type :). Vartype() is the correct one, see the help on the difference!

? Type("1")
? Type("_tally")
? Type("_screen")

Would you argue all values tested are strings?

Type(expression) is about the same as Vartype(eval(expression)).

Bye, Olaf.
 
Olaf - Thanks for setting me straight.

The TRANSFORM() method appears to work equally well for both VFP9 & VFP7. Your last test code confirms the result no matter if run under VFP9 or VFP7.

Code:
CLEAR

lcInput = CHR(0x9a)+CHR(0xbc)+CHR(0xde)+CHR(0xf0)
? 'Input: ',lcInput
? 'length:',LEN(lcInput)
?

IF VAL(LEFT(VERSION(4),2)) = 9
   lcResult1 = STRCONV(lcInput,15)

   ? 'VFP9 STRCONV() Results...'
   ? 'Result: ',lcResult1
   ? 'Vartype of result:', VARTYPE(lcResult1)
   ? 'length:', LEN(lcResult1)
   ?
ENDIF

lcResult2=''
FOR lnPosition = 1 TO LEN(lcInput)
   lcResult2 = lcResult2 + RIGHT(TRANSFORM(ASC(SUBSTR(lcInput,lnPosition,1)), "@0"),2)
ENDFOR

IF VAL(LEFT(VERSION(4),2)) = 9
   ? 'VFP9 TRANSFORM() Results...'
ELSE
   ? 'VFP7 TRANSFORM() Results...'
ENDIF

? 'Result: ',lcResult2
? 'Vartype of result:', VARTYPE(lcResult2)
? 'length:', LEN(lcResult2)

Since the TRANSFORM() approach works for both VFP versions, I will go with that one.

My last 'test' will be to send the Authorization Hash key to my recipient and see if it is compatible with what they are looking for.

Thanks,
JRB-Bldr





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top