Convert integer to binary hex value

techsupportpete
06-09-2005, 03:55 AM
Hi
I have a variable which is for sake of argument the Size of a File in Bytes. The value is an integer

I wish to convert this value into a binary string so that I can transmit it via the ethernet winsock control.
Basically I want to convert the whole number using something like the CHR function. However I also need the number to be padded to 'n' bytes....



FileSize = 123456
n = 6

DataToSend = convert(filesize,n) ' <-- this is the function Im stuck with

The result would be DataToSend = 0x 01E240




Any ideas / suggestions?

Cheers

Pete
:D

spikey_richie
06-09-2005, 05:53 AM
Can be tailored to suit.



Function DecToBin(ByVal value As Long, Optional digits As Long = -1) As String
Dim result As String, exponent As Integer
' this is faster than creating the string by appending chars
result = String$(32, "0")
Do
If value And Power(exponent) Then
' we found a bit that is set, clear it
Mid$(result, 32 - exponent, 1) = "1"
value = value Xor Power(exponent)
End If
exponent = exponent + 1
Loop While value
If digits < 0 Then
' trim non significant digits, if digits was omitted or negative
Bin = Mid$(result, 33 - exponent)
Else
' else trim to the requested number of digits
DecToBin = Right$(result, digits)
End If
End Function

techsupportpete
06-09-2005, 06:08 AM
looks good - thanks!

Can you tell me where or what the 'Power' function is? As it does not run ....

kind regards

pete

TeraBlight
06-09-2005, 06:12 AM
looks good - thanks!

Can you tell me where or what the 'Power' function is? As it does not run ....

kind regards

pete

I'm guessing it's 2^exponent ...?

spikey_richie
06-09-2005, 06:45 AM
Sorry, missed the 'Power' routine:



Function Power(ByVal exponent As Long) As Long
Static res(0 To 31) As Long
Dim i As Long

' rule out errors
If exponent < 0 Or exponent > 31 Then Err.Raise 5

' initialise the array at the first call
If res(0) = 0 Then
res(0) = 1
For i = 1 To 30
res(i) = res(i - 1) * 2
Next
' this is a special case
res(31) = &H80000000
End If

' return the result
Power = res(exponent)

End Function

techsupportpete
06-09-2005, 07:07 AM
I got the function working...thanks.

However the function returns an Ascii String representation of the binary number.

What I need is to be able to convert the number (example) 123456 decimal

into hex string: 01 E2 40

BUT they key is that it shouldnt be ASCII....ie I dont want the string to contain the ascii equivalent of the string: (3031 6532 3430)
i really want it to be 0x01 0xE2 and 0x40

Does that make sense?

eg if I had the number 37 I could write it to the output string using: chr(37) which would put 0x25 into the string....

But how do I do this for a larger number, eg 123456 and how do I specify the result to be say 6 bytes long

kind regards

pete
:D

anthony_n
06-09-2005, 07:14 AM
I got the function working...thanks.

However the function returns an Ascii String representation of the binary number.

What I need is to be able to convert the number (example) 123456 decimal

into hex string: 01 E2 40

BUT they key is that it shouldnt be ASCII....ie I dont want the string to contain the ascii equivalent of the string: (3031 6532 3430)
i really want it to be 0x01 0xE2 and 0x40

Does that make sense?

eg if I had the number 37 I could write it to the output string using: chr(37) which would put 0x25 into the string....

But how do I do this for a larger number, eg 123456 and how do I specify the result to be say 6 bytes long

kind regards

pete
:D

what is it you are trying to do??? In vb there is a function called hex
but this returns a string as well

If you want to store the information as a hex var then that is not possible
you would have to store each part of the hex value into a byte array

IE

1E240

Byte 1 = 64
Byte 2 = 226
byte 3 = 1

Diurnal
06-09-2005, 07:20 AM
Can you use the Hex$() function and pad it?

Private Function SetHex6(ByVal lDec As Long) As String
'Return a 6 digit hex representation string.
'Input: an integer to convert.

SetHex6 = Right$("000000" & Hex$(lDec), 6)

End Function

techsupportpete
06-09-2005, 07:37 AM
This function converts the decimal to the hex value in a string...ie it does not convert to the actual hex values and put them in a string? confusing. sorry. guessing im not explaining properly.

