I am shifting in 3 chars from serial port every 3rd char is a double that i split up for the port, so now i need to put it back to gether.
(Value before sending = 0x3F3400 or 0.703125)
char[0] = 0x3F (msb)
char[1] = 0x34
char[2] = 0x00 (lsb)
double total = (char[0] << 16)+(char[1] << 8)+(char[2]); // this part did not work
Pls advise how to rebuild this, I dont understand doubles well enough, I cannot even tell which part of a double is the decimal point or - sign
thanks!
I can't get yor value, but as help can advice you to use BitConverter and byte array like this:
var v = new byte[8];
v[7] = 0x3F;
v[6] = 0x34;
v[5] = 0x00;
double total = BitConverter.ToDouble(v, 0);
Console.WriteLine(total.ToString("0.0000000000000"));
Related
I'm communicating with a device that requires a password to be embedded into the command using hex. The password must be 8 bytes. If the ASCII numeric password is less then 16 digits the password is padded with a F.
For example if the password is 12345 I need to be able to convert the password to 0x12 0x34 0x5F 0xFF 0xFF 0xFF 0xFF 0xFF.
What I am not sure how to do is get 0x12 from "12".
I was thinking about if I could convert 1 to 0x10 and 2 to 0x02 I could then add the two bytes together to get 0x12. However is there a way to get the 0x10 and 0x02 without having to create a hash table or is there a better approach?
Edit: To whomever was rude enough to down vote. This is a unique question I'm not looking to do simple get the hex value of 12345 where as hex it would be represented as 31 32 33 34 35. If one of the other answers in that question does cover this, this at least presents the answer to the question where the questioned is presented directly. I intend to look into the bit shifting and the answers to the linked question flew right over my head. If this site is intended to facilitate learning then we cannot have a catch all section because the answer becomes too complex to help a layman learn.
Big thanks to Itisme86. =) Based on Itisme86's help above I put together the following code to do the conversion. This is pre error handling and enforcing the format before running the code.
internal string FormatPassword(string password)
{
string formattedPassword = string.Empty;
for (int pos = 0; pos < password.Length; pos+=2)
{
string partialPassword = string.Empty;
if (password.Length > pos + 1)
{
partialPassword = GetPasswordByte(password.Substring(pos, 2));
}
else
{
partialPassword = GetPasswordByte(password.Substring(pos, 1));
}
if (!string.IsNullOrEmpty(partialPassword))
{
formattedPassword += partialPassword;
}
}
if (formattedPassword.Length < 8)
{
formattedPassword = formattedPassword.PadRight(8, (char)0xFF);
}
return formattedPassword;
}
internal string GetPasswordByte(string partialPassword)
{
string byteString = string.Empty;
int digit1 = Convert.ToInt16(partialPassword.Substring(0, 1));
int digit2 = 15;
if (partialPassword.Length > 1)
{
digit2 = Convert.ToInt16(partialPassword.Substring(1, 1));
}
byte[] passwordByte = BitConverter.GetBytes((digit1 << 4) | (digit2 & 0xF));
byteString = System.Text.Encoding.Default.GetString(passwordByte, 0, 1);
return byteString;
}
How can I extract bytes from double type. I know it has 8 bytes, just like long. How can I create a long variable that has the same bytes as the double has.
double a = 1.5;
long b = (long)a; // <- this returns 1
// i want to get this: 0 01111111111 1000000000000000000000000000000000000000000000000000
//which is 4609434218613702656 as long (I guess :) )
How can I do this quickly, please?
You can get it like this
double a = 1.5;
long l = BitConverter.ToInt64(BitConverter.GetBytes(a), 0);
It will be 4609434218613702656
As #harold suggested
var l2 = BitConverter.DoubleToInt64Bits(a);
is possible too
I have a unique situation where I have to write code on top of an already establish platform so I am trying to figure out a hack to make something work.
The problem I have is I have a user defined string. Basically naming a signal. I need to get this into another program but the only method available is within a double value. Below is what I have tried but not been able to get it to work. I tried converting the string to byte array and then creating a new string by looping the bytes. Then I convert this string to a Double. Then use BitCoverter to get it back to byte array and then try to get the string.
Not sure if this can even be achieve. Any ideas?
string signal = "R3MEXA";
string newId = "1";
byte[] asciiBytes = System.Text.Encoding.ASCII.GetBytes(signal);
foreach (byte b in asciiBytes)
newId += b.ToString();
double signalInt = Double.Parse(newId);
byte[] bytes = BitConverter.GetBytes(signalInt);
string result = System.Text.Encoding.ASCII.GetString(bytes);
Asuming your string consists of ASCII characters (7Bit):
Convert your string into a bit-Array, seven bits per character.
Convert this bit-array into a string of digits, using 3 bits for each digit. (there are digits 0..7)
Convert this string of digits to a double number.
You initially set newId to "1", which means when you're doing later conversion, you're not going to get the right output unless to account for the "1" again.
It doesn't work, because if you convert it back you don't know the length of a byte.
So I made every byte to a length of 3.
string signal = "R3MEXA";
string newId = "1";
byte[] asciiBytes = System.Text.Encoding.ASCII.GetBytes(signal);
foreach (byte b in asciiBytes)
newId += b.ToString().PadLeft(3,'0'); //Add Zero, if the byte has less than 3 digits
double signalInt = Double.Parse(newId);
//Convert it back
List<byte> bytes = new List<byte>(); //Create a list, we don't know how many bytes will come (Or you calc it: maximum is _signal / 3)
//string _signal = signalInt.ToString("F0"); //Maybe you know a better way to get the double to string without scientific
//This is my workaround to get the integer part from the double:
//It's not perfect, but I don't know another way at the moment without losing information
string _signal = "";
while (signalInt > 1)
{
int _int = (int)(signalInt % 10);
_signal += (_int).ToString();
signalInt /= 10;
}
_signal = String.Join("",_signal.Reverse());
for (int i = 1; i < _signal.Length; i+=3)
{
byte b = Convert.ToByte(_signal.Substring(i, 3)); //Make 3 digits to one byte
if(b!=0) //With the ToString("F0") it is possible that empty bytes are at the end
bytes.Add(b);
}
string result = System.Text.Encoding.ASCII.GetString(bytes.ToArray()); //Yeah "R3MEX" The "A" is lost, because double can't hold that much.
What can improved?
Not every PadLeft is necessary. Work from back to front and if the third digit of a byte is greater than 2, you know, that the byte has only two digits. (Sorry for my english, I write an example).
Example
194 | 68 | 75 | 13
194687513
Reverse:
315786491
31 //5 is too big 13
57 //8 is too big 75
86 //4 is too big 68
491 //1 is ok 194
What I'm trying to do is loop through the values in the byte array getting the first digit of each value and swapping its place with the second digit, so 35 would be 53 and 24 would be 42.. I can almost do this but i have to convert everything to strings and that seems a little overkill
I've tried it for a while but so far I've only figured that I can convert everything to a string and then work on them, just seems a little clunky..
It sounds like you want to swap the high and low nibble in each byte.
0x35; // High nibble = 3, Low Nibble = 5
To do this, you want to shift the high nibble right 4 bits (to make it the low nibble), and shift the low nibble left 4 bits (to make it the high nibble).
var ar = new byte[] { 0x35, 0x24 };
for (int i=0; i<ar.Length; i++) {
byte b = ar[i];
b = (b>>4) | ((b&0x0F)<<4);
ar[i] = b;
}
byte nmbBase =16; //or any other 10 for decimal
byte firstDigit = number /nmbBase;
byte secondDigit = number % nmbBase;
number = secondDigit*numberBase + firstDigit;
This is from cellphone,sorry for any mistakes. You should get in which direction you should go.
I have a task to complete in C#. I have a Subnet Mask: 255.255.128.0.
I need to find the number of bits in the Subnet Mask, which would be, in this case, 17.
However, I need to be able to do this in C# WITHOUT the use of the System.Net library (the system I am programming in does not have access to this library).
It seems like the process should be something like:
1) Split the Subnet Mask into Octets.
2) Convert the Octets to be binary.
3) Count the number of Ones in each Octet.
4) Output the total number of found Ones.
However, my C# is pretty poor. Does anyone have the C# knowledge to help?
Bit counting algorithm taken from:
http://www.necessaryandsufficient.net/2009/04/optimising-bit-counting-using-iterative-data-driven-development/
string mask = "255.255.128.0";
int totalBits = 0;
foreach (string octet in mask.Split('.'))
{
byte octetByte = byte.Parse(octet);
while (octetByte != 0)
{
totalBits += octetByte & 1; // logical AND on the LSB
octetByte >>= 1; // do a bitwise shift to the right to create a new LSB
}
}
Console.WriteLine(totalBits);
The most simple algorithm from the article was used. If performance is critical, you might want to read the article and use a more optimized solution from it.
string ip = "255.255.128.0";
string a = "";
ip.Split('.').ToList().ForEach(x => a += Convert.ToInt32(x, 2).ToString());
int ones_found = a.Replace("0", "").Length;
A complete sample:
public int CountBit(string mask)
{
int ones=0;
Array.ForEach(mask.Split('.'),(s)=>Array.ForEach(Convert.ToString(int.Parse(s),2).Where(c=>c=='1').ToArray(),(k)=>ones++));
return ones
}
You can convert a number to binary like this:
string ip = "255.255.128.0";
string[] tokens = ip.Split('.');
string result = "";
foreach (string token in tokens)
{
int tokenNum = int.Parse(token);
string octet = Convert.ToString(tokenNum, 2);
while (octet.Length < 8)
octet = octet + '0';
result += octet;
}
int mask = result.LastIndexOf('1') + 1;
The solution is to use a binary operation like
foreach(string octet in ipAddress.Split('.'))
{
int oct = int.Parse(octet);
while(oct !=0)
{
total += oct & 1; // {1}
oct >>=1; //{2}
}
}
The trick is that on line {1} the binary AND is in sence a multiplication so multiplicating 1x0=0, 1x1=1. So if we have some hypothetic number
0000101001 and multiply it by 1 (so in binary world we execute &), which is nothig else then 0000000001, we get
0000101001
0000000001
Most right digit is 1 in both numbers so making binary AND return 1, otherwise if ANY of the numbers minor digit will be 0, the result will be 0.
So here, on line total += oct & 1 we add to tolal either 1 or 0, based on that digi number.
On line {2}, instead we just shift the minor bit to right by, actually, deviding the number by 2, untill it becomes 0.
Easy.
EDIT
This is valid for intgere and for byte types, but do not use this technique on floating point numbers. By the way, it's pretty valuable solution for this question.