从CPU转换kHz的值转换成MHz或GHz的 [英] Convert khz value from CPU into mhz or ghz
问题描述
我想下面的函数的字符串(结果)转换成一个整数值,除以1000就(千赫 - >兆赫)
I want to convert the String (result) of the following function into an Integer value and divide it by 1000 (khz -> mhz)
private String ReadCPUMhz()
{
ProcessBuilder cmd;
String result="";
try{
String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"};
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1)
{
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
result = result.replace("\n", "");
result = result.replace(" ", "");
return result;
}
这回给我一个字符串,F.E. 192000(千赫)。
但我想以MHz为单位。
This return gives me a string, f.e. 192000 (khz). But I want a mhz value..
使用:
int mhz = Integer.parseInt(result)
应用程序关闭本身。
the app closes itself..
我怎么能转换kHz的值转换成MHz的?
How can I convert the khz value into mhz?
如果没有.replace线我得到了一个错误。 logcat中说:无效INT:192000,这个数字后,有很多的特殊字符,如钻石。
Without the .replace lines I got an error. Logcat says: " invalid int: "192000 " and after this number there are a lot of special characters like diamonds..
所以我不知道我怎么能得到正确的值。
So I do not know how I could get the right value..
你能帮助我吗?
有一些错误的结果值,我认为。
There's something wrong with the result value I think.
感谢您
推荐答案
转换的numbrer为int之前,使用这样的事情删除所有非数字字符:
Before converting that numbrer to int, remove all non numeric characters using something like this:
int khz = result.replace("[^0-9]+", "");
然后CONVER kHz至数,除以1000吧。
Then conver khz to number and divide it by 1000.
这篇关于从CPU转换kHz的值转换成MHz或GHz的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!