Convert khz value from CPU into mhz or ghz

我怕爱的太早我们不能终老 提交于 2019-12-13 07:38:16

问题


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;


}

This return gives me a string, f.e. 192000 (khz). But I want a mhz value..

With:

int mhz = Integer.parseInt(result)

the app closes itself..

How can I convert the khz value into mhz?

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..

Could you help me please?

There's something wrong with the result value I think.

Thank you


回答1:


Before converting that numbrer to int, remove all non numeric characters using something like this:

int khz = result.replace("[^0-9]+", "");

Then conver khz to number and divide it by 1000.



来源:https://stackoverflow.com/questions/13540383/convert-khz-value-from-cpu-into-mhz-or-ghz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!