Author |
Message |
Jack Master #1 / 13
|
Converting hex value in str format to hex
Hello again, Ok. I've bought a few books on VBScript and I can't seem to find a build in method to convert a string value containing: VarA = "00000000001a" to hex. After conversion I would need to subtract 1 from it. TIA
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Chad Myer #2 / 13
|
Converting hex value in str format to hex
Quote: > Hello again, > Ok. I've bought a few books on vbscript and I can't seem to find a build in > method to convert a string value containing: > VarA = "00000000001a" > to hex. After conversion I would need to subtract 1 from it.
I'm not sure what the "a" is on the end, but the way to convert a string to a number is with CInt(string) or CLng(string). This will convert 0000000001 to 1 numeric from which you can subtract 1. It will not, however, convert "000F" to 16, though, there's no intrinsic way to do that in VBScript that I know of (but you could write one!) -Chad
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Adam D. Barrat #3 / 13
|
Converting hex value in str format to hex
"Jack Masters" wrote ... Quote: > Ok. I've bought a few books on vbscript and I can't seem to find a build in > method to convert a string value containing: > VarA = "00000000001a" > to hex. After conversion I would need to subtract 1 from it.
You'd need to do this in steps: 1 Convert VarA into a string which is recognisable as a hex number (prefix it by "&H") 2 Convert that string into decimal 3 Subtract 1 4 Convert the result back into Hex e.g. VarA = "00000000001a" Msgbox Hex( Eval( "&H" & VarA ) - 1 ) This gives the expected result of "19" - you could pad this out with leading zeroes if so desired, but since that's no different than padding any other string, I'll leave that to you ;-) hth hand Adam
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Adam D. Barrat #4 / 13
|
Converting hex value in str format to hex
"Chad Myers" wrote ... [...] Quote: > I'm not sure what the "a" is on the end
It's a hex digit - admitedly it's more often written as "A", but either is acceptable... Quote: > but the way to convert a string to a number is with CInt(string) or
CLng(string). This will Quote: > convert 0000000001 to 1 numeric from which you can subtract 1.
That doesn't give the correct result though, given the previous comment (i.e. it should produce 26, not 1) Quote: > It will not, however, convert "000F" to 16, though, there's no > intrinsic way to do that in VBScript that I know of (but you could > write one!)
There is a more-or-less intrinsic way to do it, so long as you prefix the hex number by "&H"... hand Adam
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Chad Myer #5 / 13
|
Converting hex value in str format to hex
Quote: > "Chad Myers" wrote ... > [...] > > I'm not sure what the "a" is on the end > It's a hex digit - admitedly it's more often written as "A", but either is > acceptable...
Oh yeah, duh! Sorry, had a brain fart there... -Chad
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Jack Master #6 / 13
|
Converting hex value in str format to hex
Thanks again. Hex() works very well. I was looking at the method in the VBScript help file, but wasn't aware of the "&H" or "&O". Real lifesaver. JM
Quote: > "Jack Masters" wrote ... > > Ok. I've bought a few books on vbscript and I can't seem to find a build > in > > method to convert a string value containing: > > VarA = "00000000001a" > > to hex. After conversion I would need to subtract 1 from it. > You'd need to do this in steps: > 1 Convert VarA into a string which is recognisable as a hex number (prefix > it by "&H") > 2 Convert that string into decimal > 3 Subtract 1 > 4 Convert the result back into Hex > e.g. > VarA = "00000000001a" > Msgbox Hex( Eval( "&H" & VarA ) - 1 ) > This gives the expected result of "19" - you could pad this out with leading > zeroes if so desired, but since that's no different than padding any other > string, I'll leave that to you ;-) > hth hand > Adam
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Michael Harri #7 / 13
|
Converting hex value in str format to hex
You don't actually need the Eval(), CLng() works just as well... -- Michael Harris Microsoft.MVP.Scripting --
Quote: > "Jack Masters" wrote ... > > Ok. I've bought a few books on vbscript and I can't seem to find a build > in > > method to convert a string value containing: > > VarA = "00000000001a" > > to hex. After conversion I would need to subtract 1 from it. > You'd need to do this in steps: > 1 Convert VarA into a string which is recognisable as a hex number (prefix > it by "&H") > 2 Convert that string into decimal > 3 Subtract 1 > 4 Convert the result back into Hex > e.g. > VarA = "00000000001a" > Msgbox Hex( Eval( "&H" & VarA ) - 1 ) > This gives the expected result of "19" - you could pad this out with leading > zeroes if so desired, but since that's no different than padding any other > string, I'll leave that to you ;-) > hth hand > Adam
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
|
Chad Myer #8 / 13
|
Converting hex value in str format to hex
Is "&H1a" the same as &H1a though? How does that work? -Chad
Quote: > You don't actually need the Eval(), CLng() works just as well... > -- > Michael Harris > Microsoft.MVP.Scripting > --
> > "Jack Masters" wrote ... > > > Ok. I've bought a few books on vbscript and I can't seem to find a > build > > in > > > method to convert a string value containing: > > > VarA = "00000000001a" > > > to hex. After conversion I would need to subtract 1 from it. > > You'd need to do this in steps: > > 1 Convert VarA into a string which is recognisable as a hex number (prefix > > it by "&H") > > 2 Convert that string into decimal > > 3 Subtract 1 > > 4 Convert the result back into Hex > > e.g. > > VarA = "00000000001a" > > Msgbox Hex( Eval( "&H" & VarA ) - 1 ) > > This gives the expected result of "19" - you could pad this out with > leading > > zeroes if so desired, but since that's no different than padding any other > > string, I'll leave that to you ;-) > > hth hand > > Adam
|
Sat, 22 Mar 2003 03:00:00 GMT |
|
|
Michael Harri #9 / 13
|
Converting hex value in str format to hex
No, but older versions (before 5.0) of VBScript don't support Eval()... -- Michael Harris Microsoft.MVP.Scripting --
Quote: > "Michael Harris" wrote ... > > You don't actually need the Eval(), CLng() works just as well... > Fair point ;-) Is calling Eval() is this case really any more expensive > though? > Adam
|
Sat, 22 Mar 2003 03:00:00 GMT |
|
|
Adam D. Barrat #10 / 13
|
Converting hex value in str format to hex
"Michael Harris" wrote ... Quote: > You don't actually need the Eval(), CLng() works just as well...
Fair point ;-) Is calling Eval() is this case really any more expensive though? Adam
|
Sun, 23 Mar 2003 08:03:21 GMT |
|
|
Adam D. Barrat #11 / 13
|
Converting hex value in str format to hex
"Chad Myers" wrote ... Quote: > Is "&H1a" the same as &H1a though? How does that work?
Since all variables in VBS are variants, and CLng converts a variant of subtype x to a variant of subtype Long then, yes, they're identical... hth hand Adam
|
Sun, 23 Mar 2003 08:04:52 GMT |
|
|
Adam D. Barrat #12 / 13
|
Converting hex value in str format to hex
"Jack Masters" wrote ... Quote: > Thanks again.
No problem Quote: > Hex() works very well. I was looking at the method in the > VBScript help file, but wasn't aware of the > "&H" or "&O". Real lifesaver.
It's not immediately obvious, but there is actually a note on &H notation in the documentation for Hex() (at least in the current MSDN Library version of the docs) - it's in the "Remarks" section, under the table... hth hand Adam
|
Sun, 23 Mar 2003 08:07:28 GMT |
|
|
Adam D. Barrat #13 / 13
|
Converting hex value in str format to hex
"Michael Harris" wrote ... Quote: > No, but older versions (before 5.0) of VBScript don't support Eval()...
Doh! That'll teach me to ask silly questions early in the morning ;-) hand Adam
|
Sun, 23 Mar 2003 03:00:00 GMT |
|
|