I'm trying to implement a CRC-CCITT (XModem) check in php without success. Does anyone know how to do it? I expected crc16('test') will return 0x9B06.
Here is a simple bit-wise calculation of the XMODEM 16-bit CRC, in C:
#include <stdint.h>
unsigned crc16xmodem_bit(unsigned crc, void const *data, size_t len) {
if (data == NULL)
return 0;
while (len--) {
crc ^= (unsigned)(*(unsigned char const *)data++) << 8;
for (unsigned k = 0; k < 8; k++)
crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;
}
crc &= 0xffff;
return crc;
}
This was generated by my crcany software, which also generates byte-wise and word-wise versions for speed.
This can be easily converted to php.
Related
I need to do some work on data contained in legacy files. For this purpose, I need to read and write Turbo Pascal's 6-byte (48 bit) floating point numbers, from PHP. The Turbo Pascal data type is commonly known as real48 (specs).
I have the following php code to read the format:
/**
* Convert Turbo Pascal 48-bit (6 byte) real to a PHP float
* #param binary 48-bit real (in binary) to convert
* #return float number
*/
function real48ToDouble($real48) {
$byteArray = array_values( unpack('C*', $real48) );
if ($byteArray[0] == 0) {
return 0; // Zero exponent = 0
}
$exponent = $byteArray[0] - 129;
$mantissa = 0;
for ($b = 1; $b <= 4; $b++) {
$mantissa += $byteArray[$b];
$mantissa /= 256;
}
$mantissa += ($byteArray[5] & 127);
$mantissa /= 128;
$mantissa += 1;
if ($byteArray[5] & 128) { // Sign bit check
$mantissa = -$mantissa;
}
return $mantissa * pow(2, $exponent);
}
(adapted from)
Now I need to do the reverse: write the data type.
Note:
I'm aware of the answer to the question Convert C# double to Delphi Real48, but it seems awfully hacky and I would think a much cleaner solution is possible. AND my machine does not natively support 64-bits.
On a second look, the method posted in the answer to Convert C# double to Delphi Real48 cleaned up pretty nicely.
For future reference:
/**
* Convert a PHP number [Int|Float] to a Turbo Pascal 48-bit (6 byte) real byte representation
* #param float number to convert
* #return binary 48-bit real
*/
function doubleToReal48($double) {
$byteArray = array_values( unpack('C*', pack('d', $double)) ); // 64 bit double as array of integers
$real48 = array(0, 0, 0, 0, 0, 0);
// Copy the negative flag
$real48[5] |= ($byteArray[7] & 128);
// Get the exponent
$n = ($byteArray[7] & 127) << 4;
$n |= ($byteArray[6] & 240) >> 4;
if ($n == 0) { // Zero exponent = 0
return pack('c6', $real48[0], $real48[1], $real48[2], $real48[3], $real48[4], $real48[5]);
}
$real48[0] = $n - 1023 + 129;
// Copy the Mantissa
$real48[5] |= (($byteArray[6] & 15) << 3); // Get the last 4 bits
$real48[5] |= (($byteArray[5] & 224) >> 5); // Get the first 3 bits
for ($b = 4; $b >= 1; $b--) {
$real48[$b] = (($byteArray[$b+1] & 31) << 3); // Get the last 5 bits
$real48[$b] |= (($byteArray[$b] & 224) >> 5); // Get the first 3 bits
}
return pack('c6', $real48[0], $real48[1], $real48[2], $real48[3], $real48[4], $real48[5]);
}
I am building a neural net algorithm into C++ and using images for training data.
I need the data to be in an array of pixels represented by x,y|rgba values (A 2d array).
I have ImageMagick and the Magick++.h header plus compiler options all worked out.
I know the header library is working because I can :
int col = image.columns();
int row = image.rows();
cout << "COLS: " << col << "ROWS : " << row << endl;
My images are 32x32 and the result of the compiled program is:
root#jarvis:~/Documents/Programming/C++/ImageMagick# ./magick
COLS: 32 ROWS : 32
I just cannot seem to access the pixel values. I am not so fluent in C++ as I'd like but an example in PHP would be a function like this:
Function ImageToVector($Filename){
// open an image
$im = imagecreatefrompng($Filename);
$width = imagesx($im);
$height = imagesy($im);
$i = 0;
// get a color value for each pixle in width/height matrix
for ($x = 0; $x < $width; $x++){
for($y =0; $y < $height; $y++){
$color_index = imagecolorat($im,$x,$y);
// make it human readable and store it in the inputVector array.
//each pixel is read into the array one after the other making it a single inputVector
//later, we should know the dimensions of our input images (which should all be the same size in pixels).
//so we can lay it back down layer by layer if we wish to reconstruct the image from the rgba data in our input vectors later
$inputVector[$i] = imagecolorsforindex($im, $color_index);
$i++;
}
}
$color_tran = imagecolorsforindex($im, $color_index);
//return the input vector for entire image as an array
return ($inputVector);
}
$i=0;
$InputVector[$i] = ImageToVector("Example0.png");
My cpp file is this:
#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"
using namespace std;
using namespace Magick;
int main()
{
Image image("a.png");
int col = image.columns();
int row = image.rows();
PixelPacket *pixels = image.getPixels(0,0,col,row);
cout << "VALUE X: " << col << " ROWS : " << row << endl;
return 0;
}
My work around currently is to use the php function as is with a web form used to store the set of image data (input vectors) in a db. Then I can at least access that table from the C++ side.
I know how to do that much already. I was just kind of hoping for a more elegant solution on the import side. Thanks in advance everyone!
EDIT:
To access the pixel data I have tried things like
#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"
using namespace std;
using namespace Magick;
int main()
{
Image image("a.png");
int w = image.columns();
int h = image.rows();
PixelPacket *pixels = image.getPixels(0, 0, w, h);
int row = 0;
int column = 0;
Color color = pixels[w * row + column];
int x = pixels[0];
cout << "COLS: " << x << endl;
return 0;
}
or int x = pixels[0][0];
with either pixels[0][0] or pixels[0]
root#jarvis:~/Documents/Programming/C++/ImageMagick# ./compile_main.sh
main.cpp: In function âint main()â:
main.cpp:20:17: error: cannot convert âMagickCore::PixelPacket {aka MagickCore::_PixelPacket}â to âintâ in initialization
root#jarvis:~/Documents/Programming/C++/ImageMagick# ./compile_main.sh
main.cpp: In function âint main()â:
main.cpp:20:20: error: no match for âoperator[]â in â* pixels[0]â
root#jarvis:~/Documents/Programming/C++/ImageMagick#
I edited your code a bit, just to make it clear
#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"
using namespace std;
using namespace Magick;
int main()
{
Image image("a.png");
int w = image.columns();
int h = image.rows();
PixelPacket *pixels = image.getPixels(0, 0, w, h);
int row = 0;
int column = 0;
Color color = pixels[0]; // get first pixel color as an example
unsigned int red = color.redQuantum;
unsigned int blue = color.blueQuantum;
unsigned int green = color.greenQuantum;
unsigned int alpha = color.alphaQuantum;
cout << "RED:" << red << "BLUE:" << blue << "GREEN:" << green << "ALPHA:" << alpha << endl;
return 0;
}
#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"
using namespace std;
using namespace Magick;
int main()
{
Image image("a.png");
int w = image.columns();
int h = image.rows();
PixelPacket *pixels = image.getPixels(0, 0, w, h);
int row = 0;
int column = 0;
for(int i = 0; i < w*h; i++){
Color color = pixels[i]; // get first pixel color as an example
float red = color.redQuantum();
float blue = color.blueQuantum();
float green = color.greenQuantum();
float alpha = color.alphaQuantum();
//translate the bit value into standard rgba(255,255,255) values
if (red != 0){ red = red/256;} //if the (r)gba vector is 0, don't divide by 256
if (blue != 0){ blue = blue/256;} //if the r(g)ba vector is 0, don't divide by 256
if (green !=0) { green = green/256;}//if the rg(b)a vector is 0, don't divide by 256
if (alpha !=0) { alpha = alpha/256;}//if the rgb(a) vector is 0, don't divide by 256
//output red,green,blue values
cout << "R: " << red << " G: " << green << " B :" << blue << " A:" << alpha << endl;
}
return 0;
}
**edited for clearer output
example output:
R: 110.43 G: 110.43 B :110.43 A:0
R: 114.445 G: 114.445 B :114.445 A:0
R: 117.457 G: 118.461 B :118.461 A:0
R: 121.473 G: 121.473 B :122.477 A:0
R: 124.484 G: 125.488 B :125.488 A:0
R: 127.496 G: 128.5 B :128.5 A:0
R: 130.508 G: 130.508 B :130.508 A:0
Of course I will have to round those numbers to an int but thank you for your help here!
I am creating and then writing data to a file (a new 'ESRI Shape file') using PHP, fopen, fseek, pack etc. The file spec is here http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.
The file spec states that the data written needs to be in a combination of the following:
Integer: Signed 32-bit integer (4 bytes) - Big Endian
Integer: Signed 32-bit integer (4 bytes) - Little Endian
Double: Signed 64-bit IEEE double-precision floating point number (8 bytes) - Little Endian
I cant seem to find a pack() format that allows for these formats. I don't want to use a machine dependent format as this code may be running on a variety of platforms.
Can anyone advise on what format (or combination of formats) I need to use for these 3 formats?
Many thanks,
Steve
You could check the endianness of the machine running the code and reverse the bytes manually as necessary. The code below should work, but you will only be able to convert one int or float at a time.
define('BIG_ENDIAN', pack('L', 1) === pack('N', 1));
function pack_int32s_be($n) {
if (BIG_ENDIAN) {
return pack('l', $n); // that's a lower case L
}
return strrev(pack('l', $n));
}
function pack_int32s_le($n) {
if (BIG_ENDIAN) {
return strrev(pack('l', $n));
}
return pack('l', $n); // that's a lower case L
}
function pack_double_be($n) {
if (BIG_ENDIAN) {
return pack('d', $n);
}
return strrev(pack('d', $n));
}
function pack_double_le($n) {
if (BIG_ENDIAN) {
return strrev(pack('d', $n));
}
return pack('d', $n);
}
If PHP doesn't support it, you could implement your own.
function pack_int32be($i) {
if ($i < -2147483648 || $i > 2147483647) {
die("Out of bounds");
}
return pack('C4',
($i >> 24) & 0xFF,
($i >> 16) & 0xFF,
($i >> 8) & 0xFF,
($i >> 0) & 0xFF
);
}
function pack_int32le($i) {
if ($i < -2147483648 || $i > 2147483647) {
die("Out of bounds");
}
return pack('C4',
($i >> 0) & 0xFF,
($i >> 8) & 0xFF,
($i >> 16) & 0xFF,
($i >> 24) & 0xFF
);
}
The double-precision LE is much harder. Supporting quad-precision system would involve packing the number using d, converting it to a binary string, splitting the binary into fields, truncating the fields to the right size if they're too large, concatenating the fields, then converting from binary to bytes.
I'm trying to establish communication between a website and an Arduino. I need to authenticate all the messages from my website to the Arduino, so I have found that the less time expensive way is using XTEA cryptography.
My PHP code for the website is:
mcrypt_encrypt(MCRYPT_XTEA, 'qwertyuiasdfghjk', 'asdfasdf', MCRYPT_MODE_ECB);
where "qwertyuiasdfghjk" is a 128 bits key and "asdfasdf" is a 64 bits message.
On the Arduino side I'm using:
void _xtea_dec(void* dest, const void* v, const void* k)
{
uint8_t i;
uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
for(i=0; i<32; i++)
{
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
sum -= delta;
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
}
((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
}
where the parameters are:
char dest[9]; //Destination
char v[9]; //Encrypted message
char k[17]; //Key
but my decrypted message is far away from the original message... It still having 64 bits, but it is totally different...
What should I do?
(This is the first time that I ask a question here, usually I all my questions are solved somewhere in Stack Overflow...)
Most likely your cipher keys are different. Make sure they are the same in both ends.
C:
// "annoying monkey"
uint32_t key[4] = {0x6f6e6e61, 0x676e6979, 0x6e6f6d20, 0x0079656b };
PHP:
mcrypt_encrypt(MCRYPT_XTEA, 'annoying monkey', 'data', MCRYPT_MODE_ECB);
As far as I remember, the XTEA specification did not provide test vectors and your code does not seem to care about endianness. Most probably it is a matter of key or data assumed/being in the wrong endian. Look at the implementation of mcrypt_encrypt function in the PHP source.
I'm working on a webapp where I want to match some crc32 values that have been generated server side in PHP with some crc32 values that I am generating in Javascript. Both are using the same input string but returning different values.
I found a crc32 javascript library on webtoolkit, found here. When I try and match a simple CRC32 value that I generate in PHP, for the life of me I can't generate the same matching value in the Javascript crc32 function. I tried adding a utf-8 language encoding meta tag to the top of my page with no luck. I've also tried adding a PHP utf8_encode() around the string before I enter it into the PHP crc32 function, but still no matching crc's ....
Is this a character encoding issue? How do I get these two generated crc's to match? Thanks everyone!
/**
*
* Javascript crc32
* http://www.webtoolkit.info/
*
**/
function crc32 (str) {
function Utf8Encode(string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
};
str = Utf8Encode(str);
var table = "00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D";
if (typeof(crc) == "undefined") { crc = 0; }
var x = 0;
var y = 0;
crc = crc ^ (-1);
for( var i = 0, iTop = str.length; i < iTop; i++ ) {
y = ( crc ^ str.charCodeAt( i ) ) & 0xFF;
x = "0x" + table.substr( y * 9, 8 );
crc = ( crc >>> 8 ) ^ x;
}
return crc ^ (-1);
};
I actually needed this exact same functionality recently for a work project. So this is what I have been able to figure out.
The reason they do not match is because the Javascript implementation is not working on bytes. Using str.charCodeAt(i) will not always return a value 0-255 (in the case of Unicode characters it'll return a potentially much larger number). The Utf8Encode function might be trying to work around this but I don't think it will work for any given binary data.
Using the stringToBytes function from this question: Reading bytes from a JavaScript string will help convert data in a string to bytes. Though I did experience some completely dropped bytes, which seemed more to do with how the string was being stored in the browser than the function itself, it may work in your situation however.
One other hiccup you might have is that PHP's crc32 function will return an unsigned 32bit integer. The above function will return a signed 32bit integer. So given those two things here is the function I ended up with:
crc32 = {
table:"00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D",
//This will probably match php's crc32
genBytes:function(str, crc ) {
var bytes = stringToBytes(str)
if( crc == window.undefined ) crc = 0;
var n = 0; //a number between 0 and 255
var x = 0; //a hex number
crc = crc ^ (-1);
for( var i = 0, iTop = bytes.length; i < iTop; i++ ) {
n = ( crc ^ bytes[i] ) & 0xFF;
x = "0x" + this.table.substr( n * 9, 8 );
crc = ( crc >>> 8 ) ^ x;
}
crc = crc ^ (-1)
//convert to unsigned 32-bit int if needed
if (crc < 0) {crc += 4294967296}
return crc;
}
}
Also if you happen to be using Adobe Air, you can just use a ByteArray and avoid the stringToBytes function.