This command assumes that the hex represents unsigned value:
set q 0xffffb764
set r [expr $q]
puts $r
Is there a simple way to make it treat q as representing a signed value rather than an unsigned value?
This is my solution:
proc hex_to_signed {value} {
set sign [expr {($value & 0b10000000000000000000000000000000)}]
set mag [expr {($value & 0b01111111111111111111111111111111)}]
if {$sign==0} {
set exp 0
} else {
set exp [expr -2**31]
}
set value [expr {$exp + $mag}]
return $value
}
Another solution using format
set q 0xffffb764
set r [format "%d" $q]
puts $r
Related
How do you split a large hexadecimal number, say 0x111111112222222233333333, into a list of 32 bit numbers (0x11111111 0x22222222 0x33333333) using tcl?
Arithmetic in Tcl is based on the idea that you're dealing with numbers of arbitrary length (the implementation uses various internal representations, but you're supposed to not know too much about that). That means that we do the conversion arithmetically. Also, it helps to extract the numbers in reverse order, and if we know that the number is unsigned.
proc extractIntegers {number {bits 32}} {
set accumulator {}
set mask [expr {(1 << $bits) - 1}]
while {$number != 0} {
set value [expr {$number & $mask}]
set number [expr {$number >> $bits}]
lappend accumulator [format "%#x" $value]
}
return [lreverse $accumulator]
}
puts [extractIntegers 0x111111112222222233333333]
To handle the "infinite bits" case noted by Donal, we could make the proc recursive. At least then, the interpreter will eventually reach its max recursive depth and error out.
proc extractIntegersRecursive {number {bits 32} {mask 0}} {
if {$number == 0} return
if {$bits <= 0} {error "invalid bits"}
set mask [expr {$mask ? $mask : (1 << $bits) - 1}]
set procname [lindex [info level 0] 0]
return [concat \
[$procname [expr {$number >> $bits}] $bits $mask] \
[format %#x [expr {$number & $mask}]] \
]
}
This is a section of my app which is a calculator in Qt:
QString ss;
QTextStream (&ss) << expression(); // expression() return a double value
result_box -> setText(ss); // result_box is a lineEdit
When I type 10^6 and the function expression() returns that value, in result_box the scientific notation of it will be shown, 1e+06!
How to do to make the app show the result in decimal, 1000000 rather than that scientific notation?
The updated part:
I think I should write a code for it like this:
if( d is like an int number)
result_box -> setText(QString::number(d , 'f', 0));
else if ( d is a double number with n numbers after point)
result_box -> setText(QString::number(d , 'f', n));
You want to use 'f' instead of 'g' since 'g' takes the most concise format.
http://doc.qt.io/qt-5/qstring.html#argument-formats
QString ss;
QTextStream (&ss) << 1000000.0;
ui->lineEdit->setText( QString::number( ss.toDouble() , 'f') );
.toDouble( &ok ); // if you want to check if the conversion actually worked.
Precision is 6 by default, if you want something else you can set it, otherwise no need.
So in short:
result_box->setText( QString::number( expression( ) , 'f' ) );
will do the trick for you.
I'm having a problem getting a CTRL slice.
I'm trying to analyze OpenSSL by running this:
the code is like below
int dtls1_process_heartbeat(SSL *s)
{
unsigned char *p = &s->s3->rrec.data[0], *pl;
unsigned short hbtype;
unsigned int payload;
unsigned int padding = 16; /* Use minimum padding */
/* Read type and payload length first */
hbtype = *p++;
n2s(p, payload);
pl = p;
if (s->msg_callback)
s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
&s->s3->rrec.data[0], s->s3->rrec.length,
s, s->msg_callback_arg);
if (hbtype == TLS1_HB_REQUEST)
{
unsigned char *buffer, *bp;
int r;
/* Allocate memory for the response, size is 1 byte
* message type, plus 2 bytes payload length, plus
* payload, plus padding
*/
buffer = OPENSSL_malloc(1 + 2 + payload + padding);
bp = buffer;
/* Enter response type, length and copy payload */
*bp++ = TLS1_HB_RESPONSE;
s2n(payload, bp);
/*# slice pragma stmt; */
memcpy(bp, pl, payload);
bp += payload;
/* Random padding */
RAND_pseudo_bytes(bp, padding);
r = dtls1_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
if (r >= 0 && s->msg_callback)
s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
buffer, 3 + payload + padding,
s, s->msg_callback_arg);
OPENSSL_free(buffer);
if (r < 0)
return r;
}
else if (hbtype == TLS1_HB_RESPONSE)
{
unsigned int seq;
/* We only send sequence numbers (2 bytes unsigned int),
* and 16 random bytes, so we just try to read the
* sequence number */
n2s(pl, seq);
if (payload == 18 && seq == s->tlsext_hb_seq)
{
dtls1_stop_timer(s);
s->tlsext_hb_seq++;
s->tlsext_hb_pending = 0;
}
}
return 0;
}
`
frama-c ./ssl/d1_both.c -main dtls1_process_heartbeat -slice-calls memcpy -cpp-command "gcc -C -E -I ./include/ -I ./" -then-on 'Slicing export' -print
That produced nothing, so I then tried this: want to get a backforward slicing
frama-c ./ssl/d1_both.c -main dtls1_process_heartbeat -slice-pragma dtls1_process_heartbeat -cpp-command "gcc -C -E -I ./include/ -I ./" -then-on 'Slicing export' -print
But I still get nothing like that
void dtls1_process_heartbeat(void);
void dtls1_process_heartbeat(void)
{
return;
}
How can I get a slice like that?
function A (){
…
memcpy()
...
}
function B (){
…
…
...
}
function C (){
…
memcpy()
...
}
I want to capture everything to do with memcpy(), so I want to keep A and C, but not B.
How should I choose an entry point? How do I choose the pragma?
I hope I've stated my question clearly; it's had me confused for days.
First, notice that Frama-C Fluorine is an obsolete version. It has been released more than 3 years ago. Some slicing-related bugs have been fixed in the meantine. Please upgrade to a newer version, preferably Aluminium.
Second, the documentation for option -slicing-value is
select the result of left-values v1,...,vn at the
end of the function given as entry point (addresses are
evaluated at the beginning of the function given as entry
point)
It is unlikely to do what you want. Did you try option -slice-calls, more precisely -slice-calls memcpy ?
Also, keep in mind that B will be kept in the slice if it computes a value that is later used within a call to memcpy.
I have a problem with this function I created, the function should return the number of characters entered in the array. but always return 20 that its the limit of the array itself.
Code:
int LongitudCadena (char *pcadena) {
// cantidad its the counter of chars that in the array
int cantidad=0;
//M its a constant equals 20, that its the limit of the array
for(int a=0;a<M;a++){
if(pcadena!=0){
pcadena++;
cantidad++;
} else {
return 0;
}
}
return cantidad;
}
Replace if(pcadena!=0) by if(*pcadena!='\0').
Also, change the else condition to either
else
{
return cantidad;
}
or
else
{
break;
}
pcadena, the pointer, is never going to be 0 (NULL)... what you meant that the character it points to is '\0'
if (*pcadena)
Another problem is once you find the terminator, you return 0. You should return cantidad there.
Note: cantidad == a
There are several problems with the code. First of all, you should test the content of the address the pointer points at.
...
if(*pacadena!=0) {
....
Secondly, why do you return 0 in the while loop when pcadena is 0? Shouldn't you return the current length? Assuming your data always terminate with \0, then your for loop should look something like this:
for(int a=0;a<M;a++){
if(*pcadena){
pcadena++;
cantidad++;
} else {
return cantidad;
}
}
Further, if your data is indeed terminated by \0, then you should just use the strlen function instead. There's no need to rewrite this.
The function gets an integer and a digit, and should return true
if the digit appears an even number of times in the integer, or false if not.
For example:
If digit=1 and num=1125
the function should return true.
If digit=1 and num=1234
the function should return false.
bool isEven(int num, int dig)
{
bool even;
if (num < 10)
even = false;
else
{
even = isEven(num/10,dig);
This is what I've got so far, and I'm stuck...
This is homework so please don't write the answer but hint me and help me get to it by myself.
To set up recursion, you need to figure out two things:
The base case. What is are the easy cases that you can handle outright? For example, can you handle single-digit numbers easily?
The rule(s) that reduce all other cases towards the base case. For example, can you chop off the last digit and somehow transform the solution for the remaning partial number into the solution for the full number?
I can see from your code that you've made some progress on both of these points. However, both are incomplete. For one thing, you are never using the target digit in your code.
The expression num%10 will give you the last digit of a number, which should help.
Your base case is incorrect because a single digit can have an even number of matches (zero is an even number). Your recursive case also needs work because you need to invert the answer for each match.
This funtion isEven() takes a single integer and returns the true if the number of occurence of numberToCheck is even.
You can change the base as well as the numberToCheck which are defined globally.
#include <iostream>
using std::cout;
using std::endl;
// using 10 due to decimal [change it to respective base]
const int base = 10;
const int numberToCheck = 5;
//Checks if the number of occurence of "numberToCheck" are even or odd
bool isEven(int n)
{
if (n == 0)
return 1;
bool hasNumber = false;
int currentDigit = n % base;
n /= base;
if (currentDigit == numberToCheck)
hasNumber = true;
bool flag = isEven(n);
// XOR GATE
return ((!hasNumber) && (flag) || (hasNumber) && (!flag));
};
int main(void)
{
// This is the input to the funtion IsEven()
int n = 51515;
if (isEven(n))
cout << "Even";
else
cout << "Odd";
return 0;
}
Using XOR Logic to integrate all returns
// XOR GATE
return ((!hasNumber) && (flag) || (hasNumber) && (!flag));