在android中保存字符串的最后4个字符(Saving last 4 characters of a string in android)
今天早上很简单,但造成我的问题,似乎无法找到任何代码。 我创建了一个数字的十六进制表示,并希望现在保存十六进制字符串的最后4个字符,然后转换回一个整数。
转换的代码如下所示:
int in2 = new Integer(mycard.resourceid.toString()); String hex = Integer.toHexString(in2);如果有人能够朝着正确的方向推动我,我将非常感激。
int in3 = new Integer(hex.length()); int in4 = new Integer (in3 - 4); String mystring = hex.subString(in4, in3);Simple one this morning, but causing me issues and can't seem to find any code out there. I have created a hex representation of a number, and wish to now save the last 4 characters of the hex string to then convert back to an integer.
The code that converts is shown below:
int in2 = new Integer(mycard.resourceid.toString()); String hex = Integer.toHexString(in2);If someone could nudge me in the right direction I would be most appreciative.
int in3 = new Integer(hex.length()); int in4 = new Integer (in3 - 4); String mystring = hex.subString(in4, in3);最满意答案
如果您只想轻推一下,请查找String类的substring方法。
编辑:响应OP后续的一些示例代码:
public class Test { public static void main(String[] args) throws Exception { Integer in2 = 897387483; String hex = Integer.toHexString(in2); System.out.println(hex); System.out.println(hex.substring(hex.length()-4, hex.length())); } }If you just want a nudge, look for the substring method of the String class.
EDIT: Some sample code in response to OP followup:
public class Test { public static void main(String[] args) throws Exception { Integer in2 = 897387483; String hex = Integer.toHexString(in2); System.out.println(hex); System.out.println(hex.substring(hex.length()-4, hex.length())); } }更多推荐
发布评论