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

HEX Values

Status
Not open for further replies.

netwalker1

Programmer
Feb 5, 2000
1,241
0
0
EG
Dear All :
I need to create a file contains special structure ...
this file need in each row , combination from 2 or 3 values with Ascii , and 2 or 3 values with HEX ...and also some values with Binary ..

so that it's like this :

name : Mohamed --> ascii
card number : A005DF --> HEX
special Value : 1010101111 --> Binary


how can I create something like this ?



Mohamed Farid
[green]Know Me No Pain , No Me Know Pain !!![/green]
CCNP,CCA,MCSE,MCSA
 
In the case of the latter two items, why store them in a number base at all? Just storing the value as a couple of bytes would be a lot more efficient - and convert to binary / hex in your program when you need to...

mmilan
 
this is my problem :
How can I do that !


Mohamed Farid
[green]Know Me No Pain , No Me Know Pain !!![/green]
CCNP,CCA,MCSE,MCSA
 
Mohamed,
You'll probably need to loop through each character, using the VB Hex() function, and then another internal loop to check if each bit is on or off. Haven't tested it, but something like:

for i=1 to len(strInput)
strHexOutput=strHexOutput & Hex(mid(strInput,i,1))
for j=7 to 0 step -1
strBinaryOutput=strBinaryOutput & CStr(asc(mid(strInput,i,1)) and 2^j)
next
next

You'll may need to trim spaces, pad with 0's, etc...

Hope this helps.
 
To convert a string containing 0s and 1s only use something like (note you'll have to put error checking for 1s and 0s only and keep length below 31):

Public Function fnBin2Long(Bin As String) As Long
Dim lngCount As Long
Dim strTemp As String
For lngCount = Len(Bin) To 1 Step -1
strTemp = Mid(Bin, Len(Bin) - lngCount + 1, 1)
fnBin2Long = fnBin2Long + (Val(strTemp)) * (2 ^ (lngCount - 1))
Next lngCount
End Function

To convert a hex string to Long just use (for strHex) :

lngConvert = Clng(Val("&H" & strHex))

Then store as Longs

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top