Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Long to String Conversion


bonilt Nov 17, 2009 08:20 PM

Is there any way to convert a long into a string without losing the precision of the long integer? When I try to concatenate a long integer with a string it loses the precision.


dmn Nov 17, 2009 10:49 PM

Have you tried to use the formatfloat() instruction?


bonilt Nov 18, 2009 02:37 PM

If I try to use that it will convert the long into a float and it will lose precision.


Dana Nov 18, 2009 08:04 PM

Just prior to the release of the last OS the instruction FormatLong was added. It is not yet documented in CRBasic help file and does not show up in the instruction list, but it is in the OS and the CRBasic Precompiler (which means if you type it in correctly, it should compile if you have the most recent OS & precompiler). I have limited information on it and have done NO testing, so I am not fully certain how it works. Here is the info I do have :)


FormatLong uses the specifiers d or i in order to convert the long argument into a string decimal value,or x or X in order to convert the long argument into a string hexadecimal value, or o in order to convert the argument into a string base 8 (octal).

This site is a good reference for both the long and float conversions:

http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/

(Note, referring to the above reference, we support the float specifiers with formatFloat (G,g,e,E,f) and the integer specifiers (i,d,o,x,X) with formatLong. We currently do not support char (c) or string (s) specifiers. We also support only one specifier in the format string.)

Note that for both FormatFloat and FormatLong, the

%[flags][width][.precision][length]specifier

can occur in the middle of the format string, as in "I am %d years old"

Here are some examples:

str(1) = formatLong(255,"%d")
str(2) = formatLong(255,"%X")
str(3) = formatLong(255,"%x")
str(4) = formatLong(255,"%#X")
str(5) = formatLong(255,"%#x")
str(6) = formatLong(255,"%5d")
str(7) = formatLong(255,"%05d")
str(8) = formatLong(255,"%05x")
str(9) = formatLong(255,"%05X")
str(10) = formatLong(255,"%o")
str(11) = formatLong(65,"I am %d years old")
str(12) = formatLong(255,"&H%x") '&HFF (255)
Public number as Long
number = str(12) '"&Hff" is 255

I hope this helps. Regards,

Dana W.


bonilt Nov 19, 2009 01:24 PM

Thanks for the information Dana.

I have an unrelated question to this topic. Is there a way to calculate the checksum of a numeric array using the checksum function or any other function? It seems it only lets me calculate the checksum on a string.

Log in or register to post/reply in the forum.