•

Check out our Community Blogs

dbug

Member Since 09 Sep 2010
Offline Last Active Apr 28 2012 08:55 AM

#577342Happy Birthday CodeCall!

Posted by on 20 October 2010 - 12:41 AM

I'm quite new to this forum. I found it when I was tired of some other thematics in other forums and I decided to search google for a forum specific to programming. I don't remember if it was the first entry or not but I liked it, so I registered. I was also searching for an english forum to help me improve my knowledge of it (this is still a work in progress ). I like to investigate and find the source of the problems, and I also like to learn everything I can, so I enjoyed very much some of the questions I answered.

Now I'm a bit disconnected because I have a lot less time to spend here (current work is quite time demanding). Hope I can return soon.

Happy birthday
• 1

#574965Fastest way to flip order of bytes?

Posted by on 29 September 2010 - 04:05 AM

If you are using only 8 bits, maybe the best way is to create a lookup table (only 256 bytes long) with all precomputed values.

To do it programmatically, I think this is the best option (for 32 bits numbers):

```unsigned int swap(unsigned int value)
{
value = ((value & 0xFFFF0000) >> 16) | ((value & 0x0000FFFF) << 16);
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4);
value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2);
value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1);
return value;
}```

• 1

#574603syntax of structure definition not clear

Posted by on 26 September 2010 - 12:20 AM

Here there is a description of some interesting methods to initialize structures and arrays in gcc.
• -1

#574261two question

Posted by on 22 September 2010 - 11:47 PM

Any static declaration inside a class means that that declaration is global and visible to all objects of that class (or other classes if the declaration is public). If the declaration corresponds to a method, then this method can be called from the class itself (<class name>.<method>(<arguments>) ) instead of an object instance (<object>.<method>(<arguments>) ).

static keyword can be combined with any visibility keyword (<none>, private, protected and public). They define different things.

If no visibility keyword is specified, the value is visible to objects of all classes of the same package.
If private is specified, the value is only visible to objects of the class where it's defined.
If protected is specified, the value is visible to objects of the class where it's defined and its descendants.
If public is specified, the value is visible to objects of all classes.
• -1

#574203Swapping variables

Posted by on 22 September 2010 - 09:54 AM

If variables are integers (or integer like) you can swap the value of two variables with this code:

```#include <stdio.h>

int main(void)
{
int x = 10;
int y = 7;

printf("x before = %d\n", x);
printf("y before = %d\n", y);

x ^= y;
y ^= x;
x ^= y;

printf("x after = %d\n", x);
printf("y after = %d\n", y);

return 0;
}```

• 1

Posted by on 21 September 2010 - 05:05 AM

Checking if buffer[i] > 0 is not a valid condition to determine the end of the buffer.

I think this should work in this case:

```char buffer[10000];
char check1[] = {'c', 'h', 'e', 'e', 'z', 'b', 'u', 'r'};
int dataLength;
int p = 0;
int pos = 0;
int count = 0;
while ((dataLength = recv(Socket, buffer, sizeof(buffer), 0)) > 0)
{
int i = 0;
do
{
pos++;
if (buffer[i++] == check1[p])
{
if (++p >= sizeof(check1))    // assuming no null termination in check1
{
cout << "Match found. Current offset: " << pos << endl;
count++;
p = 0;
}
}
else
{
p = 0;
}
} while (--dataLength);
}

cout << count << " matches found" << endl;```
This code cannot detect words that appear just after a fragment of the same word. For example, if you search 'cheezbur' and the text contains 'chcheezbur', it won't detect it. I don't think this is necessary for this special case, however it can be done by adding some complexity.
• -1

Posted by on 20 September 2010 - 09:37 AM

When creating the FileStream object you should use FileShare.ReadWrite as the last parameter. This allows the logger to write to the file while you have it open.
• -1

#573879SQL date query...

Posted by on 20 September 2010 - 01:09 AM

If I'm not wrong, Access treats dates as numbers, so when you do the comparison it really is calculating 2010-09-02 = 1999, which corresponds to some date, but surely not what you want.

Also you can use this format to represent dates in Access:

`SELECT * FROM data WHERE datefield=#2010-09-02#`
I'm not sure if '-' is allowed. If it doesn't work, use '/' to separate year, month and day.
• -1

Posted by on 17 September 2010 - 11:04 PM

Here you can find some examples for various types of actions.

Basically you should specify that your new context-menu option is for anchors (setting 0x20 in Contexts value of the registry). And then you can access the selected link as shown (example taken from the previous web page):

