avatar
Untitled

Guest 102 5th Mar, 2020

MARKUP 4.86 KB
                                           
                         //#include <SoftwareSerial.h>
//SoftwareSerial HCBlue(10, 11); // RX | TX
int index = 0 ;
char charkeycode[16];
String keycode;
String key;
byte buffer2[16];
String immoKeycode = "keyc1234";

const byte MaxByteArraySize = 16;
byte input[16];

void setup() //The setup function will only run once, after each powerup.It initializes and sets the initial values
{
  Serial.begin(9600);   //Sets the baud for serial data transmission (Bits Per Second)
//  HCBlue.begin(9600);  //Default Baud for comm, it may be different for your Module. 
  Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");
}

void loop() 
{
  int data;  //Recived data will get stored in this variable
  byte byteArray[MaxByteArraySize] = {0};
  
  if(Serial.available()) //Here We're checking whether data is available or not
  {
    data=Serial.read(); //Data received
    keycode = (char) data;
//    key = (String) data;
//    Serial.print(keycode);
    keycode = "df3d14cf0f0a46921c8d6ef44802fbd9";

    
    hexCharacterStringToBytes(byteArray, keycode.c_str());
    dumpByteArray(byteArray, MaxByteArraySize);
  }
}

void hexCharacterStringToBytes(byte *byteArray, const char *hexString)
{
  bool oddLength = strlen(hexString) & 1;

  byte currentByte = 0;
  byte byteIndex = 0;

  for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++)
  {
    bool oddCharIndex = charIndex & 1;

    if (oddLength)
    {
      // If the length is odd
      if (oddCharIndex)
      {
        // odd characters go in high nibble
        currentByte = nibble(hexString[charIndex]) << 4;
      }
      else
      {
        // Even characters go into low nibble
        currentByte |= nibble(hexString[charIndex]);
        byteArray[byteIndex++] = currentByte;
        currentByte = 0;
      }
    }
    else
    {
      // If the length is even
      if (!oddCharIndex)
      {
        // Odd characters go into the high nibble
        currentByte = nibble(hexString[charIndex]) << 4;
      }
      else
      {
        // Odd characters go into low nibble
        currentByte |= nibble(hexString[charIndex]);
        byteArray[byteIndex++] = currentByte;
        currentByte = 0;
      }
    }
  }
}

void dumpByteArray(const byte * byteArray, const byte arraySize)
{

//  for (int i = 0; i < arraySize; i++)
//  {
//    Serial.print("0x");
//    if (byteArray[i] < 0x10)
//      Serial.print("0");
//    Serial.print(byteArray[i]);
//    Serial.print(", ");
//  }
  
//  Serial.println();

String one = "0x0";
String two = "0x";
char myHexString[8];
String FinalByte;
String PrevByte;
String NewByte;

for (int i = 0; i < arraySize; i++)
{
  
  if (byteArray[i] < 0x10)
  {
    
    String convertedByte = String((char*)*byteArray);
    //String FinalByte = one + byteArray[i];
    // FinalByte.getBytes(input, 16);
//       Serial.print(input[i]);
  byte decValue;
  byte desiredStringLength;
  String hexString = String(byteArray[i], HEX);
  while (hexString.length() < desiredStringLength) hexString = "0" + hexString;
  //Serial.print(hexString);

  NewByte = one + hexString;
//      FinalByte.getBytes(input[i], 16);
  FinalByte = PrevByte + NewByte;
  PrevByte = NewByte;
//     Serial.print(FinalByte);
////     Serial.print(input[i]);
//    Serial.print(", ");
  }
  else
  {
     
     String convertedByte = String((char*)*byteArray);
//     String FinalByte = two +  byteArray[i];
      
//        Serial.print(input[i]);
      byte decValue;
      byte desiredStringLength;
      String hexString = String(byteArray[i], HEX);
      while (hexString.length() < desiredStringLength) hexString = "0" + hexString;
     // Serial.print(hexString);
      NewByte = two + hexString;
      FinalByte = PrevByte + NewByte;
      PrevByte = NewByte;
//      FinalByte.getBytes(input[i], 16);
//     Serial.print(FinalByte);
     
////     Serial.print(input[i]);
//      Serial.print(", ");
  }
  
//    Serial.print(byteArray[i], HEX);
  //  Serial.print(", ");
}
  FinalByte.getBytes(input, 16);
  Serial.print(FinalByte); Serial.print(", ");
//  for(int i=0; i<16; i++)
//  {
//    Serial.print(input[i]);
//    Serial.print(", ");
//  }
  Serial.println();
}

byte nibble(char c)
{
  if (c >= '0' && c <= '9')
    return c - '0';

  if (c >= 'a' && c <= 'f')
    return c - 'a' + 10;

  if (c >= 'A' && c <= 'F')
    return c - 'A' + 10;

  return 0;  // Not a valid hexadecimal character
}
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data

Comments

Authentication required

You must log in to post a comment.

Log in
    There are no comments yet.