将字符数组转换为单个十六进制值 [英] Converting a character array to single hex value

查看:82
本文介绍了将字符数组转换为单个十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个可以通过UART与之通信的转速表

给我一个ascii值的字符数组,形式如下:


标准ascii 7个字符包括小数点,这样打印时数组

等于rpm值即


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


我已经在任何地方都在翻找,无法找到解决这个问题的任何事情。

对于任何可以指向我正确的方向或者有任何代码的人来说,我都会非常感激。 C中我可以使用的片段。


NN

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

推荐答案

所以想法是如果阵列是


[''''','0'',''3'',''1'',''''',''0 '',''0'']


然后转换的输出将是


07EF (我对小数不感兴趣,可以截断)

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)


ni ********* @ gmail.com 写道:
所以想法是如果数组是

[''''',''0'',''3'',''1'',''。'',''0'',''0'']
<然后转换的输出将是//EFF(我对小数不感兴趣并且它可以被截断)
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)



如果你可以将其转换为整数,您可以使用printf / sprintf将其格式化为




char foo [] =" 2031.00" ;;

char * tmp;

if((tmp = strchr(foo,''。''))!= NULL){

int val;

* tmp = 0;

val = atoi(foo); / *使用strtol并做更好的错误检查* /

printf("%X \ nn,val);

}


或者


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'';

}其他{

/ *哎哟,纾困* /

}


}


printf("%X", val);


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);


ni ******** *@gmail.com 写道:
所以想法是如果数组是

[''''','0'','' 3'',''1',''。'',''0'','0'']
然后转换的输出将是

07EF(我对小数并不感兴趣,可以截断)
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)




请引用足够的上下文回复自己。


另外,请说明足够的信息,以便我们知道你想要什么


我的猜测是你想要从一个char的数组转换为包含十六进制数字的字符串。


分为两部分:从输入中检索数字

并根据需要输出它。


获取数字:

你可以通过<为角色做整个角色扮演br />
你自己。

或者,如果你能保证你总是有''。'',那么

你可以用strtoul()来检索号码。但是,

我宁愿把输入字符串放在安全的一边。

或者,如果你确定字符的数量,你可以/>
可以使用sscanf(输入,"%7lu",& num)或类似的。


输出:

snprintf()如果可用,则sprintf()否则。在文档中看一下

的标志和fieldwidth。

或者自己滚动。


向我们展示你最好的镜头和我们可以为您提供进一步的帮助。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



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.


这篇关于将字符数组转换为单个十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