기본 콘텐츠로 건너뛰기

11월, 2017의 게시물 표시

C# - dynamic 변수 Type 알아내기

private string gettype(dynamic value) {     if (value is Boolean)          return "Boolean";     if (value is string)          return "string";     if (value is double)          return "double";     return "type is undefined";      }      MessageBox.Show(gettype("abcdefg"));                 double value = 123; MessageBox.Show(gettype(value)); MessageBox.Show(gettype(true));

C# - string 자릿수 맞추기

1. 자릿수 맞추기 Console.WriteLine( "{0,22} {1,22}" , byteValue.ToString( "D8" ), byteValue.ToString( "X8" )); ToString("D8") => 정수 8자리 맞추기. 빈공간 0으로 채움. ToString("X8") => 16진수 8자리 맞추기. 빈공간 0으로 채움. 2. 특정 형식으로 자릿수 맞추기 string fmt = "00000000.##" ; int intValue = 1053240; decimal decValue = 103932.52m; float sngValue = 1549230.10873992f; double dblValue = 9034521202.93217412; // Display the numbers using the ToString method. Console.WriteLine(intValue.ToString(fmt)); Console.WriteLine(decValue.ToString(fmt)); Console.WriteLine(sngValue.ToString(fmt)); Console.WriteLine(dblValue.ToString(fmt)); Console.WriteLine(); // Display the numbers using composite formatting. string formatString = " {0,15:" + fmt + "}" ; Console.WriteLine(formatString, intValue); Console.WriteLine(formatString, decValue); Console.WriteLine(formatString, sngValue); Console.WriteL...

C# - Server Socket - ReadCallback(IAsyncResult ar)

public void ReadCallback(IAsyncResult ar)         {             string content = string.Empty;             Socket handler = null;             try             {                 StateObject state = ar.AsyncState as StateObject;                 handler = state.workSocket;                 int bytesRead = handler.EndReceive(ar);                 if (bytesRead > 0)                 {                     string receive_data = (Encoding.ASCII.GetString(state.buffer, 0, bytesRead));                     irocv.IROCV_Recv(receive_data);    ...

C# - Int Byte Byte[] Hex 정리

INT / HEX간 변환방법  Int -> Hex : String str = 123.ToString("X");//"7B" Hex -> Int : int num = int.Parse("123", System.Globalization.NumberStyles.HexNumber); // 291 Hex -> Int 다른방법 : string hex = "ABC"; int myInt = Convert.ToInt32(hex, 16);  //Convert.ToInt32("바꾸고 싶은 숫자", "진수");, 여기서 진수는 2,8,10,16진수만 가능  출처 :  Hex Handling in C# Byte => HEX 변환방법 byte b = 32; string hexval = b.ToString ('X2');//첫번째 방법 Console.WriteLine("{0:X}",b);//두번째 방법 출처 :  Byte to Hex Byte[] => HEX 변환방법 //첫번째 방법 Byte[] Bytes = {0xFF, 0xD0, 0xFF, 0xD1};// to "FFD0FFD1" String byteToHex(Byte[] ba) { StringBuilder sb = new StringBuilder(ba.Length * 2); foreach (byte b in ba) {        sb.AppendFormat("{0:x2}", b) } return sb.ToString(); } //두번째 방법 Byte[] Bytes = {0xFF, 0xD0, 0xFF, 0xD1}// to "FF-D0-FF-D1"  BitConverter.ToString(Bytes);//-가 보기 흉하다면 replace하면 된다. //세번째 방법(가장 확실한 방법같음) public sta...

C# - String to Byte, Byte to String

// 바이트 배열을 String으로 변환  private string ByteToString(byte[] strByte)  {      string str = Encoding.Default.GetString(StrByte); return str;  }  // String을 바이트 배열로 변환  private byte[] StringToByte(string str)  {      byte[] StrByte = Encoding.UTF8.GetBytes(str);      return StrByte;  } 출처:  http://zephie.tistory.com/11  [zephie DebugHolic]

C# - get PLC Data

private byte[] getPLCData()         {             byte[] data1 = new byte[1024];             for(int i = 0; i < data1.Length; i++)             {                 if (i == 0) data1[i] = 0xD0;                 else if (i == 7) data1[i] = 0x01;                 else if (i == 8) data1[i] = 0x02;                 else if (i == 11) data1[i] = 0x01; // byte start 4200                 // 13, 14 double : remeasure alarm                 //=> 주소 1에 byte가 2개가 들어 가므로                 //=> 4202는 4200이 11부터 시작 할 때 15, 16번째의 값이 됨.                 // 15가 [0], 16이 [1...

C# - Multi Port Socket Programming (from MSDN Forum)

using System ; using System . Windows . Forms ; using System . Net ; using System . Net . Sockets ; using System . Collections . Generic ; namespace DefaultNamespace { public partial class SocketServer : Form { private System . Windows . Forms . Label label3 ; private System . Windows . Forms . Label label2 ; private System . Windows . Forms . RichTextBox richTextBoxReceivedMsg ; private System . Windows . Forms . TextBox textBoxPort ; private System . Windows . Forms . Label label5 ; private System . Windows . Forms . Label label4 ; private System . Windows . Forms . TextBox textBoxMsg ; private System . Windows . Forms . Button buttonStopListen ; private System . Windows . Forms . Label label1 ; private System . Windows . Forms . RichTextBox richTextBoxSendMsg ; private System . Windows . Forms . TextBox textBoxIP ; private System . Windows . Forms . Button buttonStartListen ; private ...