Sometimes we use a conversion between 10 and other systems, such as the conversion of a 10-16 binary. Here I have written a function that can easily convert 10 into other systems.
The function is CHANGEJZ (long shi,int How) where the parameter Shi is the 10 feed data to be converted, how is the type to be converted to, the value is 2,8,16 three, and the return value of the entire function is string.
As we all know, to convert 10 into such as 8, first to convert to 2, and then from 2 to 8 into the system, so the first step of the function is: the input of the data from 10 to 2 into the system.
2 in-system |
8 in-system |
10 in-system |
16 in-system |
10110010 |
262 |
178 |
B2 |
10110111011 |
2673 |
1467 |
5BB |
The overall code for the function is as follows: (The following figures are given in 10 to 1467 for example)
String Two,sixteen,eight//defines three string variables to hold the last output value
Long Lentwo,duan,i,j
String aa[50],a[50],b[4]//definition array
First, convert the 10 data into the 2 by the remainder of the system.
Since the conversion of 10 to 2 is achieved by the remainder, the MoD () function used here, through real//practice, discovers that 4 lines of code can convert 10 to 2
Do While shi>0
two = two + string (mod (shi,2))
Shi=shi/2
Loop
Choose case how//Select Output system
Case 2//select 2
Because the 2 data in the above direction is the opposite: 11011101101, so the reverse () function to reset//reverse it.
Two=reverse (two)//get the correct 2-in-system 10110111011
Return two//returns function value
Case 8//select 8
The conversion of 2 to 8 is such that, from right to left, every 3 bits are one segment, that is, the 8-in-system one,
Lentwo=len (two)//take 2 length of feed
If mod (lentwo,3) > 0 then//segmented
DUAN=LENTWO/3 + 1
Else
Duan=lentwo/3
End If
For i= Duan to 1 step-1//due to no upside down 2 is from the right to the left, so//the segment from left to right is from big to small
Aa[duan]=mid (Two,i * 3-2, 3)//Remove 2 values from each segment
a[duan]= ' 0 '
For J=1 to 3
B[j]=mid (aa[duan],j,1)//Every paragraph from right to left to take a value
If B[j]<> ' Then
A[duan]=string (Integer (A[duan)) + 2^ (j-1) * Integer (B[J))
Follow 2^0 * x + 2^1 *x +2^2 * x
X is a Right-to-left 2 value, which accumulates the 8 value of each segment
End If
Next
Eight=eight + A[duan]//8 to add the final 8 value
Next
Return eight
The 16 method is like 8, but every 4 bits is a paragraph.
Case 16//16 System
Lentwo=len (two)
If mod (lentwo,4) > 0 Then
DUAN=LENTWO/4 + 1
Else
Duan=lentwo/4
End If
For i= Duan to 1 step-1
Aa[duan]=mid (Two,i * 4-3, 4)
a[duan]= ' 0 '
For J=1 to 4
B[j]=mid (aa[duan],j,1)
If B[j]<> ' Then
A[duan]=string (Integer (A[duan)) + 2^ (j-1) * Integer (B[J))
End If
Choose Case Integer (A[duan])
Case 10
a[duan]= ' A '
Case 11
a[duan]= ' B '
Case 12
a[duan]= ' C '
Case 13
a[duan]= ' D '
Case 14
a[duan]= ' E '
Case 15
a[duan]= ' F '
End Choose
Next
Sixteen=sixteen + A[duan]
Next
Return sixteen
Case Else
End Choose
Interested friends can also calculate the value of the right of the decimal point, but he is a negative power of 2. The above code is passed under Windows2000 Professional + powerbuilder80.