×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Binary -> HexBinary with VFP7

## Binary -> HexBinary with VFP7

(OP)
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?

Thanks,
JRB-Bldr

### RE: Binary -> HexBinary with VFP7

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.

### RE: Binary -> HexBinary with VFP7

#### Quote:

Is Hexedit a part of VFP7, too?

Yes, it is. The source is all there in Tools / HexEdit.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy

### RE: Binary -> HexBinary with VFP7

Are you sure you want that and not Base64 en/decoding?

Bye, Olaf.

### RE: Binary -> HexBinary with VFP7

(OP)
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

### RE: Binary -> HexBinary with VFP7

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.

### RE: Binary -> HexBinary with VFP7

(OP)
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

### RE: Binary -> HexBinary with VFP7

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.

### RE: Binary -> HexBinary with VFP7

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.

### RE: Binary -> HexBinary with VFP7

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.

### RE: Binary -> HexBinary with VFP7

(OP)
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

### RE: Binary -> HexBinary with VFP7

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.

### RE: Binary -> HexBinary with VFP7

(OP)
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

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!