Converting byte data to integer, floats ,etc

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Converting byte data to integer, floats ,etc

1. Ken Hickey Guest

## Converting byte data to integer, floats ,etc

Hi,

I'm a new VB programmer and I'm having a **** of a time trying to pass byte
data
to INTEGER and SINGLE data types. For example, I wish to interpret two contiguous
bytes
as an INTEGER value or 4 contiguous bytes as a SINGLE data type, etc. Is
there a simple way
to do this without going into the IEEE formats for these things. I remember
from my Fortran
days that an EQUIVALENCE statement did the same thing.

Ken

2. rhwiebe Guest

## Re: Converting byte data to integer, floats ,etc

Public Function ifun2StringToInteger(ByVal strString As String) As Integer

' Converts 2 char based 256 string to Integer
' assumes valid number
' note that this assumes positive values
' errors returns 0

''Global the var to speed up the function lfun2StrToInteger
'Global intInteger As Integer

On Error GoTo ifun2StringToInteger_error
If Len(strString) < 2 Then GoTo ifun2StringToInteger_error
intInteger = Asc(Mid(strString, 1, 1))
ifun2StringToInteger = (intInteger * 256) + (Asc(Mid(strString, 2, 1)))

' depends on if you have big or little endian string
'If boolBigEndian Then
' intInteger = Asc(Mid(strString, 1, 1))
' ifun2StringToInteger = (intInteger * 256) + (Asc(Mid(strString, 2, 1)))
'Else
' intInteger = Asc(Mid(strString, 2, 1))
' ifun2StringToInteger = (intInteger * 256) + (Asc(Mid(strString, 1, 1)))
'End If

Exit Function
ifun2StringToInteger_error:
ifun2StringToInteger = 0
Call subErrorInX(" ERROR: en ifun2StringToInteger: " & Err.Number & " " &
Err.Description)
Exit Function
End Function

How to do a real? You got me there! If you find out,
let the worls know! (me too please!)
Good luck

"Ken Hickey" <khickey@thezone.net> wrote:
>
>Hi,
>
> I'm a new VB programmer and I'm having a **** of a time trying to pass

byte
>data
>to INTEGER and SINGLE data types. For example, I wish to interpret two contiguous
>bytes
>as an INTEGER value or 4 contiguous bytes as a SINGLE data type, etc. Is
>there a simple way
>to do this without going into the IEEE formats for these things. I remember
>from my Fortran
>days that an EQUIVALENCE statement did the same thing.
>
>Ken

3. rhwiebe Guest

## Re: Converting byte data to integer, floats ,etc

Sorry: forgot - I have one for LONG also:

Public Function lfun4StringToLong(ByVal strString As String) As Long

' Converts any 4 char hex string to long
' assumes valid hex number
' note that this assumes positive values
' assumes big endian

''Global the vars to speed the function lfun4StringToLong
'Global lngNum1 As Long
'Global lngNum2 As Long
'Global lngNum3 As Long
'Global lngNum4 As Long
' assumed globals:
' boolBigEndian

On Error GoTo lfun4StringToLong_error
If Len(strString) < 4 Then GoTo lfun4StringToLong_error
lngNum1 = Asc(Mid(strString, 1, 1))
lngNum2 = Asc(Mid(strString, 2, 1))
lngNum3 = Asc(Mid(strString, 3, 1))
lngNum4 = Asc(Mid(strString, 4, 1))
lfun4StringToLong = (lngNum1 * 16777216) + (lngNum2 * 65536) + (lngNum3 *
256) + lngNum4
'If boolBigEndian Then
' lfun4StringToLong = (lngNum1 * 16777216) + (lngNum2 * 65536) + (lngNum3
* 256) + lngNum4
'Else
' lfun4StringToLong = (lngNum4 * 16777216) + (lngNum3 * 65536) + (lngNum2
* 256) + lngNum1
'End If
Exit Function

lfun4StringToLong_error:
lfun4StringToLong = 0
Call subErrorInX(" ERROR: en lfun4StringToLong: " & Err.Number & " " & Err.Description)
Exit Function
End Function

"rhwiebe" <rhwiebe@usa.net> wrote:
>
>Public Function ifun2StringToInteger(ByVal strString As String) As Integer
>
>' Converts 2 char based 256 string to Integer
>' assumes valid number
>' note that this assumes positive values
>' errors returns 0
>
>''Global the var to speed up the function lfun2StrToInteger
>'Global intInteger As Integer
>
>On Error GoTo ifun2StringToInteger_error
>If Len(strString) < 2 Then GoTo ifun2StringToInteger_error
>intInteger = Asc(Mid(strString, 1, 1))
>ifun2StringToInteger = (intInteger * 256) + (Asc(Mid(strString, 2, 1)))
>
>' depends on if you have big or little endian string
>'If boolBigEndian Then
>' intInteger = Asc(Mid(strString, 1, 1))
>' ifun2StringToInteger = (intInteger * 256) + (Asc(Mid(strString, 2,

1)))
>'Else
>' intInteger = Asc(Mid(strString, 2, 1))
>' ifun2StringToInteger = (intInteger * 256) + (Asc(Mid(strString, 1,

1)))
>'End If
>
>Exit Function
>ifun2StringToInteger_error:
>ifun2StringToInteger = 0
>Call subErrorInX(" ERROR: en ifun2StringToInteger: " & Err.Number & " "

&
>Err.Description)
>Exit Function
>End Function
>
>How to do a real? You got me there! If you find out,
>let the worls know! (me too please!)
>Good luck
>
>"Ken Hickey" <khickey@thezone.net> wrote:
>>
>>Hi,
>>
>> I'm a new VB programmer and I'm having a **** of a time trying to pass

>byte
>>data
>>to INTEGER and SINGLE data types. For example, I wish to interpret two

contiguous
>>bytes
>>as an INTEGER value or 4 contiguous bytes as a SINGLE data type, etc. Is
>>there a simple way
>>to do this without going into the IEEE formats for these things. I remember
>>from my Fortran
>>days that an EQUIVALENCE statement did the same thing.
>>
>>Ken

>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise