473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting a character array to single hex value

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 '06 #1
12 8518
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 '06 #2
ni*********@gmail.com 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);
Jan 9 '06 #3
ni*********@gmail.com 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.
Jan 9 '06 #4
Nils O. Selåsdal wrote:
ni*********@gmail.com 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.
Jan 9 '06 #5
Michael Mair wrote:
Nils O. Selåsdal wrote:
ni*********@gmail.com 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 ?
Jan 9 '06 #6
ni*********@gmail.com 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.
Jan 9 '06 #7
ni*********@gmail.com 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/>
Jan 10 '06 #8
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.

Jan 10 '06 #9
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.

Jan 10 '06 #10
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.
Jan 10 '06 #11
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.
Jan 11 '06 #12
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 ?
Jan 11 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
3
by: Andrzej Jan Taramina | last post by:
I have a need to convert a number into the character that it represents. For example, if I have: <xsl:variable name="number" select="169"/> I want to be able to convert this to the...
4
by: elziko | last post by:
I have a four dimensional array which I am trying to flatten into a single dimension array: Dim Array4D(10,10,10,10) as Single I can create my one dimensional array to be big enough to contain...
4
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
6
by: davetelling | last post by:
I am a total newbie, trying to slog through the Visual C# Express application. I need to be able to convert a single ASCII character (can be anything from 0 to 255) to an int for use in other...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.