Converting a character array to single hex value

Discussion in 'C Programming' started by nikNjegovan@gmail.com, Jan 9, 2006.

  1. Guest

    So i have a tachometer that I can communicated with via UART which
    gives me a character array of ascii values in the following form:

    Standard ascii 7 characters including decimal point such that the array
    when printed would equal the rpm value ie

    2031.00 rpm = [ '2','0','3','1','.','0','0' ]

    I've been rummaging everywhere and can't find anything to tackle this.
    I would be extreemly greatful to anyone that can point me in the right
    direction or that has any code snippets in C that I can use.

    NN
    , Jan 9, 2006
    #1
    1. Advertising

  2. Guest

    so the idea is if the array is

    [ '2','0','3','1','.','0','0' ]

    then the output of the conversion would be

    07EF (I'm not interested in the decimal and it can be truncated)
    , Jan 9, 2006
    #2
    1. Advertising

  3. wrote:
    > so the idea is if the array is
    >
    > [ '2','0','3','1','.','0','0' ]
    >
    > then the output of the conversion would be
    >
    > 07EF (I'm not interested in the decimal and it can be truncated)

    If you can convert that to an integer, you can format it
    as you like using printf/sprintf.

    char foo[] = "2031.00";
    char *tmp;
    if((tmp = strchr(foo,'.')) != NULL) {
    int val;
    *tmp = 0;
    val = atoi(foo); /*use strtol and do better error checking*/
    printf("%X\n",val);
    }

    or perhaps

    char foo[] = "2031.00";
    int val = 0,i;
    for(i = 0; foo != 0 && foo != '.'; i++) {
    if(isdigit((unsigned int)foo) {
    val = val*10 + foo - '0';
    } else {
    /*ouch, bail out */
    }

    }

    printf("%X",val);
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Jan 9, 2006
    #3
  4. Michael Mair Guest

    wrote:
    > so the idea is if the array is
    >
    > [ '2','0','3','1','.','0','0' ]
    >
    > then the output of the conversion would be
    >
    > 07EF (I'm not interested in the decimal and it can be truncated)


    Please quote enough context even when replying to yourself.

    In addition, state enough information so that we know what
    you want.
    My guess is that you want to convert from an
    array 7 of char to a string containing a hex number.

    Make it two parts: Retrieving the number from the input
    and outputting it as you want.

    Getting the number:
    You can do the whole thing character for character by
    yourself.
    Or, if you can guarantee that you always have a '.', then
    you can use strtoul() to retrieve the number. However,
    I'd rather make the input a string to be on the safe side.
    Or, if you are sure about the number of characters, you
    can use sscanf(input, "%7lu", &num) or similar.

    Output:
    snprintf(), if available, sprintf() otherwise. Have a look
    at flags and fieldwidth in the documentation.
    Or roll your own.

    Show us your best shot and we can help you further.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Jan 9, 2006
    #4
  5. Michael Mair Guest

    Nils O. Selåsdal wrote:
    > wrote:
    >
    >> so the idea is if the array is
    >>
    >> [ '2','0','3','1','.','0','0' ]
    >>
    >> then the output of the conversion would be
    >>
    >> 07EF (I'm not interested in the decimal and it can be truncated)

    >
    > If you can convert that to an integer, you can format it
    > as you like using printf/sprintf.
    >
    > char foo[] = "2031.00";

    <snip>

    The OP's first post specifically wanted foo to be
    char foo[7] = {'2','0','3','1','.','0','0'};

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Jan 9, 2006
    #5
  6. Michael Mair wrote:
    > Nils O. Selåsdal wrote:
    >> wrote:
    >>
    >>> so the idea is if the array is
    >>>
    >>> [ '2','0','3','1','.','0','0' ]
    >>>
    >>> then the output of the conversion would be
    >>>
    >>> 07EF (I'm not interested in the decimal and it can be truncated)

    >>
    >> If you can convert that to an integer, you can format it
    >> as you like using printf/sprintf.
    >>
    >> char foo[] = "2031.00";

    > <snip>
    >
    > The OP's first post specifically wanted foo to be
    > char foo[7] = {'2','0','3','1','.','0','0'};


    I didn't see that requirement specifically set, and even if it was,
    why not add an extra element to the not-overly-large array and add a nul
    terminator after he read the characters from his uart for simplicity ?
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Jan 9, 2006
    #6
  7. Flash Gordon Guest

    wrote:
    > So i have a tachometer that I can communicated with via UART which
    > gives me a character array of ascii values in the following form:
    >
    > Standard ascii 7 characters including decimal point such that the array
    > when printed would equal the rpm value ie
    >
    > 2031.00 rpm = [ '2','0','3','1','.','0','0' ]
    >
    > I've been rummaging everywhere and can't find anything to tackle this.
    > I would be extreemly greatful to anyone that can point me in the right
    > direction or that has any code snippets in C that I can use.


    Look up the strto functions, such as strtod or, alternatively, sscanf.
    Don't forget to ensure the string is null terminated though, since all
    the C string functions rely on null termination.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Jan 9, 2006
    #7
  8. Chuck F. Guest

    wrote:
    >
    > So i have a tachometer that I can communicated with via UART
    > which gives me a character array of ascii values in the
    > following form:
    >
    > Standard ascii 7 characters including decimal point such that
    > the array when printed would equal the rpm value ie
    >
    > 2031.00 rpm = [ '2','0','3','1','.','0','0' ]
    >
    > I've been rummaging everywhere and can't find anything to tackle
    > this. I would be extreemly greatful to anyone that can point me
    > in the right direction or that has any code snippets in C that I
    > can use.


    The "UART" is actually mapped into a text file. Open that and read
    the appropriate data. You can either use the bulky and awkward
    scanf routines, or use some routines I published here for text
    input without buffering. Google search here (and possibly on
    comp.arch.embedded) for posts from me with the phrase "txtinput.c".

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Jan 9, 2006
    #8
  9. nikNjegovan Guest

    Thanks everyone. Great suggestions.

    However. I'm trying to avoid using any string functions being that the
    bulk of my code in other areas won't need it. The UART will be giving
    be an 8-bit value as each ascii character comes in. So I'm puting those
    values into a 7 element char array. That array is guarenteed to have a
    decimal though the index position of that decimal is not guarenteed, it
    is also guarenteed to have 7 elements. Once the array is full it will
    have a value such as

    [ '2','0','3','1','.','0','0' ]

    which then needs to be translated to 2, 8bit values

    [07 , EF] or similarly, a 2 element char array.

    Sorry if my OP was unclear.
    nikNjegovan, Jan 10, 2006
    #9
  10. nikNjegovan Guest

    Actually, its mapped to a register in the processor. I'm extracting
    each byte as it comes in and copying it to the array at each interrupt
    generated by the processor, so I don't think i can use those stream
    manipulation routines.
    nikNjegovan, Jan 10, 2006
    #10
  11. Flash Gordon Guest

    nikNjegovan wrote:
    > Thanks everyone. Great suggestions.
    >
    > However. I'm trying to avoid using any string functions being that the
    > bulk of my code in other areas won't need it. The UART will be giving
    > be an 8-bit value as each ascii character comes in. So I'm puting those
    > values into a 7 element char array. That array is guarenteed to have a
    > decimal though the index position of that decimal is not guarenteed, it
    > is also guarenteed to have 7 elements. Once the array is full it will
    > have a value such as
    >
    > [ '2','0','3','1','.','0','0' ]
    >
    > which then needs to be translated to 2, 8bit values
    >
    > [07 , EF] or similarly, a 2 element char array.
    >
    > Sorry if my OP was unclear.


    In that case, rolling your own might be simplest. Given that the C
    standard (and probably the character set specified for your serial
    interface) guarantee that the digits are in order, a simple loop
    multiplying by 10 and adding (character - '0') should do you. You might
    find the isdigit function of use as well.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Jan 10, 2006
    #11
  12. Flash Gordon Guest

    nikNjegovan wrote:
    > Actually, its mapped to a register in the processor.


    What is? Please provide context when posting, people might not have seen
    the post you are replying to or might not have easy access to it. See
    http://cfaj.freeshell.org/google/

    > I'm extracting
    > each byte as it comes in and copying it to the array at each interrupt
    > generated by the processor, so I don't think i can use those stream
    > manipulation routines.


    That does make using stream routines hard, but there are string
    equivalents of some of them, such as sscanf.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Jan 10, 2006
    #12
  13. nikNjegovan wrote:
    > Thanks everyone. Great suggestions.
    >
    > However. I'm trying to avoid using any string functions being that the
    > bulk of my code in other areas won't need it. The UART will be giving
    > be an 8-bit value as each ascii character comes in. So I'm puting those
    > values into a 7 element char array. That array is guarenteed to have a
    > decimal though the index position of that decimal is not guarenteed, it
    > is also guarenteed to have 7 elements. Once the array is full it will
    > have a value such as
    >
    > [ '2','0','3','1','.','0','0' ]
    >
    > which then needs to be translated to 2, 8bit values
    >
    > [07 , EF] or similarly, a 2 element char array.
    >
    > Sorry if my OP was unclear.

    Use C notation if you're talking about C arrays.

    If you have a

    char c[7] = {'2','0','3','1','.','0,'0'};

    And what to convert this according to what you say above, the boring
    way, first do proper validation so you know you got all digits, and
    only digits, then;

    unsigned char val[2];
    int i = (c[0] - '0') *1000 + (c[1]*100 - '0') +
    (c[2]*10 -'0' + (c[3] - '0');

    val[0] = (i&0xff00) >> 8;
    val[1] = i&0xff;

    Or did you want the ascii hex representation of them ?
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Jan 11, 2006
    #13
    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:
    10
    Views:
    6,144
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    450
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    576
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,801
  5. Replies:
    7
    Views:
    3,551
Loading...

Share This Page