|
|
View previous topic :: View next topic |
Author |
Message |
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
int32 in string? |
Posted: Wed Aug 26, 2015 1:33 pm |
|
|
Hello,
I need for a INT32 in a string to send via GPRS.
but I do not know how best to do this.
for example:
I have the string: char command [400];
I want by: 4294967290
I tried these commands, but take up much space in the string:
flo = 4294967290;
-sprintf (command, "\ f% Lu" flo);
-strcpy (command, flo);
each digit of the string ends up occupying a space, in case the length of the string will be above 10.
then I have 4 bytes occupying the space of 10 bytes.
I need to improve it.
Any tips?
I tried hard but I could not. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Aug 26, 2015 1:54 pm |
|
|
If I read this right , you have an INT32 value that you want to send to a 'device' that only accepts 'strings'.
If that's true, then your number (as shown), as a string will be 10 bytes +1 ( end of string delimiter).
It's not a limit or function of the PIC rather the GPRS device's requirement for accepting data. You should post a link to the device , perhaps ther's a 'mode' for it to accept data other than as a 'string'?
Jay |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Wed Aug 26, 2015 2:02 pm |
|
|
temtronic wrote: | If I read this right , you have an INT32 value that you want to send to a 'device' that only accepts 'strings'.
If that's true, then your number (as shown), as a string will be 10 bytes +1 ( end of string delimiter).
It's not a limit or function of the PIC rather the GPRS device's requirement for accepting data. You should post a link to the device , perhaps ther's a 'mode' for it to accept data other than as a 'string'?
Jay |
I put all data in ums string for easy assembly of a package and ship all of time. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Aug 26, 2015 2:08 pm |
|
|
That is the limitation of strings.
A ten digit number is ten digits long....
Only suggestion if this is for your own target at the other end, consider sending in hex, rather than decimal. Your 4294967290, takes 8 hex characters, versus ten decimal.
Two hex digits per byte.
It is also quicker to convert from binary to hex, and from hex to binary than to between binary and decimal. Typically about 50% of the code execution time...
This is why hex is so commonly used for things like instrumentation data. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Wed Aug 26, 2015 10:19 pm |
|
|
Ttelmah wrote: | That is the limitation of strings.
A ten digit number is ten digits long....
Only suggestion if this is for your own target at the other end, consider sending in hex, rather than decimal. Your 4294967290, takes 8 hex characters, versus ten decimal.
Two hex digits per byte.
It is also quicker to convert from binary to hex, and from hex to binary than to between binary and decimal. Typically about 50% of the code execution time...
This is why hex is so commonly used for things like instrumentation data. |
I do not know how to do this.
I searched but not found how I can convert a decimal number into a hex and then for him a string. (big problem) :(
this number will vary from 0 to 4,294,967,290.
I will have to treat this as well.
Can you help me with this?
I could directly send the variable format int32 this would quite reduce my traffic, correct? what do you think? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Aug 27, 2015 1:02 am |
|
|
%Lx
Going the other way, strtoul using base 16 (use twice on each group of four hex character), or atoi32 (you have to catenate "0x" to the front). |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Thu Aug 27, 2015 5:35 pm |
|
|
Ttelmah wrote: | %Lx
Going the other way, strtoul using base 16 (use twice on each group of four hex character), or atoi32 (you have to catenate "0x" to the front). |
I could directly send the variable format int32 this would reduce quite my traffic, correct? What do you think?
accurate, Lx% would use to send direct int32! may be the best solution !! ???.
But the two functions that told me over they convert a string into an int32.
I need:
convert int32 in HEX
for them on a string.
concatenate other data
send
or can:
make a printf with all the variables you want to send.
what do you think about this? |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
|
Posted: Thu Aug 27, 2015 5:39 pm |
|
|
It may be sent as a string but a int32 is 4 bytes send the 4 bytes in the string then reverse it back to a int32 at the other end. Unless the data triggers control character problems. For example, a null. If the string is fixed length there should be no problems. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Thu Aug 27, 2015 5:45 pm |
|
|
drolleman wrote: | It may be sent as a string but a int32 is 4 bytes send the 4 bytes in the string then reverse it back to a int32 at the other end. Unless the data triggers control character problems. For example, a null. If the string is fixed length there should be no problems. |
Exactly, I can do that yes.
But I thought I'd not do so to avoid problems on my server.
I think it might be better one print formatted, keep the format of the data.
Perhaps the best way though a bit more data that could be streamlined using hex. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Aug 28, 2015 1:28 am |
|
|
There are some basic bits of understanding missing here.
You start your thread with the title 'int32 in string'.
Now in C, a 'string', is an array of text characters terminated with a NULL ('\0').
If you ignore the 'string' bit, then and int32, is just four bytes of data, and can be sent as four bytes of data. However strings at this point _cannot_ be involved. Any of the four bytes could (and at times _will_) be zeros. As such no string operations can be involved. As soon as you involve 'strings', you have to accept that you can no longer send the 'raw' data.
Now there are lots of ways you can send an int32 as a string. An int32, is just 32bits of data. You can play with them any way you want. The most 'compressed' - simple - way you can send the int32, on an 8bit link and still work with 'strings', is to send the data 7bits at a time. So, something like:
Code: |
void send32(int32 val_to_send, char * string)
{
int8 temp, ctr;
for (ctr=0;ctr<5;ctr++)
{
temp=val_to_send & 0x7F; //low seven bits of value
*(string++)=temp+0x80; //send seven bits as 128 to 255
val_to_send/=128; //move to next seven bits
}
*string=0; //and the null terminator to make this a 'string'
}
|
Now this puts the data seven bits at a time, into the string, using the values 128 to 255 to hold the seven bits. This then leaves the 'low' character values (0 to 127), still available for the NULL, and things like carriage returns etc., if required for punctuation. This takes just five characters to send the int32 value, plus one terminator.
At the receive end, if the character you receive is <128, then it is punctuation, text or whatever. If it is 128 or higher then it is the bits of a number. You can just subtract the 128, and rotate to put the number back together.
This approach allows you to use standard 'string' handling, but sends the data more compactly.
In each case the key is the number of bits you actually 'use' for data in the transfer. If you are involving 'strings', you can't use all eight bits, since there is the reserved value (NULL), and potentially any other punctuation type characters (things like line feed, commas to separate numbers etc..). Using 7 bits is the easiest compact compromise to retain these. With 'hex' you use four bits out of each character sent, while with decimal, you only use 3.162 bits (ten states out of 256). |
|
|
drolleman
Joined: 03 Feb 2011 Posts: 116
|
int32 in string |
Posted: Fri Aug 28, 2015 8:56 pm |
|
|
I agree with you. But I like to cheat sometimes. Since I have control of both sides of the conversation, I like to pack as much as I can into the data stream. Many devices that carry data don't care what the data is. this is perfect for data verses a correctly formatted string. Some only check for a null. In this case I use 5 bytes maximum, so I have 0x01 to 0xff to work with. The first will indicate how many bytes are valid or null in the next 4 bytes, so no nulls appear.
For example, if it's 01 05 xx xx xx, it will only take the 1 following byte for the data, 00 00 00 05. Or 05 67 89 xx xx will take 2 following bytes, 00 67 00 89, it knows the middle byte is a null. 0xff xx xx will take no following bytes data, 00 00 00 00. These "strings" can be stored on a server for later processing if you want. If there are lots of nulls it can really shorten your data stream.
Data transmission is sometimes expensive, so finding ways to make it work is where it's at. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|