why we have to use cast or cnvert function in 2nd and 3rd selecct statement, and what we achieve by casting or converting it to varchar, what varchar data type do in it exactly?
DECLARE #mybin1 binary(5), #mybin2 binary(5)
SET #mybin1 = 0xFF
SET #mybin2 = 0xA5
-- No CONVERT or CAST function is necessary because this example
-- concatenates two binary strings.
SELECT #mybin1 + #mybin2
-- A CONVERT or CAST function is necessary because this example
-- concatenates two binary strings plus a space.
SELECT CONVERT(varchar(5), #mybin1) + ' '
+ CONVERT(varchar(5), #mybin2)
-- Here is the same conversion using CAST
SELECT CAST(#mybin1 AS varchar(5)) + ' '
+ CAST(#mybin2 AS varchar(5))
Casting it to a VarChar converts the data from a sequence of bytes to character text. As Flip says, you can't directly concatentate the different types. Typically binary strings are for images, videos, streams, etc, where as varchar is usually string data like what is found in a text file.
because in your select statement you are concatenating the the variables with space(' ') which is a type of varchar to you need to cast them to get the desired results.
in a sense you it might give you an error because your data types are different/incompatible.
Since they are different data types, you can't directly concatenate binary data and text strings (varchar), which is why you need either the CAST or the CONVERT.
Related
When I try to format a string such as '%s%s' using a line of code like so:
format('%s%s', [x]);
I get an exception because you can't have multiple '%s' without using an array with the same amount of arguments such as:
format('%s%s', [x,x]);
However, I don't know how many '%s' I will have to format and therefore I don't know how long the array would have to be. I also only want '%s' assigned to only 1 value.
Is there a way in which you can use multiple '%s' and assign them all to the same index?
As described in the documentation you can use an index specifier to identify the argument by a zero based index. The index specifier is written immediately after the % and is followed by a :.
Your example would be:
Format('%0:s%0:s', [x])
MyStr := StringReplace('%s%s', '%s', x, [rfreplaceALL]);
I am trying to store the ComboColorBox.Color property in a database field through LiveBindings in a Firemonkey Project. I want to store the hexadecimal AlphaColor value, but the hex color value is automatically converted to a decimal value and not an AlphaColor.
I've searching in the LiveBindings documentation and found that I can change the value of control property before is stored in database writing a binding expression in CustomParse property of the Binding. The problem is that there is no built in function to convert an AlphaColor to a String in an expression.
Do I have to write a custom function to do that? How do I write that function and where? Or there is another solution?
You should know that a TAlphaColor is an integer, or more precisley a cardinal as defined in System.UITypes
type TAlphaColor = Cardinal;
Thus you can apply any integer to string conversion functions, like:
Label1.Text := IntToStr((Sender as TComboColorBox).Color); // decimal notation
Label2.Text := IntToHex((Sender as TComboColorBox).Color, 8); // hexadecimal notation
So, to store the TAlphaColor value as a hexadecimal string, you would convert the color value using IntToHex(). On the other hand, Are you sure you really want to store it in the db as a string in the first place.
I am trying to write custom dissector for Wireshark, which will change byte/hex output to ASCII string.
I was able to write the body of this dissector and it works. My only problem is conversion of this data to ASCII string.
Wireshark declares this data to be sequence of bytes.
To Lua the data type is userdata (tested using type(data)).
If I simply convert it to string using tostring(data) my dissector returns 24:50:48, which is the exact hex representation of bytes in an array.
Is there any way to directly convert this byte sequence to ascii, or can you help me convert this colon separated string to ascii string? I am totally new to Lua. I've tried something like split(tostring(data),":") but this returns Lua Error: attempt to call global 'split' (a nil value)
Using Jakuje's answer I was able to create something like this:
function isempty(s)
return s == nil or s == ''
end
data = "24:50:48:49:4A"
s = ""
for i in string.gmatch(data, "[^:]*") do
if not isempty( i ) then
print(string.char(tonumber(i,16)))
s = s .. string.char(tonumber(i,16))
end
end
print( s )
I am not sure if this is effective, but at least it works ;)
There is no such function as split in Lua (consulting reference manual is a good start). You should use probably string.gmatch function as described on wiki:
data = "24:50:48"
for i in string.gmatch(data, "[^:]*") do
print(i)
end
(live example)
Further you are searching for string.char function to convert bytes to ascii char.
You need to mark range of bytes in the buffer that you're interested in and convert it to the type you want:
data:range(offset, length):string()
-- or just call it, which works the same thanks to __call metamethod
data(offset, length):string()
See TvbRange description in https://wiki.wireshark.org/LuaAPI/Tvb for full list of available methods of converting buffer range data to different types.
I have a table with TEXT, INTEGER, REAL and other data types.
I wrote a generic sql function to read results for all queries using sqlite3_column_text() like so:
char *dataAsChar = (char *) sqlite3_column_text(compiledStatement, ii);
Although I should be using sqlite3_column_int etc. to read the numeric values, the above code seems to work for me. I get the number values as string which I later convert to int using [*numberAsString* intValue].
Since I am using a generic function to read all my db values, this is very convenient for me. But is there something that can go wrong with my code?
I could use sqlite3_column_type for each column to determine the type and use appropriate function. Am I correct in assuming that sqlite3_column_text basically returns the column value in TEXT format and does not necessarily need the value itself for be TEXT?
The only situation where I can see this implementation failing is with BLOB data type.
The documentation says:
These routines attempt to convert the value where appropriate. […]
The following table details the conversions that are applied:
Internal Type Requested Type Conversion
NULL TEXT Result is a NULL pointer
INTEGER TEXT ASCII rendering of the integer
FLOAT TEXT ASCII rendering of the float
BLOB TEXT Add a zero terminator if needed
I'm trying to use LiveBindings to format a number for display in a TEdit on a FireMonkey form.
I'm trying to use the Format method in the CustomFormat of the binding to format the number with two decimal places.
I can 'hard code' the output:
Format("Hello", %s)
which is working, but I can't work out what formatting string to use. If I try a standard formatting string such as,
Format("%.2f", %s)
I get a runtime error "Format invalid or incompatible with argument".
Indeed I get an error whenever I include a % symbol in the format string, so I'm guessing Format takes a different type of argument, but I can't find any documentation to say what the correct format string is.
You can not use Format('%.2f',[%s]) in LiveBindings -> CustomFormat
The %s is reserved for the data and for a TEdit , it's a string
d : double;
s : string;
...
d := 1234.5678;
s:=Format('%.2f',[d]);
Format() is to convert [int, decimal, double, float] to a string .
all other give you a error : invalid argument
valid is for example
TLinkControlToField1 -> CustomFormat : "Double : "+UpperCase(%s)
will give you in Edit1.text
Double : 1234.5678
OK , we know that Uppercase() for '1234.5678' has no effects .
Is only to show (%s) is a string
Solutions:
Set to TFloatField -> DisplayFormat #00000.00
rounds and display 01234.57
check TFloatField -> currency
rounds and display 1234.57
use a component look here
LiveBindings in XE3: Formatting your Fields
The parameter is passed into CustomFormat as %s. The bindings system preparses out this parameter before the data is passed onto the evaluator. Thus any other % symbols in the CustomFormat string will give an error.
As with a normal format string you can include a literal % sign by putting a double % (i.e. %%).
So, any %s in the format string need to be converted to %%, e.g.
Format('%%.2f', %s)
which gets parsed out to
Format('%.2f', 67.66666)
and then parsed down to
67.67
for display.
If you want to include a literal % in the final output you need to put a quadrupal %, e.g.
Format('%%.2f%%%%', %s)
becomes
Format('%.2f%%', 67.6666)
and displays as
67.67%
Note: The normal format function takes a final parameter which is an array of values. The Format method in the bindings system takes a variable length list of parameters.
Also, the method names are case sensitive. 'Format' is correct, 'format' will fail.
imput 67.6666
CUSTOM FORMAT: ToStr(Format('%%.2f', Value)) + ' %%'
output 67.00 %