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<uns igned short>(ch[i])
<< 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 4 3718
Dave wrote: 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<uns igned short>(ch[i])
probably not portable in 1's complement:
<< std::hex << (reinterpret_ca st<unsigned char *>(ch))[i]
or perhaps better :
<< std::hex << static_cast<uns igned char>(ch[i])
<< 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
* 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<uns igned short>(ch[i])
Here you should either cast to a signed integer type such as 'int'
(which may produce apparently negative values, but values which can be
safely casted back to 'char' or 'unsigned char' or 'signed char'), or
you should use 'unsigned char' in the first place, or you should first
cast to 'unsigned char'.
<< std::endl; } return 0; }
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
I managed to solve the problem in a way similar to this:
int main () {
char ch[10] = { 0x01, 0x05, 0x07, 0x08, 0xFF,
0xFE, 0x0F, 0x7F, 0x80, 0x18 };
unsigned char* puc;
// Pretend the char* is really an unsigned char*
puc = reinterpret_cas t<unsigned char*>(ch);
// Casting from a uchar to uint doesn't sign extend.
for (int i=0; i < 10; i++) {
std::cout << std::dec << "The value of puc[" << i << "] is "
<< std::hex << static_cast<uns igned int>(puc[i])
<< std::endl;
return 0;
}
but it seems highly non-portable - comments?.
The most portable way I can think of would be to manually calculate the
checksum in two character array. The problem I am working on is summing up
the bytes of a char* string to create a two byte checksum. But at present,
sign extension is messing things up.
If I use the 2 char array approach I could tackle the problem much like an
assembly language problem. I would look for a carry and increment the high
byte of the check sum. But the problem then becomes detecting the carry.
In assembly it is easy...just look for the 'C' flag to change. But how can
I do that in C++ efficiently. I don't know of any portable interface into
the processor registers. Is there a guru's trick using shifts or basic
logic operations?
* Dave: I managed to solve the problem in a way similar to this:
int main () { char ch[10] = { 0x01, 0x05, 0x07, 0x08, 0xFF, 0xFE, 0x0F, 0x7F, 0x80, 0x18 }; unsigned char* puc; // Pretend the char* is really an unsigned char* puc = reinterpret_cas t<unsigned char*>(ch);
The need for reinterpret_cas t (or a C cast) should be a a very large,
waving, flashing and noisily flapping red flag telling you that this
approach is UnGood (TM).
// Casting from a uchar to uint doesn't sign extend. for (int i=0; i < 10; i++) { std::cout << std::dec << "The value of puc[" << i << "] is " << std::hex << static_cast<uns igned int>(puc[i]) << std::endl; return 0; }
but it seems highly non-portable - comments?.
I pointed out three better solutions in a previous posting.
The most portable way I can think of would be to manually calculate the checksum in two character array. The problem I am working on is summing up the bytes of a char* string to create a two byte checksum. But at present, sign extension is messing things up.
Post code.
If I use the 2 char array approach I could tackle the problem much like an assembly language problem. I would look for a carry and increment the high byte of the check sum. But the problem then becomes detecting the carry. In assembly it is easy...just look for the 'C' flag to change. But how can I do that in C++ efficiently. I don't know of any portable interface into the processor registers. Is there a guru's trick using shifts or basic logic operations?
Gurus are not telepaths -- you should first describe what you're trying
to achieve (a bit of code would help).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jp Calderone |
last post by:
When using PyChecker on a project which uses bsddb, dozens of messages
like the following are reported:
warning: couldn't find real module for class
bsddb._db.DBAccessError (module name: bsddb._db)
I've looked through the PyChecker usage information, documentation, and
browsed some of the source, but I can't find any options for supressing
this.
|
by: tarmat |
last post by:
sorry for this silly little question, but whats the function to grab
the sign of a value?
|
by: Roy |
last post by:
Maybe I am in the wrong place and doing this the wrong way... please
tell me if you think there is a better way.
I need to transfer some files from a server to the client workstation.
The transfer has to be initiated from a page served up by the web
server. This is easy enough to do with Java Script and Windows FSO
but I have to loosen up the browser security and my (military)
employer will not allow that. So, is there some way I can...
|
by: Sona |
last post by:
Hi,
Could someone please explain what sign-extension means? If I have a hex
number 0x55, how does this get sign-extended? Can a sign-extended
counterpart be equal to -91? In a program I'm expecting 0x55 in return
from a function whereas I am getting -91 every time.. does this mean
anything? Thanks
Sona
|
by: Michael B Allen |
last post by:
Is there a standard method for supressing warnings regarding unused
parameters? I have a function that might be called hundreds of thousands
of times that looks like this:
const void *
idx_byte(const void *p, int idx, void *context)
{
return (unsigned char *)p + idx;
}
| |
by: Vadim |
last post by:
I want to make strong name for may apllication assemblies, but I don't know
where and how can I do it in Whidbey.
Help me please
|
by: Steve K. |
last post by:
I recall a few months ago coming across an article allowing for encoding
(or converting?) xml and html documents into sign language as well as
brail for deaf and blind people, and that they were pending an ISO
designations.
I cannot seem to find any information on this. From what I recall they
were supposed to be use able in <metacharset or so tags.
Thanks in advance for any help on this.
|
by: Terry Chapman |
last post by:
I have a 2003 Access .mda add-in which I can sign using my Thawte digital
certificate.
I am now migrating this add-in to Access 2007 and when I try to add my
Thawte digital signature Access 2007 says 'Cannot save the digital signature
at this time' and suggests that I 'package and sign' the file from the
Microsoft Office Button / Publish / Publish and Sign menu option. I am able
to do this but it saves my add-in as a signed package...
|
by: Ravikiran |
last post by:
Hi Friends,
I wanted know about whatt is ment by zero optimization and
sign optimization and its differences....
Thank you...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |