在android中保存字符串的最后4个字符(Saving last 4 characters of a string in android)

系统教程 行业动态 更新时间:2024-06-14 16:52:52
在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())); } }

更多推荐

本文发布于:2023-04-05 12:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4c045516f546e8fabe8ec987e2479dda.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   android   string   characters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!