Thứ Sáu, 11 tháng 4, 2014

Convert a Hex String to an Integer and Back Again on Android



Have you ever wanted to convert a hex string to an integer or convert an integer to a hex string? Java classes on Android makes this conversion a simple process. This Android tutorial shows you how.

Hex String to an Integer
In the Integer class, the parseInt() method provides a way to convert a string to int. In the parseInt() method, you can specify the radix value. For hex numbers, the radix would be 16.


String hex = "2A"; //The answer is 42
int intValue = Integer.parseInt(hex, 16);



Integer to Hex String

Now that you have converted it to an integer, lets now convert the int value back to a hex string. Again, Java makes this extremely easy.


String hex = Integer.toHexString(42);



Summary

Java classes on Android makes it easy to convert between integers and string representations of numbers. Using the additional methods of
toOctalString() or
toBinaryString(), you can also convert to additional number formats.



Source : codebeach[dot]com

Không có nhận xét nào:

Đăng nhận xét