Visual studio get int value in hexadecimal format - visual-studio-2017

Why I get these question marks, do I have to change the encoding or something? Can I create a unicode point from hexadecimal or int and then just do:
char output = convertedValue;
Here`s my code for the task above:

What's going on?
public class UpdateInvoice : BaseInvoice
{
public bool booked { get; set; }//
public int? balance { get; set; }//
public string paidDate { get; set; }//
public bool sent { get; set; }//
}

Related

Working with Inner List containing Item with specific Property value, Java 8

I created a code, for specific task. But, I think: must to have better manner to do this code.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class InnerListContainingItemWithSpecificPropertyValue {
public static void main(String[] args) {
List<List<Person>> nestedPersonList = Arrays.asList(
Arrays.asList(new Person("a0", 0), new Person("b0", 1)), //0
Arrays.asList(new Person("a1", 0), new Person("b1", 1)), //1
Arrays.asList(new Person("a2", 0), new Person("b2", 1)), //2
Arrays.asList(new Person("a3", 0), new Person("b3", 1)) // 5
);
List<List<Person>> outNestedPersonList = new ArrayList<>();
nestedPersonList.stream().flatMap(List::stream).forEach(outerPerson -> {
//Determine if Exist Some inner List
boolean ageFound = outNestedPersonList
.stream()
.flatMap(List::stream)
.filter(innerPerson -> outerPerson.getAge() == innerPerson.getAge())
.count() > 0L;
List<Person> listPersonWithAge;
if (!ageFound) {
listPersonWithAge = new ArrayList<>();
outNestedPersonList.add(listPersonWithAge);
} else {
// Get the Inner List with Specific Property Value
listPersonWithAge = outNestedPersonList
.stream()
.filter(innerListPerson -> {
return innerListPerson
.stream()
.filter(innerPerson -> outerPerson.getAge() == innerPerson.getAge())
.count() > 0L;
}).findFirst().get();
}
listPersonWithAge.add(outerPerson);
// Do something
if (listPersonWithAge.size() == 4) {
System.out.println("listPerson with age:" + outerPerson.getAge() + "\twill be removed!");
outNestedPersonList.remove(listPersonWithAge);
}
});
}
public static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public int hashCode() {
int hash = 5;
hash = 73 * hash + Objects.hashCode(this.name);
hash = 73 * hash + this.age;
return hash;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if (this.age != other.age) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
#Override
public String toString() {
return "Person{" + "name=" + name + ", age=" + age + '}';
}
}
}
output:
listPerson with age:0 will be removed!
listPerson with age:1 will be removed!
Alternatively my code:
How know if is there inner List containing some item with some
Property Value?
How I can to obtain the inner list with that item?
If the external list does not contain an internal list, How create one?
The first step of improving your code is to avoid using flatMap. It makes it easier to operate on the nested data, but you are trying to operate on one of the sublists, not on the all of the people as a whole.
You are trying to operate on one sublist, not all of the people in all of the lists, so instead of using flatMap, you can nest two sets of stream operations.
listOfListOfPeople.stream()
// filter out sublists that don't have anyone with the target age
.filter(sublist ->
// Check if the nested list contains anyone with the given age
sublist.stream().anyMatch(p -> p.age == targetAge))
// get one sublist out of the stream
.findAny()
// if there wasn't one, get an empty list
.orElse(Collections.emptyList());
If you want to be able to modify the empty list you get if there aren't any people with the target age, replace the last line with something like .orElseGet(ArrayList::new).

Static int with array C++

I'm trying to static count my driver user. however it always give me same value instead
class Driver {
private:
static int ID;
string name;
public :
void displayDriver(string n) {
cout << ID << endl;
}
void createDriver(string n) {
name = n ;
ID++;
}
}
int Driver::id=0;
int main() {
Driver driver[10];
Driver[0].createDriver("first");
Driver[1].createDriver("second");
Driver[2].createDriver("first");
Driver[0].displayDriver();
Driver[1].displayDriver();
Driver[2].displayDriver();
}
my expected output should be :
1
2
3
but system shows me :
3
3
3
private:
static int ID;
Denotes that ID is shared among Driver instances. Any change to it (from createDriver in your current code) will be reflected in ALL instances. Don't use static field if you want each instance has its own unique field.
Do something like I show here to get the results you expect. The idea is to keep the next counter in ID but every instance to have its own id that get initialized when you create the driver.
class Driver
{
private:
static int ID;
int my_id;
string name;
public :
void displayDriver(string n)
{
cout << my_id << endl;
}
void createDriver(string n)
{
name = n ;
my_id = ID++;
}
}
int Driver::id=0;
int main
{
Driver driver[10];
Driver[0].createDriver("first");
Driver[1].createDriver("second");
Driver[2].createDriver("first");
Driver[0].displayDriver();
Driver[1].displayDriver();
Driver[2].displayDriver();
}
It is because the value of ID gets incremented each time u createDriver("");
(no matter for which Driver[]) as it is a static variable
So u r incrementing ID progressively to 3 in first 3 lines
while displaying value of ID in next 3 lines
To get the expected output
I think u need to try it like this :
Driver[0].createDriver("");
Driver[0].displayDriver();
Driver[1].createDriver("");
.
.
.
So on!
Note that all drivers have the same ID (static field).

Can Fortify identify the servlet filter?

I used the filter to fix the XSS. But when i scan my codes using fortify software, the number of XSS issues didn't change. Did i miss something? or Fortify cannot recognize the filter? Here are my filter codes:
public final class RequestWrapper extends HttpServletRequestWrapper {
public RequestWrapper(HttpServletRequest servletRequest) {
super(servletRequest);
}
public String[] getParameterValues(String parameter) {
String[] values = super.getParameterValues(parameter);
if (values==null) {
return null;
}
int count = values.length;
String[] encodedValues = new String[count];
for (int i = 0; i < count; i++) {
encodedValues[i] = cleanXSS(values[i]);
}
return encodedValues;
}
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
private String cleanXSS(String value) {
System.out.println("filter : " + value);
//System.out.println("afterfilter : " + (isNotEmptyOrNull(value) ? StringEscapeUtils.escapeHtml4(value) : value));
//return isNotEmptyOrNull(value) ? StringEscapeUtils.escapeHtml4(value) : value;
if(isNotEmptyOrNull(value)){
value = value.replaceAll("<", "<").replaceAll(">", ">");
value = value.replaceAll("\\(", "(").replaceAll("\\)", ")");
value = value.replaceAll("'", "'");
value = value.replaceAll("eval\\((.*)\\)", "");
value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
value = value.replaceAll("script", "");
}
System.out.println("afterfilter : " +value);
return value;
}
public static boolean isNotEmptyOrNull(String string) {
if (string != null && !"".equals(string.trim())) {
return true;
}
return false;
}
}
The problem here is that Fortify doesn't have a rule for your cleanXSS method. What you will have to do is to write a custom rule, specifically a "data flow cleanse rule". This will let fortify know that any data that enters and then returned from this method will be safe from XSS.
However after looking at you XSS filter, I have to inform you that it's incomplete and will not take in to account all possible XSS vectors. I recommend that you use OWASP ESAPI's XSS filter. Fortify already has a rules for ESAPI.

C# Byte Array of lists (or dictionaries) with equal content are not equal

I have two Lists with the same content. The first one had added three items and removed one again. The second one had added only the remaining two items. Then I converted both to byte array, but the byte array are not equal. They differ in one byte.
Is there a way to have two "equal" lists with equal byte arrays independent of performed operations?
I need this for saving the list to a text file with a RSA signature included. But I can't have a valide signature if the list in memory, with operations performed on it in the past, is not equal to the new generated list from the content of the text file.
In my Application i use dictionaries instead of lists, but it's the same behaviour.
Here is my test code:
static void Main(string[] args)
{
List<string> one = new List<string>();
one.Add("hallo");
one.Add("hallo1");
one.Remove("hallo1");
one.Add("hallo1");
byte[] test = Program.ObjectToByteArray(one);
Console.WriteLine(test.Length);
List<string> two = new List<string>();
two.Add("hallo");
two.Add("hallo1");
byte[] test1 = Program.ObjectToByteArray(two);
Console.WriteLine(test1.Length);
bool areEqual = false;
int count = 0;
if (test.Length == test1.Length)
{
areEqual = true;
for (int i = 0; i < test.Length; i++)
{
if (test[i] != test1[i])
{
areEqual = false;
count++;
}
}
}
Console.WriteLine("Are they equal?: " + areEqual.ToString() + ": " + count.ToString());
Console.ReadLine();
}
private static byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
Edit:
In my application i will have a class like this:
[Serializable]
class MetaDataModel
{
private Dictionary<string, string> readers;
private Dictionary<string, string> writers;
public bool IsDirty { get; set; }
public string PublicReaderKey { get; set; }
public string CipherPrivateReaderKey { get; set; }
public Dictionary<string, string> Readers
{
get
{
if (this.readers == null)
this.readers = new Dictionary<string, string>();
return this.readers;
}
set
{
this.readers = value;
}
}
public string PublicWriterKey { get; set; }
public string CipherPrivateWriterKey { get; set; }
public Dictionary<string, string> Writers
{
get
{
if (this.writers == null)
this.writers = new Dictionary<string, string>();
return this.writers;
}
set
{
this.writers = value;
}
}
public byte[] Signature { get; set; }
}
And want it use like this:
MetaDataModel metaData = new MetaDataModel()
//filling the properties here
metaData.Signature = RSASignatureGenerator.HashAndSignData(this.ObjectToByteArray(metaData), this.rsaKeyPair[0]);

calling method that contains if/else

This is a fairly basic question but as a new Java student it's stumped me. This a practice problem that I'm working on and can't figure out how to call the hotOrColdOutside method into the main. As of now it's not compiling and asking for a return but my instructions specify there is no arguments or return in this instance. I feel like this is something simple that I know but is going over my head at the moment and any help in correcting this will be appreciated.
import java.util.Scanner;
class TempExp
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String answer = outsideTemp();
System.out.print(answer);
}//end main
public static String outsideTemp()
{
Scanner input = new Scanner(System.in);
System.out.print("What is the temperature outside: ");
int userIn = input.nextInt();
if(userIn >= 80)
{
System.out.print("It is very hot outside.");
}
else if(userIn >= 60)
{
System.out.print("It is very nice outside.");
}
else
{
System.out.print("It is very cold outside.");
}//end if/else
}//end method
}
The function
public static String outsideTemp()
returns a String. The main function doesn't return anything, though.
You'll want something like this:
import java.util.Scanner;
class TempExp
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String answer = outsideTemp();
System.out.print(answer);
}//end main
public static String outsideTemp()
{
Scanner input = new Scanner(System.in);
System.out.print("What is the temperature outside: ");
int userIn = input.nextInt();
if(userIn >= 80)
{
return "It is very hot outside.";
}
else if(userIn >= 60)
{
return "It is very nice outside.";
}
else
{
return "It is very cold outside.";
}//end if/else
}//end method
}
Because you say your instructions (I'm guessing you mean the assignment?) say there are no arguments or returns, you need to replace the String part of the function definition of outsideTemp() with void. Because it will not return anything, your main function will also need to be changed to not expect anything to be returned.
public static void main(String[] args)
{
outsideTemp();
}//end main
public static void outsideTemp()
{
... // this all stays exactly how it is now
}
However, if you do mean to return a string, use John's answer