HEX to BCD conversion


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2004
    Posts
    30

    Default HEX to BCD conversion

    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

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Lightbulb

    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

  4. #4
    Join Date
    May 2004
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi don ...

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

    Alain

  6. #6
    Join Date
    May 2004
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Question

    PBP is not as clever as us ...

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

    Alain

  8. #8
    Join Date
    May 2004
    Posts
    30


    Did you find this post helpful? Yes | No

    Default

    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

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Post

    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

  10. #10
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

Similar Threads

  1. Conversion from picbasic pro to hex for mplab
    By pr2don in forum Forum Requests
    Replies: 5
    Last Post: - 10th October 2010, 19:53
  2. Replies: 2
    Last Post: - 7th March 2008, 02:16
  3. Rotary BCD
    By Tobias in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 03:04
  4. Hex to Binary Conversion
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th September 2006, 21:25
  5. BCD conversion
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 17th October 2005, 19:24

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts