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

Convert a string to a number

Status
Not open for further replies.

Per9922

IS-IT--Management
Oct 1, 2004
74
SE
Hello,

I have a string of 8 bytes that I would like to convert to a number. I read the bytes from a file and I need to change the order of the bytes before converting the number to a string according to the following.

96 6A 45 43 39 01 00 00 -> 00 00 01 39 43 45 6A 96

How do I do I change the order and convert the string to a number in VBA ?

Thanks Per
 
Hi Per,

easiest would be to Split the string with delimiter [space], then to re-concatenate the string in a backwards loop.
Something like this:
Code:
tempstr=Split(oldString, " ")
for i = ubound(tempstr) to 0 [b]Step -1[/b]
  newstring = newstring & tempstr(i)
next i

;-)

Cheers,
MakeItSo


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
MakeItSo: Haven't you forgotten to restore the spaces??

Try something based on:
Code:
Sub Reverse()
Dim StrIn As String
StrIn = "96 6A 45 43 39 01 00 00"
MsgBox Reversed(StrIn)
End Sub

Function Reversed(Str As String) As String
If (Len(Str) > 1) Then
  Reversed = Reversed(Mid$(Str, 2)) + Left$(Str, 1)
Else
  Reversed = Str
End If
End Function

Cheers
Paul Edstein
[MS MVP - Word]
 
macropod said:
MakeItSo: Haven't you forgotten to restore the spaces??
Indeed! Not exactly rocket-science to put them back in though.
Code:
for i = ubound(tempstr) to 0 Step -1
  newstring = newstring & tempstr(i)[COLOR=#CC0000] & " "[/color]
next i 
newstring=left(newstring, len(newstring)-1) [COLOR=#4E9A06]'to remove the final, superfluous space[/color]
[tongue]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
I knew that!!

PS: newstring=Trim(newstring)

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks all!

How to convert the string to a number ?
 
6A?

=>Do you mean Number as "decimal number value of the entire string" or "decimal value of each two-digit hex code" or... ??
A bit more detail in your question would be helpful.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Sorry!

"decimal number value of the entire string" :)
 
It's a string containing a series of numbers! If you want to process the numbers individually, you'll need to do something like:
Code:
Sub Demo()
Dim Str As String, i As Long
Str = "96 6A 45 43 39 01 00 00"
For i = UBound(Split(Str, " ")) To 0 Step -1
  MsgBox Split(Str, " ")(i)
Next i
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
And I have another string (4 bytes) that I would like to convert to float
 
In that case you don't need the spaces and my original code would do.
The ouput then is:
0000013943456A96

A simple conversion will do:
Code:
DecVal=CLng("&H" & newstring)
or as float
Code:
DecVal=CDbl("&H" & newstring)

:)

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
in your example
[tt]
96 6A 45 43 39 01 00 00 -> 00 00 01 39 43 45 6A 96
[/tt]
you show the result for reversing the string, but you do not show the expected result for converting this string of ALPHA NUMERIC CHARACTERS to a number nor do you explain the LOGIC that you expect to be applied to this conversion.

Can we read your mind?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry I failed to read your entire post [blush]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
So you have a hex string to convert to decimal

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks! I get the first issue solved! but how to convert to floating value ...

I have the string 3B46E03D that should convert to 0,109508953989
 
Why?
How do you determine where the decimal point is?
Is it always "0,..." or can it also result in "1,...." or "23,114..."?

If it is always "0," followed by the decimal value, you could simply do something like this:
Code:
CDbl("0," & Cstr(DecValue))
Not pretty, but should do the trick.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
1 Sign bit
8 bits of biased exponent
23 bits of mantisse
Don't you think that might have been a helpful piece of information to share with the members trying to help you?

And since your string was to be transposed, maybe even the ORDER of where the sigm, exponent & mantissa exists in your example.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry :(

I thought there was som easy function that are doing this that I have not found.
 
manually or function?
=>Both!! [bigcheeks]

=>You use your hand (manually) to type a function!

What you need to do in this scenario is
a) convert your hex string to binary. See here: b) parse the binary string like shown in the link you gave yourself
First determine positive/negative number:
Code:
negativeFlag=(left(BinString,1)=1)

Then use
Code:
Mid(BinString,2,8)
to read the exponent and calculate its value according to
Then use
Code:
Mid(BinString,10,23)
to read the mantissa.


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top