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

Convert HexToByte

Status
Not open for further replies.

iwahyudi

Programmer
Nov 26, 2013
7
ID
Hello,

Please help me to find out how to create function convert from hex to byte

Thank you
 
what do you mean with hex (could be a string with hexadecimal digits 0-9a-f or a binary field), and what do you mean with byte? Integer value in the range or 0-255?

If you have two hex digits, you convert them to a decimal number with 0x, eg 0xFF is the same as 255. As a byte you could simply use CHR(0xFF).

But what are we talking about? A string, a Q type table field? And what is your goal, a number or a character?

Bye, Olaf.
 
Other samples of when you need this is, you have binary fields in mysql, coming over as binary fields Q(N) in VFP and you just need to change you MYSQL query with a collate clause to return an ANSI string field instead of binary data.

So depending on the larger problems solutions can differ.

Also notice 0x is no command nor function, you can only prefix FF in source code with it, not a string variable containing FF "0xFF" will stay 0xFF, but EVAL("0xFF") of course is possible.

Another prefixes is 0h for binary fields, eg 0hff stays 0hff, but ''+0hFF returns the same char as CHR(0xFF).

Also see CREATEBINARY(), BINTOC() and CTOBIN() functions.

Bye, Olaf.

 
@Olaf for clear information I read function in VB like below:

****************************************************************
Private Function HexToByte(ByVal HexText As String) As Byte()
Dim Looper As Long
Dim Looper2 As Long
Dim Nibble As Byte
Dim Bytes() As Byte

HexText = UCase(HexText)

For Looper = 1 To Len(HexText) Step 2
ReDim Preserve Bytes(0 To (Looper \ 2)) As Byte

For Looper2 = 0 To 1
Nibble = Asc(Mid(HexText, Looper + Looper2, 1))
If Nibble >= 65 And Nibble <= 70 Then
Nibble = Nibble - 55
ElseIf Nibble >= 48 And Nibble <= 57 Then
Nibble = Nibble - 48
End If
Bytes(Looper \ 2) = Bytes(Looper \ 2) + Nibble * (16 ^ (1 - Looper2))
Next
Next

HexToByte = Bytes
End Function
****************************************************************

How to translate it to vfp
 
Probably the scruffiest bit of code on the planet - but, it works:

Code:
STRHEX = "1D2A"
DIMENSION ARYBYTES(INT(LEN(STRHEX)/2))
HEXTOBYTE(STRHEX,@ARYBYTES)
FOR I = 1 TO ALEN(ARYBYTES)
	? ARYBYTES[i]
NEXT



FUNCTION HEXTOBYTE
	PARAMETER HEXTEXT,BYTES
	PRIVATE HEXTEXT
	PRIVATE X,Y
	PRIVATE NIBBLE

	FOR X = 1 TO ALEN(BYTES)
		BYTES[x] = 0
	NEXT

	HEXTEXT = UPPER(HEXTEXT)

	FOR X = 1 TO LEN(HEXTEXT) STEP 2 && kinda assumes HexText is an even number of characters

		FOR Y = 0 TO 1
			NIBBLE = ASC(SUBSTR(HEXTEXT, X + Y, 1))
			IF NIBBLE >= 65 .AND. NIBBLE <= 70
				NIBBLE = NIBBLE - 55
			ELSE
				IF NIBBLE >= 48 .AND. NIBBLE <= 57
					NIBBLE = NIBBLE - 48
				ENDIF
			ENDIF

			BYTES[INT(x / 2)+1] = BYTES[INT(x / 2)+1] + NIBBLE * (16 ^ (1 - Y))
		NEXT
	NEXT

	RETURN(.T.)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
What VB generates is a byte array, not a (single) byte.

VFP does not have this. The closest you get to this is doing:

lqBytes = Evaluate("0h"+HexString)

You can forward lqBytes to COM functions needing a byte array, it's not guaranteed to work, but eg works with XMLHttpRequest.Send(), but a normal string (not a hex string though) works there, too.

So simply try that.

By the way, just before you complain: If you ? Evaluate("0h"+HexString) you see nothing but 0hFF..., it looks like you get back a string, but the 0h prefix makes it a binary string, the closest similar to a VB byte array VFP knows.

Simply check VARTYPE(Evaluate("0h"+HexString)), it's Q, not C.
Still I can't guarantee you get what you need for calling some VB COM Server Method or anything else needing a byte array. What you can do obviously without any doubt is make this VB funcion an olepublic function of a OLE DLL and use it with VFP by passing in a hexstring. Then you end up with a VB Byte Array in VB and can go on from there.

Bye, Olaf.
 
@GriffMG and @OlafDoschke: thank you for your responded and supported, I will try it

Thank you,
 
Just one more hint:

If you initiall create a hex string from a normal VFP string, eg a text or a string you get from FILETOSTR(), you don't need to first convert this to a hexstring, you can simply use CREATEBINARY(lcString) to get to the Q type directly, where lcString can be anything, eg "a##+*?b" or what you get reading in a BMP, JPG via FILETOSTR(), anything. In VFP this is already sufficient to work on binary data as "normal" strings, but in COM Servers you need to pass in Q types instead, sometimes. You might also try the original data itself, without going through Hex and without going through CREATEBINARY at all.

Bye, Olaf.
 
I would write the HEXTOBYTE function something like this:

Code:
[b][COLOR=blue]FUNCTION HEXTOBYTE

PARAMETER hextext, bytes
PRIVATE x, nibble1, nibble2

hextext = UPPER(hextext)
	
x = 0

DO WHILE NOT EMPTY(hextext)
	nibble1 = ASC(hextext) - 48
	nibble1 = nibble1 - IIF(nibble1 > 9, 7, 0)
	nibble2 = ASC(SUBSTR(hextext, 2)) - 48
	nibble2 = nibble2 - IIF(nibble2 > 9, 7, 0)
	x = x + 1
	bytes(x) = 16 * nibble1 + nibble2	&& High byte, Low byte
	hextext = SUBSTR(hextext, 3)
ENDDO

RETURN[/color][/b]

This assumes that hextext is valid hex.
Also assumes hextext is even character count.
If not, this will fail.

Also assumes that high hex byte is first in every pair.
If low byte is first, then change multiplier.

And also assumes I understand what you are trying to accomplish.

Not tested.



mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top