Skip to content Skip to sidebar Skip to footer

Split A Hex String Without Spaces And Flip It

I need to split a string like this: String= 0C46BA09 I know booth the StringTokenizer and the String.split() methods, and I have used them to split strings with spaces. But in th

Solution 1:

You could do

char[] charArray = "0C46BA09".toCharArray();

Solution 2:

Using for each and toCharArray() method on String

for (Character c : s.toCharArray()) {
        System.out.println(c);
 }

O/P:

0
C
46BA09

Update:

String s = "2B00FFEC";
StringBuilder  result = new StringBuilder();
for (int i = 0; i <=s.length()-2; i=i+2) {
    result.append(new StringBuilder(s.substring(i,i+2)).reverse());
 }
System.out.println(result.reverse().toString());   //op :ECFF002B

Solution 3:

Strings="0C46BA09";
s.split("");

this should do the work. The result will be { "", "0", "C", "4", "6", "B", "A", "0", "9", "" }

Solution 4:

A purely numeric answer (inspired from idioms to convert endianness); saves going to and from strings

n is an int:

int m = ((n>>24)&0xff) |       // byte3 to byte0
        ((n<<8)&0xff0000) |    // byte1 to byte2
        ((n>>8)&0xff00) |      // byte2 to byte1
        ((n<<24)&0xff000000);  // byte0 to byte3

If you really need the hex,

Integer.toHexString(m);

Solution 5:

Why don't you use

public String substring (intstart, intend) 

method then?

YOURSTRING= YOURSTRING.substring(4,8) + YOURSTRING.substring(0,4);

Post a Comment for "Split A Hex String Without Spaces And Flip It"