Click to See Complete Forum and Search --> : Converting ASCII (byte) to Decimal


jasiu
October 16th, 2001, 09:30 AM
I have this string HEX values 02 CB 02 and its integer value is 715 or 727. Is there a function or how would I go about converting it to a decimal ?

Thank you

Cimperiali
October 16th, 2001, 09:41 AM
put &H before the hex value.
Ie:
MsgBox &H2CB02
will display the dec value

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

jasiu
October 16th, 2001, 09:45 AM
That is incorrect, perhaps I am not explaining myself.

02 CB 02 are a hex output from a BINARY long variable. How do I convert it to Decimal ?

Thank you

Cimperiali
October 16th, 2001, 10:06 AM
I do not have answer for sure, as I did not played a lot with math...
may dec value be 812?
Print &H2 * &HCB * &H2
(which is= 2* 203*2)

I also tried:
(&H2) ^ 0 + (&HCB) ^ 1 + (&H2) ^ 2
which gave only 208...

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

jasiu
October 16th, 2001, 10:25 AM
Ok, heres what it is, if A=715, then visual basic stores that in memory, it stores it as 02 CB 02 ....

Now how do I get it to tell me the value only when I have the "memory" dump ?

Thx

Cimperiali
October 16th, 2001, 10:39 AM
odds, you know?
715 => hex = 02 CB
last 02 should be something else (maybe representation of sign?)

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

jasiu
October 16th, 2001, 10:45 AM
Well it is as 02 but the value of A might is 727 or 715,

Now what I need I guess is a method of copying these values directly in memory or something. ???

Cimperiali
October 16th, 2001, 10:53 AM
...but I do not know what I am talking about. To me, ascii value of "A" is 65 and of "a" is 95. I do not even understand how it could be 715 or about...
However, if this may be of any help, there's an api called Copymemory
from api-guide:
"The CopyMemory function copies a block of memory from one location to another.
Requires Windows NT 3.1 or later; Requires Windows 95 or later
example:

private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst as Any, pSrc as Any, byval ByteLen as Long)
private Declare Function GetTickCount Lib "kernel32" () as Long
private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim sSave as string, Cnt as Long, T as Long, Pos as Long, Length as Long
Const mStr = "Hello "
Length = len(mStr)
sSave = Space(5000 * Length) 'make buffer for justified comparison
'get the current tickcount
T = GetTickCount
Pos = 1
sSave = Space(5000 * Length)
for Cnt = 1 to 5000
mid(sSave, Pos, Length) = mStr
Pos = Pos + Length
next Cnt
'Show the results
MsgBox "It took Visual basic" + Str$(GetTickCount - T) + " msecs. to add 5000 times a string to itself."
'get the current tickcount
T = GetTickCount
Pos = 0
sSave = Space(5000 * Length)
for Cnt = 1 to 5000
CopyMemory byval StrPtr(sSave) + Pos, byval StrPtr(mStr), LenB(mStr)
Pos = Pos + LenB(mStr)
next Cnt
'Show the results
MsgBox "It took CopyMemory" + Str$(GetTickCount - T) + " msecs. to add 5000 times a string to itself."
End Sub





Hope this may help


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

Iouri
October 16th, 2001, 12:38 PM
Private Const MaxPower As Byte = 31 'maximum power of 2 we can do because 2^31 is the maximum value of a long

Private Sub cmdBTD_Click()
Dim i As Integer
Dim CurrentDec As Double

'the formula to use here is: for each binary digit, we
'sum to the actual decimal number: digit_value * 2 ^ character_position
' so a binary number like this: 1101
' will result in:
' 1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0
' what means: 8 + 4 + 0 + 1 = 13

For i = 1 To Len(txtBIN)
CurrentDec = CurrentDec + CLng(Mid(txtBIN, i, 1)) * (2 ^ (Len(txtBIN) - i))
Next
txtDEC.Text = CurrentDec
End Sub






Iouri Boutchkine
iouri@hotsheet.com

shobamukund
October 16th, 2001, 01:24 PM
write out the hex number, placing each digit under the appropriate decimal value for that position. Multiply the decimal value by the base 16 digit and add the values. (Convert A through F to their decimal equivalent before multiplying).
The decimal equivalent of 2C is 44
c = 12...so
2 * 16^1 + 12 * 16^0
32+ 12 = 44.

Hence decimal equvivalent of 2CB02 is definitely not 715 or 727. The best way to approach is to either convert this value to Long or Double using CLng or CDbl

John G Duffy
October 16th, 2001, 04:43 PM
The decimal value for a hex 02CB02 = 183042 not the 712 or 715 you expect. HEx values are base 16 so it computes something like this
Digit 0 = 1,048,576 * 0 = 0
Digit 2 = 65,526 * 2 = 131072
Digit C = 4,096 * 12 = 49152
Digit B = 256 * 11 = 2816
Digit 0 = 16 * 0 = 0
Digit 2 = 0 + 2 = 2

John G

John G Duffy
October 16th, 2001, 06:19 PM
Slight error
Digit 2 should be 65536 not 65526

John G

John G Duffy
October 16th, 2001, 06:51 PM
option Explicit


public Function ConvertIt(D1, D2, D3)
Dim x as Long, x1 as Long
Dim y as Long, y1 as Long
Dim z as Long, z1 as Long

x = Int(D1 / 65536)
x1 = D1 Mod 65536
y = Int(D2 / 256)
y1 = D2 Mod 256
z = Int(D3 / 16)
z1 = D3 Mod 16
ConvertIt = (x * 1048576) + (x1 * 65536) + (y * 4096) + (y1 * 256) + (z * 16) + z1
End Function

private Sub Command1_Click()
Dim lRet as Long
Dim a as Byte
Dim b as Byte
Dim c as Byte
a = &H2
b = &HCB
c = &H2
lRet = ConvertIt(a, b, c)
MsgBox lRet
End Sub




John G

Vikranth
October 17th, 2001, 10:43 AM
Hi,
I did not get the exact problem. But try this function you might get the decimal value of Hex.


private Function HexToDec(strHex as string) as Long
Dim lngHexFromAToF as Long
Dim strEachCharFromstrHex as string

Do While (strHex <> "")
strEachCharFromstrHex = mid(strHex, 1, 1)
Select Case UCase(strEachCharFromstrHex)
Case "A"
lngHexFromAToF = 10
Case "B"
lngHexFromAToF = 11
Case "C"
lngHexFromAToF = 12
Case "D"
lngHexFromAToF = 13
Case "E"
lngHexFromAToF = 14
Case "F"
lngHexFromAToF = 15
End Select
If lngHexFromAToF = 0 then
If IsNumeric(strEachCharFromstrHex) then
HexToDec = HexToDec + CLng(strEachCharFromstrHex) * 16 ^ (len(strHex) - 1)
else
HexToDec = 0
Exit Do
End If
else
HexToDec = HexToDec + lngHexFromAToF * 16 ^ (len(strHex) - 1)
lngHexFromAToF = 0
End If
strHex = mid(strHex, 2, len(strHex))
Loop
End Function