```var aDestination = "http://www.google.com/search?q=cache:"
aLink contains the url you want.
• -1

#573705C# include file

Posted by on 17 September 2010 - 10:37 AM

You don't need to add any reference to other files. If both files are in the same namespace (and are compiled together), the compiler will find any needed declaration, whatever the file. If files are in different namespaces, then you must add a reference in one file to the namespace of the other file using the directive 'using <namespace>', but you never add references to specific files.
• 1

#573630Simple JavaScript Validator

Posted by on 16 September 2010 - 10:13 AM

There are some problems...

• IsNaN should be isNaN (first 'i' must be lowercase)
• innerText is not supported by some browsers. It's better to use innerHTML.
• The age check should only be done if the name is correct or don't change the value of isValid if it was false (actually, if you write a wrong name but a correct age, it will show the last message 'You entered ...')
With these changes it should work
• -1

#573609what is best programming language for relatively small applications??

Posted by on 16 September 2010 - 08:44 AM

A packet sniffer is a very low level application. I've never tried to do things like this in Java. I think that Java doesn't support raw sockets in the standard JRE, so this cannot be done (AFAIK). However there are some documents on internet referencing third party libraries that can supply raw sockets to Java. With one of these libraries you could build a packet sniffer in Java. However if the network trafic is high, there can be problems processing all the information in real time.
• -1

#573339Tree in Prolog

Posted by on 13 September 2010 - 11:04 PM

Prolog is basically a backtracker. It tries to find a match between your question and the rules you have defined. If a match is found it replies "true" (or what values are needed to satisfy the asked question).

If you ask max(1, 2, 3). it will return false because there are only two rules for max and both require that the third argument must be equal to the first one or second one. If some of the arguments of the question are not specified (variable names are used), then Prolog tries to find all possible values for that variables than can be derived from the defined rules. This can be seen as a return value in procedural programming. The third argument to max can be seen as the result of the operation that we are looking for.

For example, if we ask max(1, 2, R). Prolog will try to find a value for R that satisfies the rules defined. A rule has two parts separated by ':-'. Rules can be read like this: if the right part of the rule is true then the left part is assumed to be true.

In our case, we can try to apply the first rule: max(X, Y, X) :- X >= Y. We have specified the first and second arguments, so Prolog knows that X = 1 and Y = 2. But max(X, Y, X) can only be true if X >= Y is true. In this case this is false. We must apply the second rule: max(X, Y, Y) :- X < Y. Prolog knows that X = 1 and Y = 2. In this case X < Y is satisfied so the left part of the rule is valid. This means that max(1, 2, 2) is valid (substituting known variables into the rule). The last step is to find a match between our asked question and this true fact. max(1, 2, R) must match max(1, 2, 2). The only possibility to do this is that R = 2. This is exactly what Prolog says for our request.

We could also ask max(X, 1, 2). and he will answer X = 2. There are no input and output arguments in Prolog. It only tries to find matchings between questions made and rules available.
• -1

#573292Logic error in C hex parse program

Posted by on 13 September 2010 - 06:45 AM

At first sight there are two problems:

• for loop does not update the counter variable

`for ( i = 0; i < 400; i+2 )`
The correct way to update the variable i is:

`for ( i = 0; i < 400; i = i+2 )`
You need to assign the result of i+2 to somewhere or it is simply lost.

• the case when the parsed hex digit is a decimal number is not correct

The function hexparse takes a character. A character is a number that represents the ASCII code of a letter, digit or sign. The character '0' is not the same than the number 0. Character '0' has the ASCII code 48, so if you simply convert the char to an int, you get a bad result.

The correct way to convert a decimal digit from character to number is:

`y = x - '0'`
This way you substract the ASCII code of '0' from the digit you have. Example: if x = '4', its ASCII code is 52, so '4' - '0' is the same than 52 - 48 = 4, that is what you want.
It would also be recomendable to add some verifications to avoid errors. For example the user could write 'GG', that is an invalid hex number.
• -1

#573236how to avoid "out of memory" exception on phones with low heap?

Posted by on 12 September 2010 - 02:30 PM

In the second option you should reuse the inputStreamData array instead of reallocating it. Anyway this should not exhaust all the memory. Do you repeat this action many times or use more memory doing other things ? is it possible that some connections give you a lot more of 1500 characters ?

You could use the Runtime object to track the memory usage and determine where free memory drops drastically, or if it is a gradual loss.

```Runtime rt = Runtime.getRuntime();
rt.freeMemory(); // current free memory
rt.totalMemory(); // total memory available```
One thing you can do to free memory as soon as possible is to delete references to objects when they are not needed anymore. This allows the garbage collector to recover the used memory earlier. This can be important in long functions that create a lot of objects. If you don't remove the references, the objects cannot be freed until the function terminates and local variables are automatically destroyed.

For example, when you stablish the connection and read the received data, you can close it just after the reading and set this.connection = null to remove the reference to the object:

```this.connection = (HttpConnection) Connector.open(url,Connector.READ);
this.inputStreamData=new byte[(int)this.connection.getLength()];
this.connection.close();
this.dataInputStream = null;