I am using some sensor with Serial Communication. Because the sensor data have HEX value, I should convert string data to hex data. So, I am using Encoding.Default.GetBytes():
byte[] Bytdata0 = Encoding.Default.GetBytes(st.Substring(0, 1));
byte[] Bytdata1 = Encoding.Default.GetBytes(st.Substring(1, 1));
foreach (byte byte_str in Bytdata0) Whole_data[0] = string.Format("{0:X2}", byte_str);
foreach (byte byte_str in Bytdata1) Whole_data[1] = string.Format("{0:X2}", byte_str);
In this example, there is a problem - the converted value of sensor is wrong when the value is bigger than 0x80.
For example
74 61 85 0A FF 34 00 :: Original signal.
74 61 3F 0A 3F 34 00 :: Converted signal.
the fifth bytes differ. I don't know what is wrong.
string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}
/* Output:
Hexadecimal value of H is 48
Hexadecimal value of e is 65
Hexadecimal value of l is 6C
Hexadecimal value of l is 6C
Hexadecimal value of o is 6F
Hexadecimal value of is 20
Hexadecimal value of W is 57
Hexadecimal value of o is 6F
Hexadecimal value of r is 72
Hexadecimal value of l is 6C
Hexadecimal value of d is 64
Hexadecimal value of ! is 21
*/
SOURCE: http://msdn.microsoft.com/en-us/library/bb311038.aspx
// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
from http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html
Related
As the title says, I've been working on MiFare Classic reading a card.
I'm using the MiFare v1.1.3 Library from NuGet
and it returns a byte array, which I parse to readable Hex strings, by looping thru it.
Here's the code snippet:
int sector = 1;
int block = 0;
int size = 16;
var data = await localCard.GetData(sector, block, size);
string hexString = "";
for (int i = 0; i < data.Length; i++)
{
hexString += data[i].ToString("X2") + " ";
}
// hexString returns 84 3D 17 B0 1E 08 04 00 02 63 B5 F6 B9 BE 77 1D
Now, how can I parse it properly?
I've tried parsing it into ASCII, ANSI, Int, Int64, Base64, Long
and all of them didn't match the 'data' that it's suppose to contain
EDIT:
The expected output: 1206058
HEX String returned: 84 3D 17 B0 1E 08 04 00 02 63 B5 F6 B9 BE 77 1D
I've checked the source code
it looks like both Task<byte[]> GetData Task SetData methods do not have any special logic to transform the data. Data are just saved (and read) as byte[]
I suppose that you have to contact author/company that has wrote data you are trying to read.
The expected output: 1206058
Looks strange since you are reading 16 bytes size = 16 and expecting 7 characters to be read.
Is it possible that block or sector values are incorrect ?
I have written a simple program to solve your problem. Perhaps this is what you want to achieve:
// The original byte data array; some random data
byte[] data = { 0, 1, 2, 3, 4, 85, 128, 255 };
// Byte data -> Hex string
StringBuilder hexString = new StringBuilder();
foreach (byte item in data)
{
hexString.Append($"{item.ToString("X2")} ");
}
Console.WriteLine(hexString.ToString().Trim());
// Hex string -> List of bytes
string[] hexArray = hexString.ToString().Trim().Split(' ');
List<byte> dataList = new List<byte>();
foreach (string item in hexArray)
{
dataList.Add(byte.Parse(item, System.Globalization.NumberStyles.HexNumber));
}
dataList.ForEach(b => Console.Write($"{b} "));
Console.WriteLine();
If it is not the right solution please provide us more info about your problem.
If var data potentially is string - you can reverse it from hex by:
// To Hex
byte[] plainBytes = Encoding.ASCII.GetBytes("MiFare v1.1.3");
string hexString = "";
for (int i = 0; i < plainBytes.Length; i++)
hexString += plainBytes[i].ToString("X2") + " ";
Console.WriteLine(hexString); // Result: "4D 69 46 61 72 65 20 76 31 2E 31 2E 33"
// From Hex
hexString = hexString.Replace(" ", ""); // Remove whitespaces to have "4D69466172652076312E312E33"
byte[] hexBytes = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length / 2; i++)
hexBytes[i] = Convert.ToByte(hexString.Substring(2 * i, 2), 16);
string plainString = Encoding.ASCII.GetString(hexBytes);
Console.WriteLine(plainString); // Result: "MiFare v1.1.3"
Just, probably, should be needed to define correct Encoding.
Given a selection of bytes in a hex editor shows the file separates text with 00 bytes, it's a coloring file, with the material name, follolwed by a 3 byte hex code that determines colour. which is why it contains bytes like FF. the bytes are shown like this:
00 11 46 6F 6C 69 61 67 65 5F 45 76 65 72 67 72 65 65 6E 00 FF FF FF 00 0D 46 6F 6C 69 61 67 65 5F 42 69 72 63 68 00 80 A7 55
which translates into ascii as such:
Foliage_Evergreen�ÿÿÿ�
Foliage_Birch�€§U
How would I separate these bytes down into a list and convert them into text list of the bytes' values? I'm having trouble understanding how I'd go about doing it... this is what I'm doing right now:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
string line = System.IO.File.ReadAllText(openFileDialog1.FileName);
byte[] bytes = Encoding.ASCII.GetBytes(line);
List<string> listtext = line.Split('.').ToList();
listBox1.DataSource = listtext;
You shouldn’t do that, will crash for some files with encoding exception:
string line = System.IO.File.ReadAllText(openFileDialog1.FileName);
byte[] bytes = Encoding.ASCII.GetBytes(line);
Use File.ReadAllBytes instead, no need to read text then convert to bytes.
Then you’ll need to parse array of bytes into your records.
Based on your example data, your format uses 0 as field separator, and strings are prepended by their lengths. Here’s an example how to parse, untested:
static IEnumerable<(string, Color)> parse( byte[] data )
{
for( int p = 0; p < data.Length; )
{
// '\0'
if( 0 != data[ p++ ] ) throw new ApplicationException();
// String length byte
int length = data[ p++ ];
// The string; assuming the encoding is UTF8
string name = Encoding.UTF8.GetString( data, p, length );
p += length;
// '\0'
if( 0 != data[ p++ ] ) throw new ApplicationException();
// 3 color bytes, assuming the order is RGB
Color color = Color.FromArgb( 0xFF, data[ p ], data[ p + 1 ], data[ p + 2 ] );
p += 3;
yield return (name, color);
}
}
I am trying to write one program for serial port communication, where i need to send data packet with CRC, I am writing this code in C# language.
Below is sample packet which receiver is expecting with CRC.
10 02 B1 F0 3F 32 08 00 00 10 03 B4 5C
Second Data Packet : 10 02 B1 F0 3F 32 07 00 00 10 03 4D EE
10 - DLE Code
02 - STX
B1 F0 3F 32 08 00 00 are Data
10 - DLE
03 -ETX
B4 - CRC Lower Bye
5C - CRC -Upper Bye
CCIT : (Fx) = x16 + x12 + x5 + 1
Operational Initial Value: FFFFH
I tried some online CRC calculators but so far no luck, can anybody guide how to calculate CRC for above data(B1 F0 3F 32 08 00 00)? may be can suggest online calculator which can give me above output(B4 5C).
Thanks for your advise!
I found something that works, but it seems a bit strange.
First I xor'ed the two samples
10 02 B1 F0 3F 32 08 00 00 10 03 B4 5C
10 02 B1 F0 3F 32 07 00 00 10 03 4D EE
--------------------------------------
00 00 00 00 00 00 0F 00 00 00 00 F9 B2
This eliminates the initial CRC and final xor values, and led to using a bit reflected 0x11021 CRC. It appears that the CRC is using 8 bytes of data, including the trailing 0x10.
Using the CRC calculator linked to below, pick any CRC16, then click on custom and set parameters to: input reflected checked, output reflected checked, poly = 0x1021. There's not enough information to determine the initial value and final xor value without a different sized message. Using 8 bytes of data, some example options are: initial value = 0x5B08, final xor value = 0x0000, or initial value = 0xffff, final xor value = 0xdde5, or initial value = 0x0000, final xor value = 0xa169.
When using reflected parameters, the calculator bit reverses the init value (0x5B08 is 0x17DA bit reversed). For code, the 3 combos are {0x17da,0x0000}, (0xffff,0xdde5}, {0x0000,0xa169}. Poly = 0x8408 and is right shifting.
Using xx's to indicate ingored data, I got
xx xx B1 F0 3F 32 08 00 00 10 xx B4 5C
xx xx B1 F0 3F 32 07 00 00 10 xx 4D EE
Since the first two bytes are {10 02}, fixed values, they could be included, by changing the initial value. However, I wasn't able to include the ETX 03 value.
http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
Second hit I've found on the web but I copied it's contents here for reference:
using System;
public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
public class Crc16Ccitt {
const ushort poly = 4129;
ushort[] table = new ushort[256];
ushort initialValue = 0;
public ushort ComputeChecksum(byte[] bytes) {
ushort crc = this.initialValue;
for(int i = 0; i < bytes.Length; ++i) {
crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
}
return crc;
}
public byte[] ComputeChecksumBytes(byte[] bytes) {
ushort crc = ComputeChecksum(bytes);
return BitConverter.GetBytes(crc);
}
public Crc16Ccitt(InitialCrcValue initialValue) {
this.initialValue = (ushort)initialValue;
ushort temp, a;
for(int i = 0; i < table.Length; ++i) {
temp = 0;
a = (ushort)(i << 8);
for(int j = 0; j < 8; ++j) {
if(((temp ^ a) & 0x8000) != 0) {
temp = (ushort)((temp << 1) ^ poly);
} else {
temp <<= 1;
}
a <<= 1;
}
table[i] = temp;
}
}
}
The original link: http://sanity-free.org/133/crc_16_ccitt_in_csharp.html
how to convert a floating point 10 byte Hex string (Extended datatype in Delphi) to a C# datatype?
For example:
00 00 00 00 00 00 00 80 ff 3f is at Delphi 1
Was involved in same issue, sharing my solution somebody can find useful:
var extendedSize = 10;
var buf = new byte[extendedSize];
// Populate buffer with something like: { 0x00, 0x68, 0x66, 0x66, 0x66, 0x66, 0x66, 0xA2, 0x02, 0x40 } = 10.15
// Read(buf, extendedSize);
var sign = (buf[extendedSize - 1] & 0x80) == 0x80 ? -1 : 1;
buf[extendedSize - 1] = (byte)(buf[extendedSize - 1] & 0x7F);
var exp = BitConverter.ToUInt16(buf, extendedSize - 2);
var integral = (buf[extendedSize - 3] & 0x80) == 0x80 ? 1 : 0;
// Calculate mantissa
var mantissa = 0.0;
var value = 1.0;
var fractal = BitConverter.ToUInt64(buf, 0);
while (fractal != 0)
{
value = value / 2;
if ((fractal & 0x4000000000000000) == 0x4000000000000000) // Latest bit is sign, just skip it
{
mantissa += value;
}
fractal <<= 1;
}
return sign * (Math.Pow(2, exp - 16383)) * (integral + mantissa);
Code needs to be improved with NaN and Inf checks and probably "double" needs to be replaced by "decimal".
Ok, here is my solution:
Every string contains a factor byte at the second position. In my example the factor is ff.
Now I have to convert the string via Floating-Point Conversion to decimal and multiply with the factor byte to get the result.
Example:
3f ff 80 00 00 (32bit) -> remove the factor byte (ff) -> 3f 80 00 00 -> convert to decimal -> result: 1 -> multiply with factor -> 1 * 1 -> result: 1
I hope this was helpfully
I can say I don't know what I'm asking for help,because I don't know the format,but I've got a picture.
I have a byte[] array ,how do I convert it to that format below(in right)?
alt text http://img512.imageshack.us/img512/3548/48667724.jpg
Its not plain ascii.
It sounds like you'd like to take an array of bytes, and convert it to text (replacing characters outside of a certain range with "."s)
static public string ConvertFromBytes(byte[] input)
{
StringBuilder output = new StringBuilder(input.Length);
foreach (byte b in input)
{
// Printable chars are from 0x20 (space) to 0x7E (~)
if (b >= 0x20 && b <= 0x7E)
{
output.Append((char)b);
}
else
{
// This isn't a text char, so use a placehold char instead
output.Append(".");
}
}
return output.ToString();
}
or as a LINQy extension method (inside a static extension class):
static public string ToPrintableString(this byte[] bytes)
{
return Encoding.ASCII.GetString
(
bytes.Select(x => x < 0x20 || x > 0x7E ? (byte)'.' : x)
.ToArray()
);
}
(You could call that like string printable = byteArray.ToPrintableString();)
Use b.ToString("x2") to format a byte value into a two character hexadecimal string.
For the ASCII display, check if the value corresponds to a regular printable character and convert it if it is:
if (b >= 32 && b <= 127) {
c = (char)b;
} else {
c = '.';
}
Or shorter:
c = b >= 32 && b <= 127 ? (char)b : '.';
To do it on an array:
StringBuilder builder = new StringBuilder();
foreach (b in theArray) {
builder.Append(b >= 32 && b <= 127 ? (char)b : '.');
}
string result = builder.ToString();
This could be any number of encodings... try this test test script to see which of them print out:
Bl8s
Here is the script:
byte[] b = new byte[] {0x42, 0x6C, 0x38, 0x73 };
foreach (EncodingInfo ei in Encoding.GetEncodings())
{
Console.WriteLine("{0} - {1}", ei.GetEncoding().GetString(b), ei.Name);
}
[edit 2018:] Re-wrote the function from scratch to make the code much more efficient and fix some other problems. You can now also optionally specify a starting offset and number of bytes (starting from there) to display.
If you want the whole memory display, including the offset number, and left and right displays, you can do it like this: (32-byte width)
/// <summary> Returns a String where the specified bytes are formatted in a
/// 3-section debugger-style aligned memory display, 32-bytes per line </summary>
public static unsafe String MemoryDisplay(byte[] mem, int i_start = 0, int c = -1)
{
if (mem == null)
throw new ArgumentNullException();
if (i_start < 0)
throw new IndexOutOfRangeException();
if (c == -1)
c = mem.Length - i_start;
else if (c < 0)
throw new ArgumentException();
if (c == 0)
return String.Empty;
char* pch = stackalloc Char[32]; // for building right side at the same time
var sb = new StringBuilder((c / 32 + 1) * 140); // exact pre-allocation
c += i_start;
for (int i = i_start & ~0x1F; i < c;)
{
sb.Append(i.ToString("x8"));
sb.Append(' ');
do
{
if (i < i_start || i >= c) // non-requested area, or past the end
{
sb.Append(" ");
pch[i & 0x1F] = ' ';
}
else
{
var b = mem[i];
sb.Append(b.ToString("x2") + " ");
pch[i & 0x1F] = non_monospace(b) ? '.' : (Char)b;
}
}
while ((++i & 0x1F) != 0);
sb.Append(' ');
sb.AppendLine(new String(pch, 0, 32));
}
return sb.ToString();
}
The code uses the following helpers to determine which characters should be shown as 'dots' in right-hand part.
static readonly ulong[] _nmb =
{
0x00000000ffffe7ffUL,
0x8000000000000000UL,
0x00002000ffffffffUL,
0x0000000000000000UL,
};
static bool non_monospace(byte b) => (_nmb[b >> 6] & 1UL << b) != 0;
Output of the above function looks like this (character width is 138 columns, scroll to the right to see the "human-readable" part):
00000000 47 49 46 38 39 61 0f 00 0f 00 91 ff 00 00 00 00 c0 c0 c0 ff ff 00 00 00 00 21 f9 04 01 00 00 01 GIF89a...................!......
00000020 00 2c 00 00 00 00 0f 00 0f 00 00 02 2c 8c 0d 99 c7 91 02 e1 62 20 5a 79 ea bd 00 6d 89 69 8a f8 .,..........,.......b Zy...m.i..
00000040 08 e5 a7 99 e9 17 9d ac 24 a2 21 68 89 1e ac b4 d9 db 51 ab da c8 8c 1a 05 00 3b ........$.!h......Q.......;
Try: Encoding.Default.GetBytes
If that doesn't work, there are different types that you can specify (UTF-8, ASCII...)