example;

number 255, convert to hex is FF
using the hex function hex(255) returns a string "FF" but this is actually 46 46 (ascii equivalent of FF)

What I want is the result to be FF (ie 1 byte long)

So if converting 123456 it should return 01 E2 40 (3 bytes) rather than the ascii equivalent 3031 6532 3430 (6bytes)

Does that make it any clearer?

sorry for any confusion

pete

01 E2 40

Diurnal
06-09-2005, 08:32 AM
I guess I am confused. Unless you can use a byte array, as was suggested above, I'm not sure what to do here. VB uses unicode strings which are two bytes per character.

snarfblam
06-09-2005, 09:26 AM
Okay. Lets say you have a number, 538. What you want to do is convert it into an array of bytes? 538 in hex is $02 1B, so you want an array of bytes that equals {$02, $1B}. Is this right?

If so, then all the confusion is coming from the fact that what you are asking for is a "hex value". Inside the computer it is all binary. Just because we display the value as hex doesn't mean it is stored as hex. It is an array of bytes, which can represented as hex, but also as binary or decimal. As a matter of fact, or {$02, $1B} is actually exactly the same as {2, 27}, which is the same as {00000010, 00011011}. And your "binary string", I suppose, would be an array of bytes (which could be stored in the form of a string).

Suppose we have an integer 16706. To store it in a byte array, we could do this:

Const IntVal As Integer = 16706

'We do this to treat the number as unsigned
Dim MyValue As Long
MyValue = IntVal
If MyValue < 0 Then MyValue = MyValue + 65536

'Extract the bytes
Dim Bytes(1) As Byte
Bytes(1) = CByte(MyValue And 255)
MyValue = MyValue \ 255
Bytes(0) = Cbyte(MyValue And 255)
'Bytes now holds the bytes from IntVal

If you want that in the form of a string, the next step would be

Dim MyString As String * 2 = Chr(Bytes(0)) & Chr(Bytes(1))

As mentioned, VB6 uses Unicode internally, and has to convert to ASCII or different codepages sometimes. This means that you will be wasting a byte on each character, and you need to be careful because it is possible that VB will have do some sort of conversion on your string, altering the binary values, and ultimately corrupting your data.

I hope that I understood your question correctly. It seems like everyone interpreted it differently. I recommend that you make an effort to use clear and precise terminology so that your question can be answered more quickly and accurately. For example, ask how to convert an integer to an array of bytes or a string whose ASCII values equal the individual bytes that represent the integer (if that is what you are after).

techsupportpete
06-09-2005, 09:42 AM
Yes. Thanks marble_eater.
This solved my problem.

Yes, I agree that my initial question was a little confused, and I appologise for any confusion.

thanks for everyone's help

kind regards
pete

trisight
06-09-2005, 09:59 AM
You don't have to convert a number to hex or binary to transmit it using the winsock control. You can send and receive pretty much any data type through the winsock.. if I'm understanding your intention purposes correctly.

OnErr0r
06-09-2005, 11:04 AM
trisight is correct, in that the SendData method takes a variant, so you could pass any data type. However, it is often necessary to pass a string of data containing various types in a single call to SendData. I think this is pete's aim here.

If a string is to contain binary data then there will be a unicode conversion done by the SendData method. In order to prevent that conversion (and need for an opposite conversion on the other end) you should consider using a byte array.

Here are two methods to put a long into an array of bytes. RtlMoveMemory looks cleaner, but actually takes longer.


Option Explicit

Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)

Private Sub Form_Load()
Dim b(3) As Byte
Dim i As Long

i = &H87654321

'RtlMoveMemory b(0), i, 4

LongToByte b, i

' Proof that it worked:
For i = 0 To 3
Debug.Print Hex$(b(i))
Next i
End Sub

Private Sub LongToByte(ByRef b() As Byte, ByVal i As Long)
b(3) = ((i And &HFF000000) \ 16777216) And &HFF ' Fake an unsigned shift
i = i And &HFFFFFF ' Remove the negative portion
b(2) = i \ 65536
b(1) = (i \ 256) And &HFF
b(0) = i And &HFF
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum