ivandrofly

Hackish way to conver tom ascii code-point to raw int

Sep 16th, 2016
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConvertByteArray
  8. {
  9.     public static class ByteUtils
  10.     {
  11.         // By : Ivandro Ismael.
  12.         public static int ConvertByteCharsToInt()
  13.         {
  14.             var b = BitConverter.GetBytes(4);
  15.             byte[] bufferDigits = { Convert.ToByte('4'), Convert.ToByte('5'), Convert.ToByte('0') };
  16.  
  17.             // Note: Each single byte represent a single digit (radix 10 base)
  18.             // e.g: 450
  19.             // 0x34 = 4, 0x35 = 5; 0x30. (three bytes to present 450)
  20.  
  21.             byte[] newBuffer = new byte[3];
  22.             Array.Copy(bufferDigits, 0, newBuffer, 0, newBuffer.Length);
  23.             //if (BitConverter.IsLittleEndian)
  24.             //{
  25.             //    Array.Reverse(bfr);
  26.             //}
  27.             int mask = ~0x30;
  28.             newBuffer[0] = (byte)(newBuffer[0] & mask);
  29.             newBuffer[1] = (byte)(newBuffer[1] & mask);
  30.             newBuffer[2] = (byte)(newBuffer[2] & mask);
  31.  
  32.             // convert to decimal places
  33.             int intResult = newBuffer[0] * 100 + newBuffer[1] * 10 + newBuffer[2];
  34.             return intResult;
  35.         }
  36.     }
  37. }
  38. // Note: No refact were done!
Add Comment
Please, Sign In to add comment