Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Converting a character array to single hex value

Reply
Thread Tools

Converting a character array to single hex value

 
 
nikNjegovan@gmail.com
Guest
Posts: n/a
 
      01-09-2006
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

 
Reply With Quote
 
 
 
 
nikNjegovan@gmail.com
Guest
Posts: n/a
 
      01-09-2006
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)

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      01-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i] != 0 && foo[i] != '.'; i++) {
if(isdigit((unsigned int)foo[i]) {
val = val*10 + foo[i] - '0';
} else {
/*ouch, bail out */
}

}

printf("%X",val);
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-09-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-09-2006
Nils O. Selåsdal wrote:
> (E-Mail Removed) 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.
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      01-09-2006
Michael Mair wrote:
> Nils O. Selåsdal wrote:
>> (E-Mail Removed) 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 ?
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-09-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-09-2006
(E-Mail Removed) 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/>
 
Reply With Quote
 
nikNjegovan
Guest
Posts: n/a
 
      01-10-2006
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.

 
Reply With Quote
 
nikNjegovan
Guest
Posts: n/a
 
      01-10-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UTF-8 Character Encodings and "NO-BREAK SPACE" (dec: 202, hex: CA)Character mrdecav@gmail.com HTML 7 02-01-2009 11:05 PM
hex string to hex value tim Python 8 11-23-2005 06:27 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments