简体   繁体   English

C-从字符串中获取CLEAN数字变量

[英]C - get CLEAN numeric variable from string

I have been fiddling with some C programming for a little LCD project. 我一直在为一个小的LCD项目进行一些C编程的摆弄。

I have a string, which executed in BASH (script), gives back a value from 0 - 100 (Volume level) the string is like this : 我有一个字符串,该字符串在BASH (脚本)中执行,并从0-100(音量级别)返回一个值,该字符串如下所示:

!/bin/bash !/ bin / bash

amixer get 'DIYINHK Clock Selector',0 | 混频器获得“ DIYINHK时钟选择器”,0 | awk '/[/ {getline; awk'/ [/ {getline; print $5}' | 打印$ 5}'| tr -d '[]%' tr -d'[]%'

I need this value returning, so that i can use it in a if/else statement eg I think the value is returned in a ascii way or something ? 我需要返回该值,以便我可以在if / else语句中使用它,例如,我认为该值以ascii方式或其他方式返回?

the printf output is right, eg. printf输出是正确的,例如。 when amixer is 98, the printf onscreen is 98 but on lcd, there is a kind of _ after the 98.... like this : 98_ its a "returning" value of some kind ? amixer为98时,屏幕上的printf为98,但在LCD上,在98之后有一个98_像这样: 98_它是某种“返回”值?

What i need, is a clean number which i can use as eg. 我需要的是一个干净的数字,我可以将其用作例如。 :

if (get_volume <= 30) 如果(get_volume <= 30)

statement; 声明;

else if (get_volume <=50 ) 否则(get_volume <= 50)

statement; 声明;

else if .... ....... 否则...........

here is a snippet : 这是一个片段:

FILE *fpipe; 文件* fpipe;

char *command = (char *)"/home/pi/lcd/get_volume.sh"; char * command =(char *)“ / home / pi / lcd / get_volume.sh”; // get_volume script // get_volume脚本

char line[256]; 字符行[256];

  if (! (fpipe = (FILE*)popen(command,"r")) ) { perror("PROBLEMS"); exit(1); } while ( fgets( line, sizeof line, fpipe)) 

lcdPosition(lcd, 0,0); lcdPosition(lcd,0,0);

lcdPrintf(lcd, line); lcdPrintf(lcd,line); // Print output on LCD //在LCD上打印输出

printf("\\nline = %s \\n", line); printf(“ \\ nline =%s \\ n”,line); // Print output on screen //在屏幕上打印输出

atoi function converts a string representing a number to an int. atoi函数将代表数字的字符串转换为int。 So to get a "clean number", you need something like: 因此,要获得一个“干净的数字”,您需要类似以下内容:

int volume = atoi(line);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM