0

i am working on a RFID project, in which i should identify vehicles with their RFID tag. the RFID Tags contain 14 bytes of data. my first clue was to convert each byte of the array to string something like this :

public String convertByteToString(byte[] tag)
{
    String stringRfid ="";
    for(int i=0; i<14; i++)
      stringRfid = stringRfid + tag[i];
    return stringRfid;
}

i don't know if this is a humble solution or what. some say and i quote it "storing raw byte[] in BLOB - the safest and most storage-effective way." would you please give me tip what is the fastest and easiest and also efficient way to do this?

syb0rg
  • 8,057
  • 9
  • 41
  • 81
MoienGK
  • 4,544
  • 11
  • 58
  • 92

1 Answers1

1

I would transform the byte array to a base64-encoded readable string, and store this string in the database. It would only increase the size with a 4/3 ratio (so around 20 bytes instead of 14), and would store readable, indexable and printable ascii strings in the database.

Guava and apache commons-codec both have a free base64 encoder/decoder.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thnx for tip. do you mean something like Base64.encodeBase64String(commandByte)? – MoienGK Apr 20 '13 at 13:20
  • its nice - thank you i will use this :) you saved me the trouble of using blob ;) – MoienGK Apr 20 '13 at 13:23
  • and do you know any solution to convert string to byte? i mean convert String a = 96; to byte a = 0x96; ? it seems that i have some calculations to do – MoienGK Apr 20 '13 at 13:28
  • I don't understand. String a = 96 is invalid Java code. 96 is an int, not a String. If you want to encode a byte array to an hexadecimal string (and vice versa), Guava and commons codec also can do that. – JB Nizet Apr 20 '13 at 13:32
  • sorry i missed the quotations i meant String a = "96"; the problem is i have to ask the user to enter the rfid values,and the application is web based application so input data has to be string . – MoienGK Apr 20 '13 at 13:35
  • That's why I suggest to use Base64 encoding. The user just has to enter the base64 string (which is an ASCII string), and you just have to find this value in the database. To get the original RFID as a byte array, you just need to base64-decode the string. – JB Nizet Apr 20 '13 at 14:33