翻转十六进制字符串 [英] Flip a Hex String
问题描述
Acording在这里取得<一个一个的其他问题href=\"http://stackoverflow.com/questions/18829256/split-a-hex-string-without-spaces-and-flip-it\">Split一个十六进制字符串没有空格和翻转,我写这个新的问题更清楚这里。
Acording to a other question made here Split a Hex String without spaces and flip it, I write this new question more clearly here.
我有一个十六进制字符串是这样的:
I have an Hex String like this:
Hex_string = 2B00FFEC
我需要的是改变十六进制字符串的顺序从最新的字符开头,所以这会是这样:
What I need is to change the order of the Hex String to start from the latest characters, so this would be like this:
Fliped_hex_string = ECFF002B
在其他的问题,我问了这个方式使用.split()方法来实现。但应该有另一种方式以更好的方式来得到这个。
In the other question I asked a way to achieve this using the .split() method. But there should be another way to get this in a better way.
推荐答案
OP约束的字符长度正好在注释8字
一个纯数字的答案(从成语灵感转换字节序);要保存与字符串
A purely numeric answer (inspired from idioms to convert endianness); saves going to and from strings
N
是 INT
:
int m = ((n>>24)&0xff) | // byte 3 to byte 0
((n<<8)&0xff0000) | // byte 1 to byte 2
((n>>8)&0xff00) | // byte 2 to byte 1
((n<<24)&0xff000000); // byte 0 to byte 3
如果您需要将其转换为十六进制,使用
If you need to convert this to hexadecimal, use
String s = Integer.toHexString(m);
如果你需要从十六进制设置 N
,用
int n = (int)Long.parseLong(hex_string, 16);
其中, hex_string
是初始的字符串。你需要通过龙
解析器去允许底片。
where hex_string
is your initial string. You need to go via the Long
parser to allow for negatives.
这篇关于翻转十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!