473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

From binary to hex in wiegand

Hello,

I’m developing an application that needs to send a wiegand signal. I’m using a serial to wiegand converter for that. I’m having a problem though, with sending serial commands.

The documentation specifies how the frame should look. In the frame is a data portion. This contains the wiegand data that is to be converted. In the picture below is the example from the documentation.



The top red part is the 26bit wiegand data. The fist bit is parity followed by 8 bits of facility code followed by 16 bits of id code ending with another parity bit. The bottom red part shows how that is going to look in the frame.

So my question is: how do I get a user id like 15 to the binary data in the top and then to the hexadecimal data shown below using c#.

With kind regards,
Ben
Oct 27 '09 #1
6 10806
GaryTexmo
1,501 Expert 1GB
Have a look here...

http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx
http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

You can supply a base to some of the methods of this class. I believe Convert.ToString(number, base) will get you the string representation.
Oct 27 '09 #2
jensa
8
Hi Ben,

The static class BitConverter will help you a lot (you can also combine bit/byte handling with pointers in unsafe code), check out MSDN docs. But I am not really sure what you are looking to accomplish so I cannot help you out in detail before I get more info what you exactly need to do. But I will give my best to shortly get you some info of bit/byte handling in C# managed code.

Let's say that you have a byte array:

Expand|Select|Wrap|Line Numbers
  1. // Let's create the frame with 20 bytes as your image show
  2. byte[] frame = new byte[20];
  3.  
  4. // To modify/set single bytes, example setting first byte to hex
  5. // 0x55
  6. frame[0] = 0x55;
  7.  
  8. // or use int values and cast them to byte, remember that 0-255 is only valid
  9. // otherwise the result will be truncated.
  10. frame[0] = (byte)85;
  11.  
  12. // if you need to put a unsigned 16-bit integer into the frame you can use.
  13. // Observe order, big endian or little endian
  14. byte[] value = BitConvert.GetBytes(UInt16.MaxValue);
  15. frame[1] = value[0];
  16. frame[2] = value[1];
  17. // above method enables you to set big endian or little endian, e.g MSB / LSB order
  18.  
  19. //To modify single bits inside 1 byte you can do bit OR.
  20. frame[2] = frame[2] | 0x01;
For bitwise methods see,
http://www.blackwasp.co.uk/CSharpLogicalBitwiseOps.aspx

EDIT:
You can also use a shifting method to "shift in values above 1-byte" then you don't need to use BitConverter.GetBytes.


Regards,

Jens
Oct 29 '09 #3
Well I’m using a converter for serial to wiegand. The command u see in the picture is used to send a wiegand frame. A wiegand frame send a id to a other decice. The id is composed from the 16 bits user code portion of the bits. In the picture I believe there sending the id 1943. So I made a program that converts a int to a string of bits and then converts that to a string of hex and then converts it to a byte array that I send. I have been testing this all day yesterday but it does not seem to work. I think there something wrong with my conversion. I user the code below:

Expand|Select|Wrap|Line Numbers
  1.  public override byte[] CommandArray
  2. {
  3. get
  4. {
  5. byte[] returnVal = new byte[FRAME_SIZE];
  6.  
  7. // Start
  8. returnVal[0] = Commands.SYNC;
  9. returnVal[1] = Commands.SYNC;
  10.  
  11. // Command
  12. returnVal[2] = cmd;
  13.  
  14. // Size
  15. returnVal[3] = 0x1A;
  16.  
  17. // Data
  18. Factory.UserIdToWiegandFrame(id).CopyTo(returnVal, 4);
  19.  
  20. // Wiegand pulse width
  21. returnVal[16] = 0xFF;
  22.  
  23. // Wiegand bit period
  24. returnVal[17] = 0x00;
  25.  
  26. // Cariage return
  27. returnVal[19] = Commands.CR;
  28.  
  29. //string[] test = new string[FRAME_SIZE];
  30. //for (int i = 0; i < returnVal.Length; i++)
  31. // test[i] = Convert.ToString(returnVal[i], 16);
  32.  
  33. return returnVal;
  34. }
  35. }
  36.  
This is the factory code:

