combine two 16 bit integer to 32 bit float value c# [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to Combine two 16 bit integer and trying to convert into 32 bit float value in my service but I am not able to get. Finally I figure out how to do.
Int16 val1 = 0;
Int16 val2 = 16880;
output should be:
30

Int16 val1 = 0;
Int16 val2 = 16880;
var byteval1 = BitConverter.GetBytes(val1);
var byteval2 = BitConverter.GetBytes(val2);
byte[] temp2 = new byte[4];
temp2[0] = byteval1[0];
temp2[1] = byteval1[1];
temp2[2] = byteval2[0];
temp2[3] = byteval2[1];
float myFloat = System.BitConverter.ToSingle(temp2, 0);

Related

Converting From String to INT saves another value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have one function in which i take the numbers as string and when i convert the numbers to int they are saved with some other value.
Why is that Please help.
private string DoTheMath()
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int n1 = Convert.ToInt32(s1[0]);
int k1 = Convert.ToInt32(s1[1]);
}
when i input 49 51
int n1 gets value 31
and int k1 gets value 33
Since you are parsing strings to ints, you probably want Int32.Parse:
private string DoTheMath()
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int n1 = Int32.Parse(s1[0]);
int k1 = Int32.Parse(s1[1]);
}

Is the following an encryption algorithm? If so, is it reversible? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I came across the following method in a programming example. Is this really an encryption algorithm? Or is it more of a hex hashing/irreversible encoding algorithm? I see the use of bitwise shifts and bitwise ands which leads me to believe that the method has data loss and is an irreversible hex encoding algorithm.
private string Encrypt(string key, string message)
{
string result = "";
var hexValues = "0123456789abcdef";
for (int i = 0, j = 0; i < message.Length; i++)
{
var a = (Int32)message[i];
var b = (Int32)key[j] & 10;
var encChar = a ^ b;
if (++j == key.Length)
{
j = 0;
}
result += hexValues[(encChar >> 4) & 15];
result += hexValues[encChar & 15];
}
return result;
}
At its heart, this algorithm is performing XOR encryption, a weak and easily broken form of encryption.
var encChar = a ^ b;
The bit shifts are used to get a hex value corresponding to the "encrypted" character position.
result += hexValues[(encChar >> 4) & 15];
result += hexValues[encChar & 15];
The & mask is used to select a value to XOR the character at the given position against. It is providing a "hidden" change to the key, a practice sometimes called security through obscurity (which does not add much to the actual security).
var b = (Int32)key[j] & 10;

Calculating offset from two hex strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a program that calculates offset but I do not have the source code for. I am trying to see how this is calculated using C#. The first input hex digit cannot be less than 80. This is what I have written so far but it does not calculate the offset correctly:
private void getOffSet()
{
// Default offset
int defaultOffset = 0x0512;
// Input
byte byte1input = 0x80;
byte byte2input = 0x00;
int inValue = byte2input + (byte1input << 8);
// Calculate offset
int outValue = inValue + defaultOffset;
// Convert integer as a hex in a string variable
string hexValue = outValue.ToString("X");
}
Any help correcting this function to calculate the correct offset would be greatly appreciated. Thank you before hand.
The following function matches your list. It is, however, a wild guess of course as there is no way I can match it with the original program and I have no idea what these offsets mean.
private void getOffSet(byte one, byte two)
{
byte baseByte = 0x80;
int defaultOffset = 0x0418;
int mul = (one - baseByte) % 8;
int result = mul * 0x2000 + defaultOffset;
result += two * 0x0020;
Console.WriteLine(result.ToString("X"));
}

Retrieving a value that is stored as MB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table that stores the amount of RAM a server has in a biginit column with values such as 2470208.
But how I can apply a data annotation or other validations to show only 2 instead of s470208. ?
I mean to always divide by 1 million and get the number on the left side of the digit ?
1) Use this for automatic thousands-unit:
string GetByteString(long n) {
int k=0;
string u=" kMGTEP";
while(n>1024) {
n>>=10;
k++;
}
return n.ToString() + u[k];
}
Call:
string s= GetByteString(1234567890123);
Debug.WriteLine(s);
2) But if you simply always want MB just shift by 20:
long n = 123456789;
string MB = (n>>20).ToString();
But this will show 0 if n goes below 1MB.
Reason:
1 kB = 2^10 = 1<<10 = 1024;
1 MB = 2^20 = 1<<20 = 1024*1024 = 1048576;
1 GB = 2^30 = 1<<30 = 1024*1024*1024 = 1073741824;
You tagged C# but mentioned a bigint column so it isn't clear whether you're looking for a database or C# solution. The following C# method will take the number of bytes as an integer and return a formatted string...
public string FormattedBytes(long bytes)
{
string units = " kMGT";
double logBase = Math.Log((double)bytes, 1024.0);
double floorBase = Math.Floor(logBase);
return String.Format("{0:N2}{1}b",
Math.Pow(1024.0, logBase - floorBase),
units.Substring((int)floorBase, 1));
}

C# quiz calculate percentage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a quiz that have many questions and 5 possibilities of answers.
Lets take one question,it has follow answers:
148 - Good
5 - N/A
268 - Great
5 - Regular
11 - Bad
These are numbers collected directly from database.Now i need to show it as percentage.i.E:
Great - 45%
Good - 40
[..]
and so on
Any ideas?
int na = 5;
int good = 148;
int great = 268;
int regular = 5;
int bad = 11;
int sum = na + good + great + regular + bad;
naPercent = getPercent(na,sum);
float getPercent(int value, int sum)
{
return (value*100.0)/sum;
}
This is not a programming question, it is a math question. The percentage of each item is equal to the number of that item divided by the total number. In your example, the total number is 148+5+268+5+11 = 437. Great = 268 / 437 = 61.327%
total count for the answer / total count for all answers to this question combined * 100

Categories

Resources