D
Dave
I'm working on a program that will be parsing a protocol. My basic storage
element type is an array of characters or char*. The reason I am using
char* is because many of the socket and stream functions I'll be using take
char* as a parameter.
What I am seeing with my program is that when bit 7 is true, my integers are
being sign extended. Below is a program that should demonstrate my
problem. I am encountering this problem in a checksum loop that is adding
unsigned bytes together. As soon as bit7 is set my numbers become huge
because something is sign extending the high-byte. I think this may be a
processor related issue, but how can I overcome it.
I th
=============== Example
#include <iostream>
int main () {
// Define some constants
char ch[10] = { 0x01, 0x05, 0x07, 0x08, 0xFF,
0x0E, 0x0F, 0x10, 0x17, 0x18 };
// Observe sign extension
for (int i=0; i < 10; i++) {
std::cout << std::dec << "The value of ch[" << i << "] is "
<< std::hex << static_cast<unsigned short>(ch)
<< std::endl;
}
return 0;
}
============= a.out
The value of ch[0] is 1
The value of ch[1] is 5
The value of ch[2] is 7
The value of ch[3] is 8
The value of ch[4] is ffff
The value of ch[5] is e
The value of ch[6] is f
The value of ch[7] is 10
The value of ch[8] is 17
The value of ch[9] is 18
element type is an array of characters or char*. The reason I am using
char* is because many of the socket and stream functions I'll be using take
char* as a parameter.
What I am seeing with my program is that when bit 7 is true, my integers are
being sign extended. Below is a program that should demonstrate my
problem. I am encountering this problem in a checksum loop that is adding
unsigned bytes together. As soon as bit7 is set my numbers become huge
because something is sign extending the high-byte. I think this may be a
processor related issue, but how can I overcome it.
I th
=============== Example
#include <iostream>
int main () {
// Define some constants
char ch[10] = { 0x01, 0x05, 0x07, 0x08, 0xFF,
0x0E, 0x0F, 0x10, 0x17, 0x18 };
// Observe sign extension
for (int i=0; i < 10; i++) {
std::cout << std::dec << "The value of ch[" << i << "] is "
<< std::hex << static_cast<unsigned short>(ch)
<< std::endl;
}
return 0;
}
============= a.out
The value of ch[0] is 1
The value of ch[1] is 5
The value of ch[2] is 7
The value of ch[3] is 8
The value of ch[4] is ffff
The value of ch[5] is e
The value of ch[6] is f
The value of ch[7] is 10
The value of ch[8] is 17
The value of ch[9] is 18