Hey guys I have trouble converting this statement below in C to Golang. I tried debugging and I see that every 4th element in the char array is being modified and the statement is casting a char to an unsigned int so that the OR operation can take place.
I would like to know how to do this exact same operation in Golang. For the char arrays, I have decided to use uint8 arrays to store characters instead (if you know other data types that are better please let me know).
From my research, since I am planning to use the slice in Golang, I believe updating the slice headers is an option but I am not too sure how to do it.
Below is the C code and the picture shows the debugging of the char array (notice the difference in the 0 and 4th element)
#include <stdio.h>
int main()
{
unsigned char buf[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
unsigned char bb[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
unsigned int state[] = {45, 45, 33, 22, 55, 55, 90};
for(int i=0; i<7; i++)
{
((unsigned int*)buf)[i] = state[2] ^ ((unsigned int*)bb)[i];
}
return 0;
}
Below is the Golang code that I have tried (the compiler error is included as a comment)
package main
func main() {
bb := []uint8{1, 2, 3, 4, 5, 6, 7}
buf := []uint8{1, 44, 55, 66, 66, 65, 34}
state := []uint32{1, 66, 434, 234, 344, 434, 890}
for i:=0; i<7; i++ {
// ((unsigned int*)buf)[i] = state[2] ^ ((unsigned int*)bb)[i];
uint32(buf[i]) = state[2] ^ uint32((bb)[i])
// error: cannot assign to uint32(buf[i]) (value of type uint32)
}
}