Solved

xor hex and decimal

Posted on 2004-04-01
10
3,811 Views
Last Modified: 2007-12-19
Hi there,

I need to write a small function that will xor a hex value with a decimal value.

can someone please help with a small example.

Thanks
0
Comment
Question by:morees
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10738661
Hi morees,

hex or decimal does not matter as long as you use the xor operator correctly

x = 56 ^ 0xE4; is perfectly valid though a bit unintuitive

Sunnycoder
0
 

Author Comment

by:morees
ID: 10738691
Thanks,

better explain this a bit more.

I need to read the char from a file, convert that char to hex an then xor it with an integer value read from a file.

My program boms out at the xor with invalid variant type conversion.
How can I cast the hex value to "1F" to 0x1F so that the xor will work.

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 10738717
morees,

you can cast a char to an int .... I am a bit confused now

you read a char 'A' (hex 41 )

char first = 'A';

you can xor it with an int as such

first ^ 22 is perfectly valid and legal ... there is no need for any conversion here

Sunnycoder
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10738791
> need to read the char from a file

Do you mean you have a text file with a text (ASCII) representation of a number in it?
0
 

Author Comment

by:morees
ID: 10738855
the one file is a binary file that was xored with a key and the key file just contains the key.

the key file look like this
141592653589793238462643383279502884197169......

a byte at position x in the source file must be xored with the byte at position x in the key file.

that is what I want to do
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:rstaveley
ID: 10738888
The key file looks like ASCII.

> 141592653589793238462643383279502884197169...

Is the 2nd byte '4' as in 0x34?
0
 

Author Comment

by:morees
ID: 10738935
i think it should be..
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10739067
Hi morees,
> the key file look like this
> 141592653589793238462643383279502884197169......

> a byte at position x in the source file must be xored with the byte at
> position x in the key file.

To understand you correctly: As the key is in hex-ascii the key file is twice as long as the encoded file, right?

For the hex->int conversion, you *could* use the strtol() function:

int hexbyte=strtol(hexstring,NULL,16);
(hexstring MUST be \0 at the third byte)

This won't be terribly efficient, though probably better by eons than a fscanf() solution. You could convert 4 bytes (or even 8 using strtoll) at once, but then you'll skid into endianess issues.

Recommendation: Use a lookup table. Here's a Perl program to create one:
my $i;
my @tab;
for $i (0 .. 0xff){
      my ($hi,$lo) = unpack("C2",sprintf "%02x",$i);
      my $arridx=($hi << 8) + $lo;
      $tab[$arridx]=$i;
      ($hi,$lo) = unpack("C2",sprintf "%02X",$i);
      $arridx=($hi << 8) + $lo;
      $tab[$arridx]=$i;
}

print "#define ERR -1\nshort lookup[65536]={";
for $i (0 .. 0xffff){
      print "\n\t" if ($i & 0xf) == 0;
      if(defined $tab[$i]){
            printf "0x%02x,",$tab[$i];
      } else {
            print  " ERR,";
      }
}
print "\n};\n";

The lookup logic is: "94" -> ASCII 0x3934 -> lookup[0x3934] -> 0x94
The nice side effect is that you'll have both upper & lower hex values and you can detect errors without major extra effort -> faaaast.

Cheers,
Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10739080
In case you ask "why is this table defined as short?": All 256 possible values of (unsigned) char are valid, so you need a larger data type to store the error condition.

Stefan
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10751583
Do something like the following:

#include <fstream>
#include <iostream>
using std::fstream;
using std::ios_base;
using std::cout;
using std::endl;


...
// open binary file
fstream f("my file.bin", ios_base::binary | ios_base::in);

unsigned short decimal_val = 12;
while (!f.eof())
{
    char c;
    // read char
    f.read(&c, 1);
    // do what you like, xor the value with our decimal and print it
    cout << (c ^ decimal_val) << endl;  
}



Stefan73, you assume morees knows perl ? ....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to programmatically differentiate between C and Java 10 188
is twain_32.dll cmpatible with windows 10 ? 10 123
Which Linux flavors will this run on? 6 75
Issues with C++ Class 19 77
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now