Print variables in hexadecimal or decimal format - gdb

Currently, when I print the value of a variable v in GDB (print v) I get an integer.
Is it possible to have GDB print such integer variables in hexadecimal or binary?

Sure it is. Try these:
# Hexadecimal
p/x variable
# Binary
p/t variable
See output formats.

Related

How to make GDB format half-word memory as hex?

I'm working with an algorithm that uses uint16_t as the datatype. There are 4 half words in the array so I am trying to display the four half words in hex. I have not done anything other than x/4h:
(gdb) x/4h master_key
0x7fffffffd330: u"Āईᄐᤘ桷"
0x7fffffffd33c: u"桷敥\xbe0#"
0x7fffffffd346: u""
0x7fffffffd348: u"ꆋ翿"
According to the GDB | Memory:
f, the display format
The display format is one of the formats used by print (‘x’, ‘d’, ‘u’, ‘o’, ‘t’, ‘a’, ‘c’, ‘f’, ‘s’), and in addition ‘i’ (for machine
instructions). The default is ‘x’ (hexadecimal) initially. The default
changes each time you use either x or print.
I'm not sure why x is trying to print strings but I would like it to print the half words in hex.
GDB does not seem to be following the manual. I think I need to change the behavior of x and make it persistent. How do I tell GDB to print the half words in hex?
The following in in my .gdbinit but it looks like GDB is ignoring it (not a surprise).
(gdb) shell cat ~/.gdbinit
set output-radix 16
set history save on
set history size 256
set logging on
set logging overwrite on

Print float variable to IEEE format in Trace32

In my code I have the following variable that I want to display in my log
t_f32 tx_float_to_1x16_send_data[15];
In my cmm script I tried different ways to display my variable but trace32 seems to only manage hex, decimal and binary but not IEEE
PRINT VAR.VALUE(tx_float_to_1x16_send_data[0])
PRINT %Decimal VAR.VALUE(tx_float_to_1x16_send_data[0])
PRINT FORMAT.DECIMAL(8.,VAR.VALUE(tx_float_to_1x16_send_data[0]))
I only found one way to display IEEE but it works with an adress and I don't know how to apply it to a variable
PRINT DATA.FLOAT("IEEE",D:0x800B2C)
Note : when using VAR.VIEW in debug mode, Trace32 is perfectly capable to format the data as a float
Any help would be appreciated.
Thanks
Well, I guess this should work:
PRINT Data.Float("IEEE",Var.ADDRESS(tx_float_to_1x16_send_data[0]))

UDF decimal to binary

I wrote a decimal to binary converter function in order to practice my manipulation of number systems and arrays. I took the int a converted it to binary and stored each character, or so I beleive, in an array, then displayed to the screen, however it is displaying characters I do not know i looked them up on the aski table and do not recognize them, so i would like to ask for your assistance, here is a picture of the code, and console app.
Thanks in advance.
You likely want to insert number chars (such as '1') in your result, but you assign the char value. Try adding the value of '0' to get a readable result (remainder + '0').
If you interpret the result array as a string (that's what i suggested), you should also set the last char to the value 0 (not '0'!) to mark the end of the c string.
Your output function not correct output your binary text because:
1) cout output characters until '\0', so your function will correct output until get first 0 in binary representation of int (for example for 5 = 101 it will output only one smile with code 0x01).
2) your last character in array is not '\0', so cout will output garbage until '\0' or memory access exception.

how to convert string into an integer in a batch file ?

I have this piece of batch file :
#echo off
set xx=7
echo the version is %xx%
I wand to use it in a pre-build event in VS2010 - as an integer :
MY_INT = $(xx)
but it's value is a string , how can I convert the string value into an integer value in the batch file?
thanks!
Environment variables (batch variables) are always strings; they cannot be stored as integers.
The SET command with the /A option can parse integral numbers from strings, perform arithmetic operations, and store the integral result in an environment variable. But the final result is still a string representation of the final number.
Type help set or set /? from a command prompt for more info about the SET /A option.

Error when reading in float in Fortran

This should be quite simple, but I can't manage to read in a floating point number in Fortran. My program test.f looks like this:
PROGRAM TEST
open(UNIT=1,FILE='test.inp')
read(1,'(f3.0)')line
STOP
END
The input file test.inp simply contains a single float: 1.2
Now the compiling of my testfile goes fine, but when I run it I get an error:
At line 4 of file test.f (unit = 1, file = 'test.inp')
Fortran runtime error: Expected REAL for item 1 in formatted transfer, got INTEGER
(f3.0)
^
I've tried different modifications of the code and also googling for the error message, but with no result. Any help would be greatly appreciated!
Regards,
Frank
Your variable line is implicitly defined as integer. This doesn't work with thef edit descriptor. If you want to read an integer use i edit descriptor (i3 for example). Otherwise declare line as real to math the "f" descriptor.
Note beside: the .0 is not a problem, because if Fortran gets a number with decimal point the .0 part in the descriptor is ignored. It is only used when an number without a decimal is entered and then it uses the number behind the decimal point in the desciptor to add a decimal point into the right place. For with F8.5, 123456789 is read as 123.45678. More ont this here http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/lref_for/source_files/pghredf.htm .
In your read statement
read(1,'(f3.0)')line
the f3.0 tells tour program to read 3 digits with 0 digits after the decimal (this is what the n.m syntax means). So I presume that the program is just reading 1 from the file (not 1.2), which is an integer. Try replacing that line with something like
read(1,'(f3.1)')line
although, if the number in your file is likely to change and be larger than 9.9 or have more than one decimal place you should increase the field width to something larger than 3.
See the documentation of the read intrinsic and for data edit descriptors for more information on reading and writing in Fortran.
Edit: the format specifier, the second argument in quotes in your read statment, has the form fw.d, where f indicates that the data to read is a floating point number, w is the width of the field including all blanks and decimal points and d specifies the number of digits to the right of the decimal point.
I would suggest reading/writing list formatted data, unless you have a very strong reason to do otherwise. assuming that you're reading in from a file with just a single float or integer in a single line, like this
123.45
11
42
then this should do the reading
real*8 :: x,y,z
open(1,file=filename)
read(1,*)x
read(1,*)y
read(1,*)z
close(1)