Expand|Select|Wrap|Line Numbers
  1.  public static byte[] UserIdToWiegandFrame(int userId)
  2. {
  3. StringBuilder sb = new StringBuilder();
  4. StringBuilder id = new StringBuilder();
  5.  
  6. // Parity bit
  7. sb.Append("1");
  8.  
  9. // Facillity code
  10. sb.Append("00000000");
  11.  
  12. // Id toevoegen
  13. id.Append(Convert.ToString(userId, 2));
  14.  
  15. // 0 inserten tot het id 16 bit lang is.
  16. while (id.Length < 16)
  17. {
  18. id.Insert(0, "0");
  19. }
  20.  
  21. sb.Append(id.ToString());
  22.  
  23. // Parity bit
  24. sb.Append("0");
  25.  
  26. string temptest = sb.ToString();
  27.  
  28. // 0 toevoegen tot de frame 96 bit lang is.
  29. while (sb.Length < 96)
  30. {
  31. sb.Append("0");
  32. }
  33.  
  34. string testagain = BinToHex(sb.ToString());
  35.  
  36. int tmp = 0;
  37. return GetBytes(BinToHex(sb.ToString()), out tmp);
  38. }
  39.  
  40.  public static string BinToHex(string bin)
  41. {
  42. StringBuilder hex = new StringBuilder();
  43. int remainer = bin.Length % 4;
  44.  
  45. for (int i = 0; i < remainer; i++)
  46. {
  47. bin.Insert(0, "0");
  48. }
  49.  
  50. //return Convert.ToString(Convert.ToInt32(bin, 2), 16);
  51.  
  52. int count = bin.Length / 4;
  53.  
  54. for (int j = 0; j < count; j++)
  55. {
  56. int index = ((j * 4 - 1) < 0) ? 0 : (j * 4);
  57. //Console.WriteLine(string.Format("BIN: {0}", bin.Substring(index, 4)));
  58. //Console.WriteLine(string.Format("DEC: {0}", Convert.ToInt32(bin.Substring(index, 4), 2)));
  59. //Console.WriteLine(string.Format("HEX: {0}", Convert.ToString(Convert.ToInt32(bin.Substring(index, 4), 2), 16)));
  60. hex.Append(Convert.ToString(Convert.ToInt32(bin.Substring(index, 4), 2), 16));
  61. }
  62.  
  63. return hex.ToString();
  64. }
  65.  
  66.  public static byte[] GetBytes(string hexString, out int discarded)
  67. {
  68. discarded = 0;
  69. string newString = "";
  70. char c;
  71. // remove all none A-F, 0-9, characters
  72. for (int i = 0; i < hexString.Length; i++)
  73. {
  74. c = hexString[i];
  75. if (IsHexDigit(c))
  76. newString += c;
  77. else
  78. discarded++;
  79. }
  80. // if odd number of characters, discard last character
  81. if (newString.Length % 2 != 0)
  82. {
  83. discarded++;
  84. newString = newString.Substring(0, newString.Length - 1);
  85. }
  86.  
  87. int byteLength = newString.Length / 2;
  88. byte[] bytes = new byte[byteLength];
  89. string hex;
  90. int j = 0;
  91. for (int i = 0; i < bytes.Length; i++)
  92. {
  93. hex = new String(new Char[] { newString[j], newString[j + 1] });
  94. bytes[i] = HexToByte(hex);
  95. j = j + 2;
  96. }
  97. return bytes;
  98. }
  99.  
Would the bitconverter give me a different result or the same?
Oct 30 '09 #4
jensa
8
Hi,

So if I understand you right you would like to compose the 16-bits ID of the 26-bits in the frame. You current method i based on convertering the 26-bits into a string of bits where you then can modify the bits and last recreating the 26-bits (hex). If this is the case (and I understand right) I would not recommend this.

You should shift data into position, example if you are using and UInt16 (ushort). With shifting you are "extracting the 1-byte" values and moves them out/in.

frame[firstpos] = value << 8; // shift MSB 8 bits to left
frame[secondpos] = value << 16; // shift LSB 16 bits to left

But if you just need to modify the 16-bit you can just also convert these "bytes" into a UInt16 and go and modify it and they recreate the bytes by using BitConverter.GetBytes(of your UInt16).


Regards,

Jens
Oct 30 '09 #5
So I could skip the whole to string and to hex method by just shifting the bits next to each other? So to complete the entire frame 0f 36 bits I could just shift the first parity bit + 8 bit facility code + 16 bit usercode + parity bit and the convert it to a byte array?
Oct 30 '09 #6
jensa
8
Yes. I also made a mistake in the last example, you need to cast a ushort to byte when shifting also, otherwise a compile error will be shown. Shifting is the right method if you need to control different sets of bits and put/unput the data into byte array.

Regards,

Jens
Oct 30 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rakesh Sinha | last post by:
Hi, I am writing this application in C++. It reads data from binary files. My current requirement is that: Given a positive number N, I have to read in N bytes from the input stream (which is...
3
by: Matt Laver | last post by:
Hi, I have a binary file that I'm currently reading byte by byte using code similiar to: string FileName = @"c:\myFile.dat"; FileStream fs = new FileStream(FileName, FileMode.Open,...
5
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
6
by: Klaus Jensen | last post by:
Hi I have some binary files (jpeg), which contain a lot of image-data - and some embedded XML (XMP actually). If I view the file in a hex-editor, there is a lot of binary data - and then in...
1
by: Pedro Leite | last post by:
hello. got stalled at at point that i can no longer get my thoughts together. the point, is in classic asp, stream an excel file from a firebird database. temporarly save it if necessary...
8
by: gsmith | last post by:
Hello, I am developing a video recording (mjpeg) system which records large binary files. I need to read the JPEG data from the binary file and create a bitmap object to be displayed....
4
by: Florence | last post by:
How can a binary file be distinguished from a text file on Windows? Obviously I want a way that is more sophisicated that just looking at the dot extention in the filename. I want to write...
1
by: Asking | last post by:
Hi all, how to save/read content of a RichTextBox to/from binary field in Ms.SQL Server ? I want to do save and read operation... I couldn't it Thank you
1
eyalbi007
by: eyalbi007 | last post by:
Hi all, I have a flow of things I need to do, and by now I couldn't find a covenient (or any) way doing it: 1. Loading image from binary file. The image is a 3D image, and I know each slice...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.