Shop OBEX P1 Docs P2 Docs Learn Events
A quick or short way to convert $hex to Decimal — Parallax Forums

A quick or short way to convert $hex to Decimal

$WMc%$WMc% Posts: 1,884
edited 2008-11-20 00:24 in BASIC Stamp
·I'm haven A brain fart!!!

· I'm looking for A quick way to convert "$HEX" to A Decimal #, w/ the BS2p40·????...I know I've done this before,But I can't find were???? ...Tring to avoid All the Math...

Any Idea's ????.


_______________$WMc%___________________________??????






Post Edited ($WMc%) : 11/15/2008 5:32:07 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-15 05:36
    Given a character in the byte C and a value in V initialized to zero, you need a loop around:
    if C >= "0" and C <= "9" then
      V = V << 4 + C - "0"
    elseif C >= "A" and C <= "F" then
      V = V << 4 + C - "A" + 10
    elseif C >= "a" and C <= "f" then
      V = V << 4 + C - "a" + 10
    else
      goto notHex
    endif
    
  • $WMc%$WMc% Posts: 1,884
    edited 2008-11-15 06:54
    Mr.Green

    I Think You for Your Reply;
    The Code You wrote Is very clever and I'll send back the results!!!
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-20 00:24
    Are you certain any conversion is needed?

    If your $hex numbers represent ASCII values, such as $30 for the character "0", and so forth, then you may need to do a conversion. Same if you need to go the other way, taking $0A and changing it into $3130 or characters "10".

    If, on the other hand, you have $0A, for example, representing the number 10, it's already in the form you require. Decimal 10, hex $0A, binary %00001010, are human-readable ways of writing the very same byte of information, and no conversion is in order. Same for anything else that can possibly be in a byte variable. $FF is the same as decimal 255, for example, and represents just another way of writing it on the screen, or on paper, for humans to read. There isn't anything to convert.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
Sign In or Register to comment.