Obtaining digits from bit string

Discussion in 'C Programming' started by zion_zii, Apr 15, 2006.

  1. zion_zii

    zion_zii Guest

    Im working on an embedded project and i have to obtain individual
    digits from a byte thats the output of a device. Im trying to output
    this info to an LCD and need to convert it to ASCII first. I have the
    following which only works for single digits but not double digits.
    (Data from chip is stored in current[] array and needs to be converted
    and stored in array hours[]).

    hours[0] = 0x30 | ((current[2] & 0x70) >> 4); //obtain the tens minute
    digit
    //current[2] stores the byte i want to convert,

    hours[1] = 0x30 | (current[2] & 0x0F) //obtain the ones minute digit.
    ;

    This only works if i have single digit output but i a value like 36
    minutes was read from the device, the output goes crazy. I appreciate
    any suggestions/ideas.

    zion.
    zion_zii, Apr 15, 2006
    #1
    1. Advertising

  2. zion_zii

    Jack Klein Guest

    On 14 Apr 2006 19:17:14 -0700, "zion_zii" <> wrote
    in comp.lang.c:

    > Im working on an embedded project and i have to obtain individual
    > digits from a byte thats the output of a device. Im trying to output
    > this info to an LCD and need to convert it to ASCII first. I have the
    > following which only works for single digits but not double digits.
    > (Data from chip is stored in current[] array and needs to be converted
    > and stored in array hours[]).
    >
    > hours[0] = 0x30 | ((current[2] & 0x70) >> 4); //obtain the tens minute
    > digit
    > //current[2] stores the byte i want to convert,
    >
    > hours[1] = 0x30 | (current[2] & 0x0F) //obtain the ones minute digit.
    > ;
    >
    > This only works if i have single digit output but i a value like 36
    > minutes was read from the device, the output goes crazy. I appreciate
    > any suggestions/ideas.
    >
    > zion.


    What are the types of "current" and "hours"? What are the values in
    "current" when you get what you consider correct output, and what is
    that correct output? What are the values in "current" when you get
    what you consider "crazy" output, and what is that output?

    Exactly how is the format of the data in "current" defined?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 15, 2006
    #2
    1. Advertising

  3. zion_zii

    zion_zii Guest

    I apologize for being so vague.

    current and hours are both int ( 8 - bit ints) arrays. The correct
    output appears when i have single digits. Prior to what is shown above,
    the data is gathered from a terminal and fed into a chip (ds1305),
    where a few minuites later, it is read from the same chip and stored in
    current[]. The correct output appears when i have something like 8
    minutes for example (actually, any single digit). When i have a double
    digit (> 9), it doesnt work. I should also mention that the most sign
    bit of the byte just read is immaterial and doesnt do anything.
    To display this value to the lcd, it has to be in ASCII (thats where
    the 0x30 comes in). So for single digits that are less than 10, i have
    no problem (ds1305 outputs in bcd). Its when i need to determine the
    10's digit that i fail. This is also happening with other bytes but if
    i get a soln for one i can change the others accordingly. Thanks Jack.
    zion_zii, Apr 15, 2006
    #3
  4. zion_zii

    Neil Guest

    zion_zii wrote:
    > I apologize for being so vague.
    >
    > current and hours are both int ( 8 - bit ints) arrays. The correct
    > output appears when i have single digits. Prior to what is shown above,
    > the data is gathered from a terminal and fed into a chip (ds1305),
    > where a few minuites later, it is read from the same chip and stored in
    > current[]. The correct output appears when i have something like 8
    > minutes for example (actually, any single digit). When i have a double
    > digit (> 9), it doesnt work. I should also mention that the most sign
    > bit of the byte just read is immaterial and doesnt do anything.
    > To display this value to the lcd, it has to be in ASCII (thats where
    > the 0x30 comes in). So for single digits that are less than 10, i have
    > no problem (ds1305 outputs in bcd). Its when i need to determine the
    > 10's digit that i fail. This is also happening with other bytes but if
    > i get a soln for one i can change the others accordingly. Thanks Jack.
    >


    The request way for sample data (since your code does not work)

    //Looks like packed BCD No?
    // 0x30 = '0' it reads better
    //current[2] stores the byte i want to convert,
    hours[0] = '0' + (current[2] >> 4); //obtain the tens minute digit
    hours[1] = '0' + (current[2] & 0x0F) //obtain the ones minute digit.
    Neil, Apr 15, 2006
    #4
  5. zion_zii

    zion_zii Guest

    Never mind guys. I figured it out. Just divide the num by 10 to get the
    tens and subtract the tens * 10 from the initial val to get the ones.
    Thanks though.

    zion.
    zion_zii, Apr 15, 2006
    #5
  6. zion_zii

    Thad Smith Guest

    zion_zii wrote:
    > Never mind guys. I figured it out. Just divide the num by 10 to get the
    > tens and subtract the tens * 10 from the initial val to get the ones.
    > Thanks though.


    Earlier you said that the data was obtained from a chip in BCD. Are you
    now saying that the number has already been converted to binary, which
    you didn't realize before, before your code converts it to ASCII
    decimal? Otherwise, your explanation doesn't make sense.

    --
    Thad
    Thad Smith, Apr 16, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,714
    Timothy Bendfelt
    Jan 19, 2007
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    710
    those who know me have no need of my name
    May 28, 2006
  3. Mohan
    Replies:
    3
    Views:
    348
    Robbie Hatley
    Jul 14, 2006
  4. Replies:
    9
    Views:
    938
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    162
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page