Hexadecimal numbers in reverse

Asked

Viewed 303 times

1

How to print the hexadecimal numbers typed by the user backwards? For example:

Entrada: 0x101c4701
Saída:   01471C10

If possible in Java and C++.

In reality I want to get the information contained within a Windows registry file that is like this:

"0000"=hex:0x001c2701
"0001"=hex:0x001d2701

I wanted a code that only took the 0x001c2701 and reversed to: 01271c00 the code you provided to me works,.

  • What is the ratio of the output to the input? It would be the decimal representation of this Hex literally contrary? I wonder why convert these Hex to decimal not even closely result in the same idea.

  • Yes, Bruno would be the Hex, in reverse.

  • I just asked to make it clear. So it’s just basic string handling, no conversion required.

  • Bruno, would take these Hex like this: 0x401c1701 0x011d1701 0x431e1701 0x991f1701 and invert them to look like this: 01171C40 01171D01 01171E43 01171F99, but I need to do this at once and not one at a time, I would ask the user to enter all, and return the inversion of all of them.

  • I noticed that during the acceptance you first accepted my answer and then Oeslei’s. I don’t know if you know that acceptance, unlike the vote, can only be made for one answer in each question. And acceptance is the last one you click. I don’t know if your intention was to actually choose Oeslei’s answer, which is a good one, I would have no problem accepting it, or choosing mine. No matter which one you choose, the decision is yours, but it is important to understand how the tool works and make a choice according to your real will.

  • @bigown, Actually the two answers were excellent as I am new here did not know this rule, thanks for clarifying I will pay attention next times. Thanks for the help!

  • @bigown, could explain to me how I could do this way Input:"0000"=Hex:40,1c,17,01 "0001"=Hex:01,1d,17,01 "0002"=Hex:43,1e,17,01 Output: <01171c40 0171d01 01171e43>.

  • It seems to me that this is another question.

  • @bigown, I just didn’t do it because I thought it would be a double question. since it is within the subject of the question already asked.

Show 4 more comments

2 answers

4

Essentially you can use the same solution for both.

int numero = 0x101c4701;
int invertido swapped = ((numero >> 24) & 0xff) | // move byte 3 p/ byte 0
                        ((numero << 8) & 0xff0000) | // move byte 1 p/ byte 2
                        ((numero >> 8) & 0xff00) | // move byte 2 p/ byte 1
                        ((numero << 24) & 0xff000000) // byte 0 p/ byte 3

This is the principle of inversion. Since you haven’t put in the code of what you’ve already done, I don’t know what else you might need.

See how to convert with the method Integer.toHexString() and the Long.parseLong().

If you want to reverse a string, would be something like that in Java:

String s = "101c4701";
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());

I put in the Github for future reference.

In accordance with that response in the OS.

And in C++:

std::string reverse_pairs(std::string const & src) {
    assert(src.size() % 2 == 0);
    std::string result;
    result.reserve(src.size());
    for (std::size_t i = src.size(); i != 0; i -= 2) {
        result.append(src, i - 2, 2);
    }
    return result;
}

I put in the Github for future reference.

In accordance with that response in the OS.

4


In Java you can do it this way:

String input = "0x101c4701";
String result = "";

for (int i = input.length(); i > 3; i -= 2) {
    result += input.substring((i - 2), i);
}

System.out.println(result);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.