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

converting variable from char to num 2

Status
Not open for further replies.

jed2003

Programmer
Mar 25, 2003
2
US
I am trying to convert a three character variable to a numeric field. I have divided the variable in to three substrings.
The reason for this change: entering a variable as range 1 and range 2, need to keep incrementing range 1 til it equals range 2. Example: range 1 = 100, range 2 = 103, the difference is 4 so the range will need to be incremented four times, 100, 101, 102, 103. Now for char range 1 = AA8, range 2 = AB1, the difference is 4 and the values need to be AA8, AA9, AB0, AB1.
Any assistance would be greatly appreciated. I have tried VAL, ACS,VMS.
 
What base are your using for the character range (AA8 & AB1)? There's always a better way...
 
Hi

Try using the SUBSTR() function along with VAL():

SUBSTR(<expC> ,<start expN> [, <length expN>])

e.g if u had myString = &quot;ABC123YZ&quot;
u need to extract the numeric part to a variable


e.g

numpart = SUBSTR(myString,4,3)

(thus numpart = &quot;123&quot;)

u can change numpart to a value

e.g. numpart = VAL(SUBSTR(myString,4,3))

u can build further by adding to the VAL part

e.g. numpart = VAL(SUBSTR(myString,4,3)) + 1

this gives u 124 as numeric

to add back to string u must

e.g. build it from the 3 parts

for clarit create 3 seperate variables

String1 = SUBSTR(myString,1,3) (gives u &quot;ABC&quot;)

numpart as described above

String2 = SUBSTR(myString,7,2) (gives u &quot;YZ&quot;)

then add them together remebering to convert the numpart to a string

e.g. NewStr = String1 + LTRIM(STR(numpart)) + String2
should give u &quot;ABC124YZ&quot;

the LTRIM function is used to strip leading blanks.


HTH


 
Not sure if this is what you're trying to accomplish (or only half of it), but I was bored and wrote the following function to convert hexadecimal values (it looks like that's what your character variable is) to decimal. Call it by passing your char variable to be converted like : MyNewVar = HEX2DEC('AA8')
**********************************************************
function hex2dec
parameter lcInchars

** initialize variables
lnNchar = len(alltrim(lcInchars)) && length of string to be converted
lnOutVal = 0 && value to be returned
lnRtPos = 1 && counter

do while lnRtPos <= lnNchar

lcChar = substr(lcInchars,lnNchar-lnRtPos+1)

do case
case upper(lcChar) = '0'
lnNumVal = 0
case upper(lcChar) = '1'
lnNumVal = 1
case upper(lcChar) = '2'
lnNumVal = 2
case upper(lcChar) = '3'
lnNumVal = 3
case upper(lcChar) = '4'
lnNumVal = 4
case upper(lcChar) = '5'
lnNumVal = 5
case upper(lcChar) = '6'
lnNumVal = 6
case upper(lcChar) = '7'
lnNumVal = 7
case upper(lcChar) = '8'
lnNumVal = 8
case upper(lcChar) = '9'
lnNumVal = 9
case upper(lcChar) = 'A'
lnNumVal = 10
case upper(lcChar) = 'B'
lnNumVal = 11
case upper(lcChar) = 'C'
lnNumVal = 12
case upper(lcChar) = 'D'
lnNumVal = 13
case upper(lcChar) = 'E'
lnNumVal = 14
case upper(lcChar) = 'F'
lnNumVal = 15
endcase

lnOutVal = lnOutVal + (lnNumVal*(16**(lnRtPos-1)))
lnRtPos = lnRtPos + 1
enddo
return lnOutVal
******************************************************

There are probably better, faster ways to do this but it works. I'm not aware of any functions for hex arithmetic, so I'll be interested in other's responses.

Hope this helps. If not, hey, it gave me something to occupy my time this evening.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top