Integer to hex to byte
![confused.gif](http://forums.parallax.com/images/smilies/confused.gif)
·
If anyone can help, I would apprceiate it:
I am trying to convert an integer to hex
always with a prefix of "0x"
then converted to a·sendable byte
If my integer is 31, I need to convert it to a·hex 0x1F.
Then·hex 0x1F has to be converted to type byte so I
can send it like this:
fSend.sendByte(byte);
Any sample code for converting any integer < 255
to this byte would be appreciated
Thanks
Jeff (trytryagain)
·
Comments
Either send '1', then send 'F', or send 31 directly as in send(31)
regards peter
My problem is that the receiving processor
is expecting a series of hex commands in order.
It has hex check sums and some other
complicated issues.
The only way the program acknowledges the
transmission is when I send it like this:
fSend.sendByte(0x40); //Start Packet
fSend.sendByte(0x02); //Save System Parameter Command
fSend.sendByte(0x12); <= RIGHT HERE IS WHERE I NEED TO SEND MY CONVERTED DATA
fSend.sendByte(0x00); //Some other stuff
fSend.sendByte(0x00);········· |
fSend.sendByte(0x00);········· |
fSend.sendByte(0x00);········· |
fSend.sendByte(0x00);········· |
fSend.sendByte(0x00);········· |
fSend.sendByte(0x00);········· |
fSend.sendByte(0x00);··········|
fSend.sendByte(0x57); //Check Sum
fSend.sendByte(0x0A); //End Packet
I know how to convert the int to hex but
I can't figure out how to get that
into a variable of type byte with the 0x concatinated
as a prefix.
Any other ideas Peter? ·I'm really stuck
Thanks,
Jeff
·
fSend.sendByte(0x12);
is the same as
fSend.sendByte(18);
and so
fSend.sendByte(0x1F);
is the same as
fSend.sendByte(31);
So if your data is in int value,
just use
fSend.sendByte(value);
or
fSend.sendByte((byte)value); //if parameter must be type byte
regards peter
·
Neither of those 2 methods work.
I would really like to know what the
data stream actually looks like as
it is sent out the port.
When this gets sent
fSend.sendByte(0x12);
it is not·the same as:
fSend.sendByte(18);
or the same as:
fSend.sendByte((byte)18);
The program only accepts the 0x12
If I make a byte array containing
each possible value up to 255 is
the only way I can think of.
static byte[noparse]/noparse myByte = new byte[noparse]/noparse;
myByte[noparse][[/noparse]1] = 0x01;·
myByte[noparse][[/noparse]2] = 0x02;·
myByte[noparse][[/noparse]3] = 0x03;·
myByte[noparse][[/noparse]4] = 0x04;·
myByte[noparse][[/noparse]5] = 0x05;·
myByte[noparse][[/noparse]6] = 0x06;·
myByte[noparse][[/noparse]7] = 0x07;·
myByte[noparse][[/noparse]8] = 0x08;·
myByte[noparse][[/noparse]9] = 0x09;·
myByte[noparse][[/noparse]10] = 0x0A;·
myByte[noparse][[/noparse]11] = 0x0B;·
...
side so it doesn't blow up because of mismatched
types?! This stuff is really strongly typed!
I'm used to VB & JavaScript!
Thanks for trying Pete
Jeff
·
If it doesn't get accepted I suspect the mentioned checksum
is calculated wrong.
Attach your entire program and a description of the protocol
then I can give proper advise.
regards peter
You are correct sir. My check sum
was using 12 instead of 18. Another
mystery solved.
You just made my program alot smaller!
Thank you very much Peter.
Jeff
ie. The first byte is A1 and the second byte is B2. I need to convert them as B2A1.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
sejour de luxe - Centre formation a distance
regards peter