473,473 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HEX char -- > its comlement

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

Sep 9 '06 #1
3 1455

pa***********@libero.it wrote:
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.

Sep 9 '06 #2


pa***********@libero.it wrote:
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.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 9 '06 #3
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

Sep 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nicholas Parnell | last post by:
Hi! I have the problem where I cast a bit string of "10000000" to a byte, which i get -128; this is fine so far. However, when I take this byte and cast it to a char, I get a question mark('?')....
5
by: Marc Schellens | last post by:
I have: char* byteN = "BYTE"; char* byte_fun() { return type_fun< byteN>(); } char* fix_fun() {
3
by: silverburgh.meryl | last post by:
I have this linker error, and I would need some help in resolving it: I have put "#include <iostream>" in my .cpp. I am not sure why I can't link. It compiles fine. /usr/bin/ld:...
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
2
by: Zorro | last post by:
In header file istream there is an overload for every built-in type, except char. Is there a reason for this? GCC 3.3.4. is the compiler. Thanks.
0
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char...
6
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++)...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.