Take Xor between Hexadecimal and binary

4 vues (au cours des 30 derniers jours)
sami ullah
sami ullah le 28 Sep 2020
Commenté : sami ullah le 28 Sep 2020
For example, I have the following data A= 1x6 char:
1caffe
And another stream of bits B=111111110000101011110111
I ahve to take bitxor between A and B?

Réponses (1)

Walter Roberson
Walter Roberson le 28 Sep 2020
Provided that you do not have more than 64 bits, the easiest approach is to convert them both to decimal, use bitxor() with appropriate assumed type, and then convert the result to whichever representation you need.
If you do have more than 64 bits, then the easiest approach is probably to use a lookup table to convert the hex to binary, pad the shorter one with 0 on whichever edges is appropriate, and then use a lookup table to compute the xor at the bit level. Afterwards convert to whatever representation you want out. Just be sure to define how you want to handle leading zeros in the result.
I suggest a lookup table to compute the xor because that permits you to use the same code flow to work with either numeric representation of bits or else character representation of bits. If you were using numeric representation of bits then xor can also be implemented as the ~= operator, giving a logical result that is equivalent to numeric for a lot of purposes and easy to convert to numeric if not. Whereas if you are using character representation then if you were to use ~= then because the result would be logical even if the inputs where character, then you would need to convert back to character representation of the bits -- which is not difficult at all, but is not the most obvious for people who do not well experienced in programming.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by