PDA

View Full Version : HEX to BCD conversion



Don Mario
- 1st February 2005, 18:17
Hi guys

How can I convert a number in hex 4 bytes into decimal 10 digit !
With PBP of course !

Thank you very much

Don Mario

Dwayne
- 1st February 2005, 21:25
Hello Don,


Don>>How can I convert a number in hex 4 bytes into decimal 10 digit ! <<

Humm....I am sure there are many ways...This way below has NOT been tested, but I think it will work...Or will work with minor changes.



No4 VAR BYTE
No3 VAR BYTE
No2 VAR BYTE
No1 VAR BYTE
Co1 VAR BYTE


VAR Val[10]="0000000000"


'Increment ones
For Co1=0 TO No1 STEP 1
Val[10]=Val[10]+1;
GoSub Increment
Next Co1

'Increment 256
For Co1=0 TO No2 STEP 1
Val[10]=Val[10]+6;
Val[9]=Val[9]+5;
Val[8]=Val[8]+2;
GoSub Increment
Next Co1


'Increment 65536
For Co1=0 TO No2 STEP 1
Val[10]=Val[10]+6;
Val[9]=Val[9]+3;
Val[8]=Val[8]+5;
Val[7]=Val[7]+5;
Val[6]=Val[6]+6;

GoSub Increment
Next Co1



'Increment 16777216
For Co1=0 TO No2 STEP 1
Val[10]=Val[10]+6;
Val[9]=Val[9]+1;
Val[8]=Val[8]+2;
Val[7]=Val[7]+7;
Val[6]=Val[6]+7;
Val[5]=Val[5]+7;
Val[4]=Val[4]+6;
Val[3]=Val[3]+1
GoSub Increment
Next Co1



For Co1=10 TO 0 STEP -1
LCDOut Val[Co1]
Next Co1

Loop:
GoTo Loop


Increment:
IF Val[10]>9 Then
Val[9]=Val[9]+1
Val[10]=Val[10]-10;
EndIF

IF Val[9]>9 Then
Val[8]=Val[8]+1
Val[9]=Val[9]-10;
EndIF

IF Val[8]>9 Then
Val[7]=Val[7]+1
Val[8]=Val[8]-10;
EndIF

IF Val[7]>9 Then
Val[6]=Val[6]+1
Val[7]=Val[7]-10;
EndIF

IF Val[6]>9 Then
Val[5]=Val[5]+1
Val[6]=Val[6]-10;
EndIF

IF Val[5]>9 Then
Val[4]=Val[4]+1
Val[5]=Val[5]-10;
EndIF

IF Val[4]>9 Then
Val[3]=Val[3]+1
Val[4]=Val[4]-10;
EndIF

IF Val[3]>9 Then
Val[2]=Val[2]+1
Val[3]=Val[3]-10;
EndIF

IF Val[2]>9 Then
Val[1]=Val[1]+1
Val[2]=Va[2]-10;
EndIF

IF Val[1]>9 Then
Val[0]=Val[0]+1
Val[1]=Va[1]-10;
EndIF
Return


End

Acetronics2
- 2nd February 2005, 07:20
Hello, Guys

I Saw on this forum a thread about 32 bits addition with PBP( by Mel ... of course !!! )

as the result is always smaller than 32 bits, it might be widely usable, no ???

Alain

Don Mario
- 2nd February 2005, 10:25
Thanks DWAYNE

I try your sugestion,but it don't work ! On lcd apear chinese
caracter ! And in appareance there is no link with HEX value !
I say that because in hex value,change only the LSB but in chinese caracter change 5 digit.
But i'm not shure because I can't read in chinese !


Don Mario

Acetronics2
- 2nd February 2005, 11:13
Hi don ...

May be you forgot a "#" somewhere ....

Alain

Don Mario
- 2nd February 2005, 11:23
Yes ACETRONICS ! "#" was the problem with chinese caracter ! But the conversion don't work !
In hex I have 18875 and in bcd 104007715730 !

Don Mario

Acetronics2
- 2nd February 2005, 12:05
PBP is not as clever as us ...

did you tell him the numbers you work on were Hex ???

Alain

Don Mario
- 2nd February 2005, 12:31
Yes, shure !
The 4 byte hex number is result of a measurement and I see on the lcd in hex ! The converted bcd number I see also on lcd but it's no link between 2 number !


Don Mario

Acetronics2
- 2nd February 2005, 12:50
I would simply have cut the hex number into 2 16 bits parts, converted them in dec ( or not !!! ), then execute a mult 32 operation on the 16 bits higher part ( hi part x 65536 ...) - see DIV 32 and the "dummy" for that.

- see Mel's posts about how to load 32 bits numbers for having correct result locations.

and finally add the lower part with the 32 bits addition ...

that looked a simple way to me ...( too much ??? )

Alain

Dwayne
- 2nd February 2005, 14:39
Hello Don,

Don>>In hex I have 18875 and in bcd 104007715730 ! <<

My example was not meant for this kind of conversion Don.

It was made for a actual Hex between 00 and ff... 4 of them.

For the above, you will have to use Mels example of about 3 months ago, or do a Dig on each variable

the Dig command will take out each number of 18875.. then you can use my code and instead of looping 256, 65536, and 16777216, you loop 16, 256, 4096, 65536, and 1048576 to get your decimal value.

Dwayne