why does this value get cut off? - hex

So I'm trying to print out hex values in c++ using printf, and well, I want the output of the following string to be val:0x0366 including that leading 0
Here's the code
int poo = 0x00000366;
printf("val:0x%x \n",poo);
Here's the output:
0x366

Have you tried printf("val:0x%04x \n",poo);?
According to printf reference,
A format specifier follows this prototype:
%[flags][width][.precision][length]specifier
In your case:
%[0][4][default precision][default lenght]x

Related

Comparison of Hex Numbers in Tcl

I am trying to compare the two following hex numbers in a TCL Script:
0x00001111
32'h00001111
They should be equal to each other, but I am having trouble comparing them because of the difference in formatting.
I have tried using "scan" command to convert, but have not gotten the correct output I am looking for.
Code snippet:
set read1 0x00001111
set read2 32'h00001111
set new_read [scan $read2 %x]
if ($read1 == $read2) {
puts "Two values equal"
}
This code does not work and sets new_read to 4369. Any help is appreciated
You want to change the format specifier to scan to sth. like:
if {[scan $read2 "%d'h%x" bw new_read] == 2} {
# on success
} else {
# on failure
}
This way, you will be able to compare scan $read1 %x and $new_read using == & friends.

Netmiko: How to search in switch using a variable

I am not getting any output if I am doing this.
mac_address=abcd
output=net_connect.send_command('show mac-address-table | inc mac_address')
print("Output of the switch ",output)
I am getting the desired output if I am doing this.
output=net_connect.send_command('show mac-address-table | inc abcd')
print("Output of the switch ",output)
What should I make change in the code so that I can use variable?
The closing quote in the first example is after mac_address, making that literal text, not a variable. I'm not sure how you append two strings, but something like:
output=net_connect.send_command('show mac-address-table | inc '+mac_address)
where the + is appending the literal string and the variable string.

LPAD is not working in progress 4gl

I am tring to use lpad in progress Db but its not working..
Code:
lpad(act_num, 7, '#')
This code not working , Do we have any alternative way to achieve o/p.
If act_num is 101 then o/P shoud br 7777101.
There is no lpad() function in OpenEdge, but you may be able to use the FILL() function. It takes two inputs: a character string to use as the fill value, and the number of times to repeat the string.
This will add four "7"s to the beginning of act_num, as you described in your question:
DEFINE VARIABLE act_num AS CHARACTER NO-UNDO INITIAL "101".
act_num = FILL("7", 4) + act_num.
MESSAGE act_num VIEW-AS ALERT-BOX.
The fill value can be any string, and not just a single character.

Format zero currency value with {0:C} in VB.Net

I am trying to format a zero currency value as an empty string, so that when the currency value is 0.00 then an empty string gets displayed rather than $0.00.
This code is part of an ASP.Net app that will display currency value to end user.
I have used following code to achieve this goal.
Question : Is it possible to achieve this by just using {0:C} format string or another version of this format string instead of using if then else coding for this? If I use ###,###,###.## as the data format string then an empty string shows for zero currency value and also I get rid of the if then else coding but for non-zero values no currency symbol shows.
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
charValue = Nothing
Else
charValue = String.Format("{0:C}", CDec(currencyValue))
End If
UPDATE
I ended up using the following code, which is working fine. If is better than IIf because it does short-circuiting, which means that IIf will evaluate all expressions whether the condition is true or false but If will evaluate the first expression only if condition is true and evaluate the second expression only if condition is false.
Dim d As Decimal
Decimal.TryParse(currencyValue, d)
charValue = If(d = 0D, Nothing, String.Format("{0:C}", d))
I don't think there is a way using formatting to display an empty string.
But you can write it like:
charValue = If( currencyValue = 0D, "", currencyValue.ToString("C") )
using the If Operator (Visual Basic).
Also this is something I would not do:
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
If currencyValue is Decimal:
If (currencyValue = 0D) Then
If currencyValue is Double:
If (currencyValue = 0R) Then
Also, if you are using a database and this is a Sql Server mind SQL Server Data Type Mappings
I don't think you can when using C or the other similar standard formats, since they are already defining a culture-specific format that will include a format for zero.
But if you specify your own custom format, you can specify three different formats separated by ;s, one each for positive numbers, negative numbers, and zero, respectively.
For example (giving an empty string for the zero format, resulting in blank zeroes):
charValue = String.Format("{0:#,##0.00;-#,##0.00;""""}", CDec(currencyValue))
And from what I can see, omitting the format for negative gives a default that matches the positive, whereas omitting the format for zero gives blank, which is what you're looking for, so this should be sufficient as well:
charValue = String.Format("{0:#,##0.00;;}", CDec(currencyValue))
(Using whichever custom format you wish.)
UPDATE: You can get the current currency symbol and manually put it into your custom format. IE:
Dim symbol = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol
charValue = String.Format("{0}{1:#,##0.00;;}", symbol, CDec(currencyValue))
From the sound of it, though, I think I would actually recommend doing basically what you started with, maybe with an extension method.
<Extension>
Public Function ToCurrencyString(pValue As Decimal) As String
Return IIf(pValue = 0, "", pValue.ToString("C"))
End Function
Dim someValue As Decimal = 1.23
Console.WriteLine(someValue.ToCurrencyString())
This gives you exactly what you're looking for. The exact same format as C gives, but with blank zeroes.

Unit test failing on two equal Qstrings (one is read from a file)?

I'm trying to compare two string which are supposed to be the same. The test however fails.
I'm testing if the file get set correctly by the router.setForwarding(true) method.
Here is the code of the test.
void router_test::testSetForwarding_true()
{
QFile myfile("/proc/sys/net/ipv4/ip_forward");
myfile.open(QIODevice::ReadOnly | QIODevice::Text);
router->setForwarding(true);
QString forward = QString(myfile.readAll());
QCOMPARE(QString("1"),forward);
}
As a result I get:
FAIL! : router_test::testSetForwarding_true() Compared values are not the same
Actual (QString("1")): 1
Expected (forward): 1
Why aren't they equal?
As you can glean from the output, you have interchanged the actual and expected values. You're also comparing the newline-terminated output against one without a newline.
This should work:
QCOMPARE(forward, QString("1\n"));
or
QCOMPARE(forward[0], QChar('1'));

Resources