HEX char -- > its comlement

  • Thread starter Thread starter pamelafluente
  • Start date Start date
P

pamelafluente

Hi
what is the smartest function that maps the char on the left to
the char on the right (I want to complement an hexadecimal string,
like making a negative color).

0 -> f
1 -> e
2 -> d
3 -> c
4 -> b
5 -> a
6 -> 9
7 -> 8
8 -> 7
9 -> 6
a or A -> 5
b or B -> 4
c or C -> 3
d or D -> 2
e or E -> 1
f or F -> 0

If there is something able to map in one shot a whole string
to its "complement" would be even better.

-Pam
 
0 -> f
1 -> e
2 -> d
3 -> c
4 -> b
5 -> a
6 -> 9
7 -> 8
8 -> 7
9 -> 6
a or A -> 5
b or B -> 4
c or C -> 3
d or D -> 2
e or E -> 1
f or F -> 0

If there is something able to map in one shot a whole string
to its "complement" would be even better.

One way is like this

Dim S As String = "0123456789ABCDEF"
Console.WriteLine(S)
For Each C As Char In S
Console.Write("{0:X}", (Not Integer.Parse(C.ToString(),
Globalization.NumberStyles.AllowHexSpecifier) And 15))
Next

if I understand what you want correctly.
 
Hi
what is the smartest function that maps the char on the left to
the char on the right (I want to complement an hexadecimal string,
like making a negative color).

0 -> f
1 -> e
2 -> d
3 -> c
4 -> b
5 -> a
6 -> 9
7 -> 8
8 -> 7
9 -> 6
a or A -> 5
b or B -> 4
c or C -> 3
d or D -> 2
e or E -> 1
f or F -> 0

If there is something able to map in one shot a whole string
to its "complement" would be even better.

You can use the Not operator (which complements the bits of a given
integer) or Xor the integer value with -1, whichever you prefer.
Anyway, you must convert back and forth, from hex to integer to hex
again...

Function NegHex(ByVal Hex As String) As String
'Converts the hex value to integer
Dim V As Int32 = Convert.ToInt32(Hex, 16)

'Defines a mask to the minimum number of chars expected
Dim S As Integer = Hex.Length
Dim Mask As String = String.Format("x{0}", S)

'Inverts the value and converts it back to hex, extracting just
'the number of chars presented in the original string
Return Right((V Xor -1).ToString(Mask), S)

'The above might as well be the following
'Return Right((Not V).ToString(Mask), S)

End Function

HTH.

Regards,

Branco.
 
Dim S As String = "0123456789ABCDEF"
Console.WriteLine(S)
For Each C As Char In S
Console.Write("{0:X}", (Not Integer.Parse(C.ToString(),
Globalization.NumberStyles.AllowHexSpecifier) And 15))
Next
Function NegHex(ByVal Hex As String) As String
'Converts the hex value to integer
Dim V As Int32 = Convert.ToInt32(Hex, 16)
....

Beautiful. Thanks you very much, Branco and Martin. Both these solution
look very smart. I like them.

-P
 
Back
Top