I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way? 17 3462
spasmous wrote:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?
Just compare character by character.
while( data[j] != 'D' && data[j+1] != 'A') ) j++;
Don't forget to add checking for the end of the array!
--
Ian Collins.
On May 17, 1:13*pm, Ian Collins <ian-n...@hotmail.co mwrote:
spasmouswrote:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?
Just compare character by character.
while( data[j] != 'D' && data[j+1] != 'A') ) j++;
Don't forget to add checking for the end of the array!
Maybe I'm mixed up, but at least on my platform char is 8-bit and
short is 16-bit. So one data[j] is the same as two chars.
In comp.lang.c, spasmous wrote:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?
An implementation specific[1] (but still, IIRC, legal in standard C) way
would be to
while (data[j] != 'DA') ++j;
[1] ISO C 9899-1999 6.4.4.4. Point 10 (Semantics) says "The value of an
integer character constant containing more than one character ... is
implementation-defined." 'DA' is such a constant.
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
spasmous wrote:
On May 17, 1:13 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>spasmouswrot e:
>>I need a way to search through a block of memory for a char[2] array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0; while( data[j] != *((short*) "DA") ) j++; But this doesn't work. The char[2] obviously has an equivalent 16-bit value so how do I get that info in a simple way?
Just compare character by character.
while( data[j] != 'D' && data[j+1] != 'A') ) j++;
Don't forget to add checking for the end of the array!
Maybe I'm mixed up, but at least on my platform char is 8-bit and
short is 16-bit. So one data[j] is the same as two chars.
Oops, I didn't spot that data was short.
const char* p = (const char*)data;
const char* end = p+lengthOfData* 2;
while( p!= end && *p != 'D' && *(p+1) != 'A') ) p+=2;
--
Ian Collins.
On 17 May 2008 at 21:07, spasmous wrote:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work.
It obviously won't work if the occurence of DA in your data isn't
aligned at a 16-bit boundary:
short *x=(short *) "Hello DAMN you"; /* OK: x[3] is DA */
short *x=(short *) "Hello, DAMN you"; /* oh dear... */
The char[2] obviously has an equivalent 16-bit value so how do I get
that info in a simple way?
What you have, i.e. *((short*) "DA"), will do that just fine. You can
also do something like 'D'+('A' << 8), but that relies on your machine
being little endian...
In article <15************ *************** *******@t12g200 0prg.googlegrou ps.com>,
spasmous <sp******@gmail .comwrote:
>I need a way to search through a block of memory for a char[2] array "DA" using a pointer to a short. Ideally I would like to write something like:
>short *data = ... some data...; int j = 0; while( data[j] != *((short*) "DA") ) j++;
>But this doesn't work. The char[2] obviously has an equivalent 16-bit value so how do I get that info in a simple way?
Can the constant occur on an odd boundary? e.g., if the array had
XDAYDA
then would you want the match at character offset 1, or the one
at character offset 4?
If you need to find the ones on odd boundaries but were hoping to
compare characters two at a time instead of one at a time, then
you will not be able to do it simply by incrementing a byte at a time
but comparing two characters as a short, as many systems have alignment
requirements that do not allow shorts to be located on odd byte boundaries.
If you do want to search on odd boundaries, then here is a simple
algorithm that can make it more efficient:
unsigned char *datachar = (char *) data;
size_t j = 0, maxj = SIZE_OF_DATA_AR RAY;
while (j < maxj-1) {
if (datachar[j+1] == 'A') {
if (datachar[j] == 'D')
break;
} else if (datachar[j+1] == 'D') {
j++;
} else {
j += 2;
}
}
That is, if the next character ahead is not an 'A', there is no
point in checking the current character for 'D' -- either it
isn't a 'D' at all, or it isn't a "useful" 'D' because it isn't
followed by 'A'.
--
"The whole history of civilization is strewn with creeds and
institutions which were invaluable at first, and deadly
afterwards." -- Walter Bagehot
spasmous <sp******@gmail .comwrites:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?
It might be better to post a minimal example that illustrates the
way in which this does not work because, for certain values of
"work", the above code does do what you want.
The most portable solution will a string-based one. There is a good
chance it will also be much faster than you fear it will be (I suspect
you want to use a short * for fear of a slow character-based search).
For example:
char *found, *look = raw_data;
while ((found = strstr(look, "DA")) != NULL &&
(found - raw_data) % 2 == 1)
look = found + 1;
if (found)
/* Ah! there you are */
--
Ben.
Antoninus Twink <no****@nospam. invalidwrites:
On 17 May 2008 at 21:07, spasmous wrote:
>I need a way to search through a block of memory for a char[2] array "DA" using a pointer to a short. Ideally I would like to write something like:
short *data = ... some data...; int j = 0; while( data[j] != *((short*) "DA") ) j++;
But this doesn't work.
It obviously won't work if the occurence of DA in your data isn't
aligned at a 16-bit boundary:
short *x=(short *) "Hello DAMN you"; /* OK: x[3] is DA */
short *x=(short *) "Hello, DAMN you"; /* oh dear... */
Mr. "Twink" needs to re-tune his concept of what's "obvious".
The language doesn't require that char is 8 bits, that short is 16
bits, or that short has any particular alignment requirement. For
example, on some very popular platforms, accessing a 2-byte quantity
on an odd byte address works just fine (though it's slightly slower
than accessing something on an even address).
If by "won't work" he means that the behavior is undefined, he has a
point, but one possible, and in this case very plausible, consequence
of undefined behavior is that it "works".
>The char[2] obviously has an equivalent 16-bit value so how do I get that info in a simple way?
What you have, i.e. *((short*) "DA"), will do that just fine. You can
also do something like 'D'+('A' << 8), but that relies on your machine
being little endian...
There's no guarantee that the array associated with the string literal
"DA" is appropriately aligned for a short. It's likely to be
adequately aligned, but I wouldn't depend on it.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
spasmous <sp******@gmail .comwrites:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;
But this doesn't work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?
Consider treating the short array as an array of char and using
strstr().
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Danny Anderson |
last post by:
Hola!
I have an array of shorts that represents a stream of data. This data is
packed in tight, so certain elements may actually contain data for more
than one variable.
I am to decipher this stream. The first variable packed into this array
happens to be a short, meaning I get away with a simple assignment:
myFirstVariable=myArray;
|
by: Charles Kerekes |
last post by:
Hello everyone,
I am still learning C++, so I'm not sure what I'm trying to do is
possible. Her is a simplified example:
char Test = 'L';
cout << "Test Char as decimal: " << dec << Test << endl;
In the above cout statement, I was hoping it would show 76, the
decimal value of the L character. Instead, it displays an L. Is there
|
by: MrCoder |
last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!"
Ok heres my question for you lot.
Is there a faster way to compare 1 byte array to another?
This is my current code
// Check for a match
|
by: Martoon |
last post by:
I want to instantiate an STL map with my own compare function, and I
want to pass a parameter to the compare function that will be stored
and used for all comparisons in that map instance.
As an example, let's say the map key is char* strings, and I want the
comparison based on the nth character in the string. When the map is
instantiated, I need to somehow specify n. So I might have something
like this:
class MyClass
|
by: Steffen Loringer |
last post by:
Hi,
I'm using the following function to join 2 char (byte) into one short on
a 32 bit X86 platform:
unsigned short joinUnsigShort(unsigned char a,unsigned char b)
{
unsigned short val = 0;
val = a;
val <<= 8;
| |
by: arnuld |
last post by:
this programme runs without any trouble. it took 45 minutes of typing.
i posted it here so that people can save their precious time:
// Stroustrup special edition
// chapter 4 exercise 2
// printing the sizes of different types
|
by: yeti |
last post by:
Hi guys,
I am using custom string structures in a project.
typedef struct{
short int length;
char data;
}my_long_string;
and
|
by: sam.barker0 |
last post by:
Hi guys,
I am trying to form an IPV6 address string from the address bytes
contained in a unsigned char buffer
char tempstring;
sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short
*)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart()
+2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned
short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart()
+8))),htons(*((unsigned short...
|
by: Neel |
last post by:
Hi friends,
How can I store a short into an unsigned char???
its like
unsigned char msg;
//now I want to assign 0xAB into it.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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 we have to send another system
| |