In my custom dissector, I have 32 bit signed gint32 value in datagram, whose header field is described as
&hf_TargetPosition,
{ "Target Position", "machine.RxPdo",
FT_INT32, BASE_DEC, NULL, 0xffffffff,
NULL, HFILL }
Before adding this item to proto_tree, I need to scale it with double value.
Since there is no tvb_get* function that return signed gint32, I used tvb_get_letohl function to get 32 bit signed value
gint32 stmp32 = (gint32)tvb_get_letohl(tvb, suboffset);
gdouble tpos = (gdouble)stmp32 * 0.000001;
How can I add tpos to proto_tree ?
As a workaround I tried not to cast tpos to double and use proto_tree_add_int_format_value function
gint32 tpos = stmp/1000000;
proto_tree_add_int_format_value(Dout_tree, hf_TargetPosition, tvb, suboffset, 4, tpos, "%lf");
But could not get desired signed decimal-point/float value in displayed tree.
I need it to be raw integral value and display it as fractional unit.
Then you want
gint32 stmp32;
...
stmp32 = (gint32)tvb_get_letohl(tvb, suboffset);
proto_tree_add_int_format_value(Dout_tree, hf_TargetPosition, tvb, suboffset, 4, stmp32, "%lf", stmp32/1000000.0);
Related
In my custom dissector, i add gint16 value as
gint16 stmp16 = 0;
stmp16 = (gint16)tvb_get_letohs(tvb, suboffset);
proto_tree_add_int_format_value(Din_tree, hf_distanceValue, tvb, suboffset, 2, stmp16, "%lf", stmp16/100.0);
suboffset += 2;
It correctly displays the 16 bit signed gint16 value in datagram, whose header field is described as
&hf_distanceValue,
{ "Distance Value", "veh.in",
FT_INT16, BASE_DEC, NULL, 0x0,
NULL, HFILL }
},
However when I try to display 32 bit signed gint32 value, I get error
[Dissector bug, protocol CUSTOM: ..\build\epan\proto.c:4128: failed assertion "DISSECTOR_ASSERT_NOT_REACHED"]
Value is fetched in same manner using tvb_get_letohl() function,
gint32 stmp32 = 0;
stmp32 = (gint32)tvb_get_letohl(tvb, suboffset);
proto_tree_add_int_format_value(Din_tree, hf_speedValue, tvb, suboffset, 4, stmp32, "%lf", stmp32/1000000.0);
suboffset += 4;
&hf_speedValue,
{ "Speed Value", "veh.in",
FT_INT32, BASE_DEC, NULL, 0x0,
NULL, HFILL }
},
Assertion fails at proto_tree_add_int_format_value() in case value is of type gint32, it works fine in gint16 case.
I'm not sure if this is the problem or not, but you have 2 fields with the same filter name:
"Distance Value", "veh.in"
"Speed Value", "veh.in"
Normally, you would have something like, "veh.distance" and "veh.speed", assuming the abbreviation for your protocol is "veh". Maybe try changing this first to see if the problem is resolved?
In F# i'm using an external DLL (in this case SDL Graphics library) I'm importing the method I require as follows...
[<DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl)>]
extern int SDL_QueryTexture(nativeint texture, uint32& format, int& access, int& w, int& h)
This works fine and I can successfully call the method using the following...
let result = SDLDefs.SDL_QueryTexture(textTexture, &format, &access, &w, &h)
The problem is that the native SDL methods accept null values for many pointer arguments. This is required in some scenarios (which function like overloaded methods). I can't find any way to call these methods from F# passing nulls.
For example, this fails with "does not have null as proper value"
let result = SDLDefs.SDL_QueryTexture(textTexture, &format, null, &w, &h)
I read about the attribute [AllowNullLiteral] but it seems like I can only apply it to types I define, and not pre-defined types which are used in my imported DLL.
Is there any way I can do this?
If you want to specify nulls, you need to use "raw pointers", which are represented by types nativeint and nativeptr<T>.
[<DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl)>]
extern int SDL_QueryTexture(nativeint texture, uint32& format, nativeint access, int& w, int& h)
// Call without null
let access = 42
let pAccess = NativePtr.stackalloc<int> 1
NativePtr.write pAccess access
SQL_QueryTexture( textTexture, &format, NativePtr.toNativeInt pAccess, &w, &h )
let returnedAccess = NativePtr.read pAccess
// Call with null
SQL_QueryTexture( textTexture, &format, null, &w, &h )
NOTE: be careful with stackalloc. Allocating memory on the stack is quite handy, because you don't need to explicitly release it, but pointers to it will become invalid once you exit the current function. So you can only pass such pointers to an external function if you're sure that the function won't store the pointer and try to use it later.
If you need to pass a pointer to real heap memory that's not going anywhere, you'll need Marshal.AllocHGlobal. But don't forget to release! (or else :-)
let access = 42
let pAccess = Marshal.AllocHGlobal( sizeof<int> )
NativePtr.write (NativePtr.ofNativeInt pAccess) access
SQL_QueryTexture( textTexture, &format, pAccess, &w, &h )
Marshal.FreeHGlobal( pAccess )
I'm trying to modify an Order, but I always get Error #1.
From my research, I have discovered that error 1 means I have input parameter in a wrong way. How can I fix my OrderModify() function?
stoploss = NormalizeDouble(Ask - Point * TrailingStop,Digits);
int ticket;
takeprofit = NormalizeDouble(Ask + Point * TrailingStopTP,Digits);
double minstoplevel = MarketInfo( Symbol(), MODE_STOPLEVEL );
if(stoploss > NormalizeDouble(Ask - Point*minstoplevel,Digits)) {
stoploss = NormalizeDouble(Ask - Point*minstoplevel,Digits);
}
if(takeprofit < NormalizeDouble( Ask + Point*minstoplevel2, Digits )) {
takeprofit = NormalizeDouble( Ask + Point*minstoplevel2, Digits );
}
if(AccountFreeMarginCheck(Symbol(),OP_SELL,lotsize)>0) {
ticket=OrderSend(Symbol(),OP_BUY,lotsize,Ask, 0, 0.0, 0.0, "comment", MagicNumber, 0, Lime);
if(ticket<0) {
Print("Order send failed with error #",GetLastError());
} else {
Print("Order send sucesso!! Ticket#", ticket);
res=OrderModify(ticket,OrderOpenPrice(),stoploss,takeprofit,0,Blue);
if(res==false) {
Print("Error modifying order!, error#",GetLastError());
} else {
Print("Order modified successfully, res#", res);
}
}
} else {
Print("Sem dinheiro na conta D=");
}
}
Not exactly "wrong", OrderModify() legally sets _LastError == 1
There might be a bit surprise, but OrderModify() has an obligation to signal _LastError == 1 in case, the call was both syntactically and semantically correct, however, the values supplied for modification(s) were actually the very same, as the identified ticket# has already had in database.
This means, there was nothing to modify, as all the attributes already had the "quasi-new" target value(s).
One may pre-check all fields for a potential identity, which might allow our code to skip the OrderModify() call in this very case of an identity-of-{ current | target } values.
ERR_NO_RESULT == 1 // No error returned, but the result is unknown
GetLastError() - returns a last generated error-code. The same value is available via a system variable named _LastError. It's value can be reset before a critical activity to zero by calling ResetLastError().
Error codes are defined in stderror.mqh.
To print the error description you can use the ErrorDescription() function, defined in stdlib.mqh file
#include <stderror.mqh>
#include <stdlib.mqh>
The problem is that even though the entry price, stoploss, and take profit parameters to the OrderModify() call appear to be the same, they likely differ by a fraction of a unit ( smaller than "Digits" ).
To fix this,simply normalize the parameters to make sure they are a maximum of Digits decimal places long.
double entryPrice = NormalizeDouble( entryPrice, Digits );
double stoploss = NormalizeDouble( stoploss, Digits );
double target = NormalizeDouble( target, Digits );
Then pass them into the OrderModify() call.
CREATE PROCEDURE ADMINIST.STUDENT_CUSTOM_PROCEDURE1 (
IN p_id INTEGER,
IN p_maths INTEGER,
IN p_science INTEGER,
Out p_obtain_marks INTEGER,
out p_percentage decimal(3,2),
out p_status char(4)
)
P1: BEGIN
DECLARE p_total INTEGER;
SET p_total = 200;
SET p_obtain_marks = p_maths + p_science;
SET p_percentage = ((p_obtain_marks * 100)/p_total);
IF (p_percentage > 35) THEN
SET p_status = 'PASS';
ELSE
SET p_status = 'FAIL';
END IF;
insert into ADMINIST.STUDENT_RESULT values(p_id, p_maths, p_science, p_obtain_marks, p_percentage, p_status);
END P1
I got Error code:
SQLCODE=-304, SQLSTATE=22003
The DEC/DECIMAL data type is different than assumed. The data type information can be found in the DB2 manual under CREATE TABLE:
"DECIMAL(precision-integer, scale-integer) or DEC(precision-integer, scale-integer)
For a decimal number. The first integer is the precision of the number; that is, the total number of digits; it may range from 1 to 31. The second integer is the scale of the number; that is, the number of digits to the right of the decimal point; it may range from 0 to the precision of the number.
If precision and scale are not specified, the default values of 5,0 are used. The words NUMERIC and NUM can be used as synonyms for DECIMAL and DEC."
So in your case, to hold percentages, change the variable declaration:
out p_percentage decimal(5,2),
I got this answer by casting value into dacimal.
SET p_percentage = DECIMAL(((p_obtain_marks * 100)/p_total),3,2);
Thanks Henrik Loeser and Alex.
I am a student in high school and I am currently learning in Delphi XE3. We are learning about BIT manipulation. We have an assignment and while I have read a lot on the subject and understand the entire process of storing information in Bits and SHL/SHR I am having difficulty understanding how to do this process in Delphi.
The assignment is as follows:
Decimal Hexidecimal Binary
1 0x0001 0000000000000001
2 0x0002 0000000000000010
4 0x0004 0000000000000100
Passing an integer value in an XML file to identify the options set. For example. If I wanted to send option 1 and option 2, I would add 1+2=3. I would send 3 as the number to specify that options 1 and 2 are true.
On the client the binary value would be 0000000000000011 = 3
From what I have read I need to use a mask but I do not understand how to do this. How would do I use masks in Delphi ot obtain the individual values which would be True or False.
I tried doing this in a regular Integer variable but it always gets treated as an Integer and the result is very strange. If I convert the integer to a binary string representation and I iterate thru the characters the result is correct but I am assuming that I should not be doing this with strings. Any help or an example would be greatly appreciated. Thank you.
You usually check if a particular bit is set in a Integer variable using the and binary operator, and you set individual bits using the or operator, like this:
const
OPTION_X = $01;
OPTION_Y = $02;
OPTION_Z = $04;
var
Options: Byte;
begin
Options := OPTION_X or OPTION_Y; //actually 3, like in your example
//check if option_X is set
if (Options and OPTION_X) = OPTION_X then
ShowMessage('Option X is set'); //this message is shown, because the bit is set
//check if option_Z is set
if (Options and OPTION_Z) = OPTION_Z then
ShowMessage('Option Z is set'); //this message is NOT shown
end;
The different OPTION_ constants, are actually masks, in the sense they are used to mask bits to zero (to check if a particular bit is set) or to mask bits to 1 (to set a particular bit).
Consider this fragment:
begin
..
if cbOptionX.Checked then
Options := Options or OPTION_X;
..
the or will mask the first bit to 1. If we start with a Options value (in binary) of 01010000, the resulting Options would be 01010001
01010000
OR 00000001 //OPTION_X
= 01010001
the same value is used to mask all the other bits to 0 to check if a particular bit is set. The if condition, for example: (Options and OPTION_Z) = OPTION_Z, does this:
first it MASKS all the non-interesting bytes of the Option variable to 0. If we consider the last value of 01010001, the operation will result in clearing all the bits, but the first.
01010001
AND 00000001
= 00000001
considering a starting value of 01010000 it will return zero:
01010000
AND 00000001
= 00000000
next, it compares if that value is equal to the mask itself. If it is equal, the bit was set in the original Options variable, otherwise it was not set. If your mask contains only one bit, that's matter of taste, you can just check if the resulting value is, for example, different than 0, but if your mask contains multiple bits and you want to check if all the bits was set, you have to check for equality.
Delphi has a predefined type TIntegerSet which allows to use set operators. Assuming that options is an Integer, you can check if any bit (0-based) is set like this:
option1 := 0 in TIntegerSet(options); { Bit 0 is set? }
option3 := 2 in TIntegerSet(options); { Bit 2 is set? }
Changing the options is done via Include or Exclude:
Include(TIntegerSet(options), 0); { set bit 0 }
Exclude(TIntegerSet(options), 2); { reset bit 2 }
Of course you can use any other set operator that may be helpful.
Delphi has Bitwise Operators for manipulating individual bits of integer types. Look at the shl, shr, and, or, and xor operators. To combine bits, use the or operator. To test for bits, use the and operator. For example, assuming these constants:
const
Option1 = 0x0001;
Option2 = 0x0002;
Option3 = 0x0004;
The or operator looks at the bits of both input values and produces an output value that has a 1 bit in places where either input value has a 1 bit. So combining bits would look like this:
var
Value: Integer;
begin
Value := Option1 or Option2;
{
00000000000000000000000000000001 Option1
00000000000000000000000000000010 Option2
-------------------------------- OR
00000000000000000000000000000011 Result
}
...
end;
The and operator looks at the bits of both input values and produces an output value that has a 1 bit only in places where both input value have a 1 bit, otherwise it produces a 0 bit instead. So testing for bits would look like this:
var
Value: Integer;
Option1Set: Boolean;
Option2Set: Boolean;
Option3Set: Boolean;
begin
Value := 7; // Option1 or Option2 or Option3
Option1Set := (Value and Option1) = Option1;
{
00000000000000000000000000000111 Value
00000000000000000000000000000001 Option1
-------------------------------- AND
00000000000000000000000000000001 Result
}
Option2Set := (Value and Option2) = Option2;
{
00000000000000000000000000000111 Value
00000000000000000000000000000010 Option2
-------------------------------- AND
00000000000000000000000000000010 Result
}
Option3Set := (Value and Option3) = Option3;
{
00000000000000000000000000000111 Value
00000000000000000000000000000100 Option3
-------------------------------- AND
00000000000000000000000000000100 Result
}
...
end;