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

Home Posts Topics Members FAQ

Calculate XOR of array of bytes

63 New Member
Hi a,

I'm writing a C function to calculate the XOR of all members in an array of bytes. Each member should be a 512-byte block.

Here is what i'm figuring how to write this function.

the parameter includes the input array of 512-byte blocks
the return result should be an string (char* or char[512])

i've started the code as below:

Expand|Select|Wrap|Line Numbers
  1. char* calXOR(char* arr[512]) {
  2.     char* xr;
  3.  
  4.     if (sizeof(arr) < 2) { // if there is only one segment payload
  5.         xr = arr[0];
  6.     }
  7.     else {
  8.         if (sizeof(arr) == 2) { // if there are payload from two segments
  9.             xr = arr[0] ^ arr[1];
  10.         }
  11.         else {
  12.             if (sizeof(arr) > 2) { // if there are payload from more than 2 segments
  13.                 xr = arr[0] ^ arr[1];
  14.                 int i;
  15.                 for (i = 2; i < sizeof(arr); i ++) {
  16.                     xr = xr ^ arr[i];
  17.                 }
  18.             }
  19.         }
  20.     }
  21.  
  22.     return xr;
  23.  
  24. }
  25.  
  26.  
  27.  
Hmm, the code is not working as I write it down but i'm trying to figure it out because i am not so familiar with handling data type and size in C.

Would be really grateful if somebody can fix these.

Thank you very much!
Nov 2 '13 #1
6 16530
donbock
2,426 Recognized Expert Top Contributor
You define parameter arr as an array of 512 char pointers. However, C doesn't work that way. The compiler silently throws away the dimension (512). In truth, parameter arr is a pointer to a char pointer. Thus, sizeof(arr) is the size of a pointer; and bears no relation to the number of elements in the array.

The typical approach is to pass two parameters: one that points to the beginning of the array; and another that specifies the number of elements in the array. The caller may be able to use sizeof to compute the value of the second parameter.
Nov 4 '13 #2
donbock
2,426 Recognized Expert Top Contributor
Lines 5, 9, 13, and 16 exclusive-or the array elements together. The array elements are pointers. Did you want to exclusive-or pointers or the chars that are being pointed at?
Nov 4 '13 #3
Corsari
2 New Member
I was seeking for the same question,
unfortunately I can't read a real answer.
Is it possible for you to share the fastest and efficient way to check the XOR result of all the bytes in an array?
The array would be filled with ASCII HEX values.
Thank you
Cor
Nov 30 '14 #4
donbock
2,426 Recognized Expert Top Contributor
Do you want to XOR the hex-ASCII characters or the binary values represented by those characters?

For example, consider array 'A', '1', 'b', '0':
XOR of the characters is 0x41 XOR 0x31 XOR 0x62 XOR 0x30 = 0x22.
XOR of nibble values represented by characters = 0xA XOR 0x1 XOR 0xB XOR 0x0 = 0x0.

What are you trying to do?
Dec 1 '14 #5
Corsari
2 New Member
Hello Don! Thank you for the interest

I'm going to process a data flow which will be get from an array buffer.

The first exercise is to count how many chunks/buffers/arrays are "blank" so which the content are all x00 bytes.

So I was wondering:
instead than running a "for" or a "while" cycle to compare every single byte in that array, it could be faster to instant XOR the buffer content and check if the result is 0.

Or the question could turn to:
Which could it be the faster and less cycles consuming process to verify that an binary array content is all x00?

Thank you

Cor
Dec 1 '14 #6
donbock
2,426 Recognized Expert Top Contributor
The most reliable way is to compare each byte to 0. The XOR approach you suggest is not reliable -- there are many cases where it will give the wrong answer.

Many processors perform implicit comparison to zero very quickly; a good compiler will take advantage of that.
Dec 1 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bangalore | last post by:
Hi all, Plz clarify me, on the implementation of two or three dimensional array using overloaded operator. Thanks, in advance Bangalore
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
2
by: Jeroen Ceuppens | last post by:
I created new bitmap file, bitmapdata, now a want the pixeldata that are in the array bytes put into the bmpData, what should I change in the code unsafe? there is something wrong with p =...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
2
by: Raed Sawalha | last post by:
I have a Bitmap Object and I need to return it as array of bytes is it possible
4
by: Lee Crabtree | last post by:
I need to shift all of the values in a byte array by more than 8 bits, meaning that values should flow from one byte to another. Since I don't know in advance how many bits will be shifting, I...
8
by: piggy | last post by:
I am trying to convert an image to an byte array (tobytes() method) and from byte array back to an image (frombytes() method). The problem i have here is say the image size is 285 bytes but the...
13
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of...
1
by: sriprasanna | last post by:
Hello All, In our project we have to store PDF, word document files in DataBase(SQL SERVER 2005). We have planned to convert the file to array of bytes and store the binary file into...
26
by: Bert | last post by:
Hi, I'm unhappy: why doesn't this work? char enc; char temp; for(int i=0;i<10000;i++){ fscanf(in,"%s",&temp); if(temp=='#')break; else
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
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: 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 ...

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.