This question already has answers here:
Visual Studio debugger - Displaying integer values in Hex
(7 answers)
Closed 9 years ago.
I am using the below code
int.Parse("376") the result is coming as
int.Parse("376") = 0x00000178 int
and i tried to do as
Convert.Toint32("376") also then the result is same
please help me how to convert string to number?
It is working fine. 0x00000178 is the hexadecimal representation of 376.
Your Hex button is enabled in Visual Studio.
0x00000178 is the hexadecimal representation for 376, so using int.Parse or Convert.ToInt32 is OK.
However, I suggest to use the int.TryParse() method:
int i;
if (int.TryParse(yourString, out i))
{
// the string is converted successfully to an int, now you can find the int value in the variable 'i'
}
else
{
// Can't convert to an int: the string contains probably some characters that aren't digits
}
It is working fine. 0x178 is hex based for 376 in decimal.
Related
This question already has an answer here:
How do I print an integer in binary with leading zeros?
(1 answer)
Closed last year.
Would anyone know how to convert a binary number into a string that represents its digits ?
let s: u32 = 0b00100000001011001100001101110001110000110010110011100000;
I need study different parts of this binary number by cuting it into pieces (ex first 5 digits, then digit 6 to 15, etc...).
In order to do so, I'm thinking using string slices but first I need to convert the binary number into a string ( "00100000010110011...").
Thank you !
Use binary format:
fn main() {
let s: u64 = 0b00100000001011001100001101110001110000110010110011100000u64;
let s_str: String = format!("{s:b}");
println!("{s_str}");
}
Playground
This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 2 years ago.
I have a integer value i.e. 1010110. How to treat this value as binary? So that i can find the integer value of that binary value.
You can pass a base parameter (2 in this case) to the int function:
s = "1010110"
i = int(s, 2)
# 86
Can anyone tell me how to convert a float number to 32-bit binary string and from a 32-bit binary string to a float number in python?
'bin' function in python works only for integers.
I need a single bit string as in internal representation. I do not want separate bit strings for the number before and after the decimal places joined by a decimal place in between.
EDIT: The question flagged does not explain how to convert binary string to float back.
Copied from this answer and edited per suggestion from Mark Dickinson:
import struct
def float_to_bin(num):
return format(struct.unpack('!I', struct.pack('!f', num))[0], '032b')
def bin_to_float(binary):
return struct.unpack('!f',struct.pack('!I', int(binary, 2)))[0]
print float_to_bin(3.14) yields “01000000010010001111010111000011”.
print bin_to_float("11000000001011010111000010100100") yields “-2.71000003815”.
I was able to create a program that takes bin decimals as string an returns int decimals!
I used a for loop to start from 1 until the len() of the str+1 to use i number to elevate 2 and, then just keep track of the result with result +=:
def binary_poin_to_number(bin1)->float:
#Try out string slicing here, later
result = 0
for i in range(1,len(bin1)+1):
if bin1[i-1] == '1':
result += 2**-i
return result
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 4 years ago.
This sounds simple, but I can't find a way to convert a String value(possibly null) to Integer in single line without using if else in Java 8+. The answer might involve usage of ofNullable and isPresent.
Some things I have tried:
String x = ...;
Integer.valueOf(x); // fails if x is null
Optional.ofNullable(Integer.valueOf(x)).orElse(null); // NullPointerException
int value = Optional.ofNullable(x).map(Integer::parseInt).orElse(0);
This will result in a default value of 0 if the input String is null.
As an alternative, use:
Integer value = Optional.ofNullable(x).map(Integer::valueOf).orElse(null);
which will result in null if the input String is null.
What about using ? operator instead of one line if...else?
Integer value = x != null ? Integer.valueOf(x) : null;
This question already has answers here:
How do I decode HTML entities in Swift?
(23 answers)
Closed 7 years ago.
I have an RSS feed and in its description element (here: www.marketoloji.com/?feed=rss2) I have ascii characters for the ones like ' or & and they are seen as ’ / &. How can I render that description string in Swift so that I wont see ascii characters?
You can use NSAttributedString to easily convert html code for you using the NSHTMLTextDocumentType option:
extension String {
var htmlString:String {
return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}
}
let htmlCode = "’ / &"
htmlCode.htmlString // "’ / &"