简体   繁体   English

在java中将十进制数转换为ascii

[英]Converting decimal number into ascii in java

I want to break a decimal number and then convert it to ASCII format so that i can view it in text View. 我想打破一个十进制数,然后将其转换为ASCII格式,以便我可以在文本视图中查看它。

I am getting Battery percentage from a device as decimal.I cant display it directly by passing decimal number, but need to pass ASCII to the text View. 我从设备获得电池百分比为十进制。我不能通过传递十进制数直接显示它,但需要将ASCII传递给文本视图。

For Eg: I am getting data in a byte array; 对于Eg:我在字节数组中获取数据; consider arr[7] has value 98 which means battery is 98% I want to display the value 98 so i need to pass it as 0x39 and 0x38 respectively How can i break 98 into 0x39 and 0x38 respectively. 考虑arr [7]有值98,这意味着电池是98%我想显示值98所以我需要分别传递它为0x39和0x38我怎么能分别将98分为0x39和0x38。 I tried by doing bitwise ending with 0x0f and 0xf0 and then added 0x30 but it gave me wrong value. 我尝试通过按位结束0x0f和0xf0,然后添加0x30,但它给了我错误的值。

Use String.valueOf(byte) to get the string representation of the byte. 使用String.valueOf(byte)获取String.valueOf(byte)的字符串表示形式。

If you must have ASCII representation, you can use the toCharArray() method in String . 如果必须具有ASCII表示,则可以在String使用toCharArray()方法。

Try this: 尝试这个:

String hexValue = Integer.toHexString(Integer.parseInt("98"));

Then you can spilt it by " " 然后你可以通过“”溢出它

String[] splited = hexValue.split(" ");

Now you can get splited[0] = 0x39 and splited[1] = 0x38 现在你可以得到splited[0] = 0x39splited[1] = 0x38

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

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