2

I need to take an integer value and convert it so that when it is represented in hexadecimal, the digits are reversed. For instance:

Dim a As Integer = &H4321
Dim a_changedorder As Integer = ReverseHexDigits(a)
Console.Writeline(a_changedorder.ToString("X4")) ' Should output 1234

How can I implement a ReverseHexDigits method that works like that?

As a second example, &H4F2A should become &HA2F4.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • `dim a as Integer = 0x4321` will not compile because that is not how hex is designated in VB: `Dim a As Integer = &H4321` – Ňɏssa Pøngjǣrdenlarp Dec 01 '15 at 15:27
  • 1
    You have to perform these modifications by treating the variable as a string. To get the hexadecimal form of an integer (the string you would be modifying) you can use the function `Hex`. – varocarbas Dec 01 '15 at 15:38
  • 3
    `How can I change the order of the numbers` its a number, you'd actually be changing the *value*. when you assign `&H4321` look at the value of `a` it should be 17,185 in base 10 – Ňɏssa Pøngjǣrdenlarp Dec 01 '15 at 15:38
  • @Plutonix It might also be possible to change the order of the numbers in its original hexadecimal form. It would be a bit weird, although perhaps the OP wants to create some kind of encryption. – varocarbas Dec 01 '15 at 15:45
  • @varocarbas how I can treat a hexnumber like 4F2A as a string so I can rotate each value ? – VbQuestionGuy Dec 01 '15 at 15:47
  • @Plutonix yeah sure I can say its also 20266 in decimal ... now I need in dec 41716 so it will be A2F4 but that is senseless and would not solve my problem :) – VbQuestionGuy Dec 01 '15 at 15:50
  • As said: the function `Hex` delivers what you want. The integer value of &H4321 is 17185, but when you use it as argument to call this function it returns 4321. – varocarbas Dec 01 '15 at 15:50
  • @varocarbas I dont get it or you dont get want I want :( ... yes I know if I use hex(a) I wil get 4321 --- I want that "a" will be 1234 as Hex – VbQuestionGuy Dec 01 '15 at 16:43
  • So you want a method that will reverse the order of the nibbles in a two-byte integer? – Steven Doggart Dec 01 '15 at 16:46
  • @StevenDoggart Yes, example: hex "4F2A" (as binary "01001111 00101010") should be hex "A2F4" (as binary "10100010 11110100"). Thanks !!! – VbQuestionGuy Dec 01 '15 at 16:49
  • 1
    You are not being very clear. What are the rules? In your examples, you seem to be implying that you want to completely reverse all of the nibbles, but only the nibbles in the low word. Are the nibbles in the high word also reversed, but the words are kept in their place? Or is the high word left alone? Since you are having trouble explaining what you mean, perhaps understanding the reason why you need to do this would be helpful. – Steven Doggart Dec 01 '15 at 16:53

2 Answers2

4

While Matt's method will work, it is rather inefficient to convert the integer to a hexadecimal string, reverse the order of the characters in the string, and then parse it back into an integer again. If efficiency is important, and you only need to reverse the nibbles in a two-byte integer, then the following will work:

Public Function ReverseHexDigits(input As Integer) As Integer
    Return ((input And &HF) << 12) Or 
        ((input And &HF0) << 4) Or 
        ((input And &HF00) >> 4) Or 
        ((input And &HF000) >> 12)
End Function

However, that's confusing, since it only operates on the lower two-bytes. It would be more clear if it operated on UShort variables instead:

Public Function ReverseHexDigits(input As UShort) As UShort
    Return ((input And CUShort(&HF)) << 12) Or
        ((input And CUShort(&HF0)) << 4) Or
        ((input And CUShort(&HF00)) >> 4) Or
        ((input And CUShort(&HF000)) >> 12)
End Function
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
1

(I really can't understand why you would want to do this...but) Just convert the numeric value to a string, reverse it then convert back to a numeric value:

    Dim a As UShort = &H4321

    Dim hexCharArray As Char() = a.ToString("X4").ToCharArray
    Array.Reverse(hexCharArray)
    Dim hexStringReversed = New String(hexCharArray)

    Dim a_changedorder As UShort = Convert.ToUInt16(hexStringReversed, 16)

Confirm the output is correct:

Debug.WriteLine(a_changedorder.ToString("X4")) '1234

Note you should be using UShort as you have only two bytes, Integer is signed and is 4 bytes

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 1
    That is exactly what I was looking for and solved my problem! So perfect. My english is not very good and tried to explain it with this example. Thank you very much @Matt Wilko works like a champ! – VbQuestionGuy Dec 01 '15 at 16:56