SQL convert unexpected result - sql

Please explain why
select convert(int,'1') = 1
but
select convert(int,
convert(varbinary(MAX),'1')) = 49

Convert to varbinary without a style parameter defined translates each ASCII character in input string to a binary byte. That is why the input string '1' first gets mapped to the ASCII code 49, after which the outer convert returns the integer value 49.

Because convert(varbinary(MAX),'1') is binary 0x31 which is integer 49.

By using varbinary you are essentially converting the ascii representation of "1" to the actual binary digits which represent it.
You will get the same result using select ascii('1').
In the same way select convert(varbinary(1),'A') = 0x41 which in base 10 is 65, which is the ascii value for A

Related

need help for sql server

When I save data byte[] in SQL Server the value change and add 0x0 in the first of value
the correct value (0xFFD8FFE000104A46)
the incorrect value (0x0FFD8FFE000104A46494600010102004C)
0xF and 0x0F are the same number, both are hexadecimal notations of number 15 decimal. A byte contains two hexadecimal 'digits'. If the left most digit is 0, it doesn't affect the value, just like zero-hundred and fifteen is the same as fifteen. The notation with the leading 0 just prints all the bytes, the one without strips the leading zeros.
Where the 494600010102004C part is coming from I don't know.

ISO 8583 Field 22

I'm trying to build an parser to deserialze into object. Socket will send byte into parser. For the length of field 22 POS Entry Mode will N3 and byte will be always 2 digit. How to get the value for this field ?
You read the ASCII value of this field, and convert it into an integer.
if it says N3 that means they are three digits numeric field, so if the value say 51, you cast it to 051 and send the ASCII equivalent
Field 22 is pos entry mode. It's 3 digit numeric value. If format is BCD then 2 bytes contains 4 digits[ 0 (padded) + 3 digit POS entry mode). If format is ascci then it is 3 byte.

VB.net Decimal to Hex conversion

I am using the code below to convert decimal to hex.
intPatternColorsRed(1)=255 I get "FF" which is good
intPatternColorsRed(1)=0 I get "0" but I need "00"
Can I format the output to use a placeholder so I alway get a 2 digit return
'//Convert Dec to Hex
strData(0) = intPatternColorsRed(1).ToString("x")
strData(0) = intPatternColorsRed(1).ToString("X2")
ref: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

How to format integer as string with 2 digits?

I would like to format an integer 9 to "09" and 25 to "25".
How can this be done?
You can use either of these options:
The "0" Custom Specifier
value.ToString("00")
String.Format("{0:00}", value)
The Decimal ("D") Standard Format Specifier
value.ToString("D2")
String.Format("{0:D2}", value)
For more information:
Custom Numeric Format Strings
Standard Numeric Format Strings
If its just leading zero's that you want, you can use this:
value.tostring.padleft("0",2)
value.ToString().PadLeft(2, '0'); // C#
If you have 2 digits, say 25 for example, you will get "25" back....if you have just one digit, say 9 for example, you will get "09"....It is worth noting that this gives you a string back, and not an integer, so you may need to cast this later on in your code.
String formate is the best way to do that. It's will only add leading zero for a single length. 9 to "09" and 25 to "25".
String.format("%02d", value)
Bonus:
If you want to add multiple leading zero 9 to "0009" and 1000 to "1000". That's means you want a string for 4 indexes so the condition will be %04d.
String.format("%04d", value)
I don't know the exact syntax. But in any language, it would look like this.
a = 9
aString =""
if a < 10 then
aString="0" + a
else
aString = "" + a
end if

VB Convert RGB String to Hex

The title pretty much explains my issue. I need to convert a single string RGB value into a Hex value. I can do this if the value is given in three separate strings, but as the RGB is given from a color picker I'm unable to do this - unless I split the string which I don't want to do as I feel it's unnecessary.
I want to be able to convert a string such as: 0, 112, 192 into it's hexadecimal equivalent. Can I convert the entire string or do I have to split the string into its RGB parts first?
Since you can have varying numbers of decimal digits for the RGB value, you'll need to separate it before you convert it.
s1 = "0, 112, 192"
s2 = ""
For Each s As String In s1.Split(",")
s2 &= CInt(s).ToString("x2")
Next s