Byte array conversion
Hello,
I am trying to convert a variable array of bytes to an unsigned long int. The number of bytes is passed into the function as tcpoptlen. Manually converting the first type bytes in the array works fine, but trying to convert n bytes crashes. This worked to convert the first two bytes into a unsigned long integer. Code:
tcpoptdata = (opt[i+2] << 8) | opt[i+3]; Code:
u_int64_t tcpoptdata; |
how big is a unsigned long?
if its 64 bits: Code:
tcpoptdata = i just reread your post replace j with the number you want to convert Code:
tcpoptdata = ((j > 0)* |
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
Hello. What exactly does each byte represent? What is the type of opt? Why are you not using uint64_t, uint8_t (stdint.h)?
|
opt is an unsigned char(edit actually it looks like an array of chars). tcpopelen defines how many bytes are storing bitwise data. I need to take each byte as a field, and contantonate them together so the bits remain the same in integer form.
tcpoptlen = 2 so its two bytes or 16-bit the value is 65000 or byte[1] = 11111101, byte[2] = 11101000. I need to convert them back into an integer = 1111110111101000 At least I think thats how it work. I thought it would be easier to start with reading the into before I started trying to write to opt[]. |
I'm (still) guessing you're looking for something along these lines:
Code:
/* warning: the following convert function makes many gross assumptions. */ |
Well I am finally getting there. My test data is tcpoplen = 4, and opt[i+2] + opt[i+3] = the 16-bit binary value of 1460.
It works(meaning not crashing the system), but the data I get is 1461 not 1460 like it should be. So the problem is with my routine that converts the bytes into a single integer value. Code:
|
Fixed it! Had to add "& 0xFE" to this line. Not sure what that does, but it is now returning the correct result for any byte length tcp option.
Code:
tcpoptdata |= opt[i+bytefield]; Code:
|
Quote:
|
Quote:
so there is no reason it would not work but my sugjestion is if it works to fool with it |
All times are GMT -5. The time now is 09:22 AM. |