Hex values to decimal


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: Hex values to decimal

    to start with
    mai var byte[2]
    HSerin 1000,main, [wait ("41 05"), skip 1, hex mai]
    probably does not work as you intend

    input
    41 05xC , mai[0]=$C or 12 or %00001100
    41 05x2C , mai[0]=$2C or 44 or %00101100
    41 05x42C , mai[0]=$2C or 44 or %00101100
    41 05x552C , mai[0]=$2C or 44 or %00101100
    41 05x3FED2C , mai[0]=$2C or 44 or %00101100
    mai[1] takes no part in this

    secondly
    the data is always binary once "parsed" by the HEX modifier in hserin ,it just depends on how you display/interpret the data
    Warning I'm not a teacher

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Hex values to decimal

    Quote Originally Posted by richard View Post
    to start with


    probably does not work as you intend

    input
    41 05xC , mai[0]=$C or 12 or %00001100
    41 05x2C , mai[0]=$2C or 44 or %00101100
    41 05x42C , mai[0]=$2C or 44 or %00101100
    41 05x552C , mai[0]=$2C or 44 or %00101100
    41 05x3FED2C , mai[0]=$2C or 44 or %00101100
    mai[1] takes no part in this

    secondly
    the data is always binary once "parsed" by the HEX modifier in hserin ,it just depends on how you display/interpret the data
    Thanks Richard for answer, but still not clear for me. The serin input data is a string like:
    41 05 2B The "41 05" part represent the identificator and the "2B" is the value in hex which is changing and this part I want store in variable in dec format.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    Did you find this post helpful? Yes | No

    Default Re: Hex values to decimal

    You don't get to choose in what format the number is stored, it doesn't work like that.
    2B in hex
    43 in decimal
    00101011 in binary
    + in ASCII
    These are all the same thing and stored exactly the same in a variable.
    Code:
    mai = $2B
    mai = %00101011
    mai = 43
    mai = "+"
    Exactly the same result. It's just how we choose to interpret the information.

    If you want to receive a number expressed in a "human readable" format (ie represented as an ASCII string) you use the available DEC/HEX/BIN modifiers (as you are).

    The code, as it runs, will then interpret ASCII characters and convert to an actual number so that the ASCII string '2B' becomes the decimal value 43. There's no need to use an array in the HSERIN statement because you're not storing the '2' and the 'B', you're storing the decimal value 43 that the ASCII string '2B' represents.

    Then, when you do the LCDOUT you, again, use the DEC (or HEX or BIN) modifier to convert the decimal value 43 back into the ASCII characters '4' and '3' which are displayed on the LCD.

    Since your LCD shows 43 when you send '41 05 2B' everything is working exactly as expected and when you do decvalue = mai you'll copy the content of mai to decvalue so it TOO contains 43.

    I don't get what the problem is......

    /Henrik.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Hex values to decimal

    Quote Originally Posted by HenrikOlsson View Post
    You don't get to choose in what format the number is stored, it doesn't work like that.
    2B in hex
    43 in decimal
    00101011 in binary
    + in ASCII
    These are all the same thing and stored exactly the same in a variable.
    Code:
    mai = $2B
    mai = %00101011
    mai = 43
    mai = "+"
    Exactly the same result. It's just how we choose to interpret the information.

    If you want to receive a number expressed in a "human readable" format (ie represented as an ASCII string) you use the available DEC/HEX/BIN modifiers (as you are).

    The code, as it runs, will then interpret ASCII characters and convert to an actual number so that the ASCII string '2B' becomes the decimal value 43. There's no need to use an array in the HSERIN statement because you're not storing the '2' and the 'B', you're storing the decimal value 43 that the ASCII string '2B' represents.

    Then, when you do the LCDOUT you, again, use the DEC (or HEX or BIN) modifier to convert the decimal value 43 back into the ASCII characters '4' and '3' which are displayed on the LCD.

    Since your LCD shows 43 when you send '41 05 2B' everything is working exactly as expected and when you do decvalue = mai you'll copy the content of mai to decvalue so it TOO contains 43.

    I don't get what the problem is......

    /Henrik.
    Hi Henrik, yes right the LCD shows the value in dec that is what I want, but next in the code I want to do some math or comparsion with decimal value of this number.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    Did you find this post helpful? Yes | No

    Default Re: Hex values to decimal

    And what's stopping you?
    Either I don't get it or you don't :-) No offense.

    Code:
    mai VAR BYTE
    value VAR BYTE
    
    mai = $2B   'assign value to variable, in this case the value 43 represented as HEX.
    
    mai = mai + 10
    LCDOUT $FE, 1, DEC mai   ' will show 53
    PAUSE 2000
    
    value = mai * 4
    LCDOUT $FE, 1, DEC value   ' will show 212
    PAUSE 2000
    
    value = value / 5
    LCDOUT $FE, 1, DEC value    ' will show 42
    PAUSE 2000
    
    IF mai < $3A THEN
      LCDOUT $FE, 1, "mai is smaller than 58"
    ENDIF
    PAUSE 2000
    
    IF mai > value THEN
      LCDOUT $FE, 1, "Yes, mai is bigger than value"
    ENDIF
    PAUSE 2000
    
    LCDOUT $FE, 1, DEC mai, "  ", HEX mai
    PAUSE 2000
    Show us the math or comparison you're wanting to do and tell us wha the problem you're having actually is.

    /Henrik.
    Last edited by HenrikOlsson; - 3rd January 2017 at 19:53.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Hex values to decimal

    ahaaa, I thought that in PBP not work and I don't try this all the time. Now it's
    more clear. Thanks guys for the patience with me, I'm just an old beginner (63) and I try to learn about these (new for me) things.

Similar Threads

  1. Hex to Decimal to Binary
    By Ramius in forum FAQ - Frequently Asked Questions
    Replies: 3
    Last Post: - 28th March 2014, 01:01
  2. Decimal PWM Cycle values?
    By RossWaddell in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th April 2012, 12:58
  3. Hex to Ascii to Decimal
    By khoog in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 31st July 2009, 05:27
  4. Convert Hex to Decimal
    By RONKON in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 9th May 2006, 01:01
  5. Decimal values from string
    By GioppY in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th November 2005, 21:10

Members who have read this thread : 0

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