473,659 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8535
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*********@gma il.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",v al);
}

or perhaps

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

}

printf("%X",val );
Jan 9 '06 #3
ni*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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.embed ded) for posts from me with the phrase "txtinput.c ".

--
"If you want to post a followup via groups.google.c om, 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

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

Similar topics

22
5478
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 codecvt, in Josuttis' "The Standard C++ Library", did not help, and I'm still awaiting delivery of Langer's "Standard C++ IOStreams and Locales". Thanks,
3
10313
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 character which is represented by the number( in this example, it would be the hardcoded character © the copyright character) for output.
4
1685
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 teh data from the 4D array: Dim Array2D(Array4D.Length - 1) As Single
4
412
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
12917
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 the end? I haven't been able to do it so far. Is there a string function I can use? Can anyone help? Charles L
2
3539
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 entered. I know what the Ascii value of each letter, so i understood that i can transfer my string to BYTE and enter the ascii value by myself. if someone has a better idea, i'll be happy to hear about it. how can i know the Unicode value of a...
6
28100
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 places. So far, I cannot find anything that works. My application gets a string of characters from an external device via the serial port. I can use the substring method to get just one character from that input string, and I need to be able to convert...
16
5440
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 learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
15
9653
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 problem with strtol() is that a number that fits into a long might be too big for an int. sscanf() doesn't seem to do the over/underflow checking. atoi(), of course, doesn't do any checking. I've long thought it odd that there aren't strtoi() and...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.