What is the most concise way to convert a CInt to an Int (the native integer type) in Swift? Right now I'm using:
NSNumber.numberWithInt(myCInt).integerValue
Is this the preferred approach?
You can do it like this:
var c: CInt = 4
var i: Int = Int(c)
CInt is a type alias of Int32 which is one of the types Int can take in its init.
Here are the types I've found you can use like that: UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, UInt, Float, Double, Float80
You can try this:
var myCInt : CInt = 4
var newNumber : NSNumber = NSNumber(int: myCInt)
Related
Assume the following:
#include <iostream>
int main()
{
double a = 5.6;
intptr_t b = (intptr_t) a;
double c = (double) b;
return 0;
}
c will be 5. My question is, since intptr_t is also 64 bits on a 64 bit machine (same as double), how come the precision bits are not saved during casting?
Although intptr_t is meant to represent a pointer to an int, its underling type is still an integer. Thus
intptr_t b = (intptr_t)a
Is still truncating the double, similar to if you'd just written:
int b = (int)a;
What you want to do is take the address of a:
intptr_t b = (intptr_t)&a
And then convert it back
double c = *(double*)b;
could any please explain the below code
const uInt32 eVal = 8ul;
const uInt32 fVal = 5ul;
const uInt32 zVal = 0ul;
what does ul and numbers 8,5,0 are stand for
It's the programmer being unnecessarily verbose. ul is the prefix for an unsigned long literal, so 8ul has an unsigned long type.
Most likely, that isn't the same type as the uInt32, so really the code you present is an exercise in obfuscation. Really they should rely on the automatic implicit conversion rules and write
const uInt32 eVal = 8;
const uInt32 fVal = 5;
const uInt32 zVal = 0;
perhaps even preferring constexpr in place of const.
Reference: http://en.cppreference.com/w/cpp/language/implicit_conversion
ul stands for unsigned long representation of numbers. Here the number 8 or 5 or 0 of type unsigned long is being assigned to constant uInt32 type variable.
The 'ul' is used for representation. It need not be used also.
I don't have any experience of Crypto++ library. In my project I need to typecast Integer to int. This is what I am trying:
int low_bound1=8;
int low_bound2=9;
Integer x=1,y=2;
low_bound1=(int)x;
low_bound1=(int)y;
This is the error I am getting:
error: invalid cast from type 'CryptoPP::Integer' to type 'int'
Is it possible to do? If yes then how?
Is it possible to do? If yes then how?
Yes, it may possible to do, but not with a simple C-style cast.
Here is the documentation for the Integer class from the manual: Integer Class Reference. Under the heading ACCESSORS, there are two methods:
bool IsConvertableToLong () const
Determines if the Integer is convertable to Long. More...
signed long ConvertToLong () const
Convert the Integer to Long. More...
So you need to do something like:
int low_bound1, low_bound2;
Integer x=1,y=2;
if (x > std::numeric_limits<int>::max() || x < std::numeric_limits<int>::min())
throw std::out_of_range("Integer x does not fit int data type");
if (y > std::numeric_limits<int>::max() || y < std::numeric_limits<int>::min())
throw std::out_of_range("Integer y does not fit int data type");
low_bound1 = static_cast<int>(x.ConvertToLong());
low_bound2 = static_cast<int>(y.ConvertToLong());
I've a C dll and I want to use it in vb6, here is the C syntax:
#ifndef _MSC_VER
typedef long long INT64T;
#else
typedef __int64 INT64T;
#endif
int Init(int opts, void *key, INT64T offs);
I've converted it to VB6:
Public Declare Function Init Lib "x.dll" (ByVal opts As Integer, ByVal key As Long, ByVal offs As Currency)
and call it:
Init 0, 0, 0
some part of the function is executed and the I've got this error:
Bad DLL calling convention
would you please kindly let me know what is the problem? The dll is from third-party dll so I don't know anything more about it,
For 64 bit you should use:
Type ULARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
and your declaration should be like this:
Public Declare Function Init Lib "x.dll" (ByVal opts As Long, ByVal key As Long, ByVal offs As ULARGE_INTEGER)
Public Declare Sub CopyMemory Lib "kernel32" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Now to the conversions:
After you get the ULARGE_INTEGER you can translate it to Currency like this:
' Copy int64 into the Currency data type
Dim curr as Currency
Dim int64 as ULARGE_INTEGER
' Copy ULARGE_INTEGER into the Currency data type
CopyMemory curr, int64, 8
' Multiply by 10000 to move Visual Basic decimal point to the end of the actual number
curr = curr * 10000
to traverse back to ULARGE_INTEGER from Currency:
Dim curr as Currency
Dim int64 as ULARGE_INTEGER
' Devide by 10000 to move Visual Basic decimal point to original position
curr = curr * 0.00001
' Copy Currency into the ULARGE_INTEGER data type
CopyMemory int64, curr, 8
I often have a situation, where I expect an int (of any type, int/int8/16/32/64) and check for it using a type switch
switch t := v.(type) {
case int, int8, int16, int32, int64:
// cast to int64
case uint, uint8, uint16, uint32, uint64:
// cast to uint64
}
Now I cannot use a direct cast, because t in this case will be of type interface{}. Do I really have to split this up into cases for each integer type?
I know that I could do it via reflection using reflect.ValueOf(v).Int(), but shouldn't there be a better (less verbose) way of doing this?
UPDATE:
Filed an issue, and Rob advised to just use reflect in this case.
It's hard to give you an opinion without more context but it looks like you're trying to make your implementation too generic, which is common from people that worked mostly with more dynamic languages or w/ generic support.
Part of the process of Learning Go is learning to embrace its type system, and depending on where you're coming from, it can be challenging.
Usually, in Go, you want to support one type that can hold all possible values you need to handle. In your case it would probably be a int64.
Take a look on the math package, for example. It only work with int64 and expect anyone using it to typecast it properly instead of trying to convert everything.
Another option is to use a interface to be type-agnostic, like the sort package does. Basically, any method that are type specific will be implemented outside of your package and you expect certain methods to be defined.
It takes a while to learn and accept these attributes but overall, in the end, it proves to be good in terms of maintainability and robustness. Interfaces ensure you have orthogonality and strong types makes sure you're in control of type conversions, which in the end can cause bugs and also unnecessary copies in memory.
Cheers
What problem are you trying to solve? The full solution that you've described looks like this:
func Num64(n interface{}) interface{} {
switch n := n.(type) {
case int:
return int64(n)
case int8:
return int64(n)
case int16:
return int64(n)
case int32:
return int64(n)
case int64:
return int64(n)
case uint:
return uint64(n)
case uintptr:
return uint64(n)
case uint8:
return uint64(n)
case uint16:
return uint64(n)
case uint32:
return uint64(n)
case uint64:
return uint64(n)
}
return nil
}
func DoNum64Things(x interface{}) {
switch Num64(x).(type) {
case int64:
// do int things
case uint64:
// do uint things
default:
// do other things
}
}
Use the reflect package. Notice that this is likely to be a lot slower than unrolling the switch.
switch t := v.(type) {
case int, int8, int16, int32, int64:
a := reflect.ValueOf(t).Int() // a has type int64
case uint, uint8, uint16, uint32, uint64:
a := reflect.ValueOf(t).Uint() // a has type uint64
}
You could do something like this... convert to a string and then parse the string. Not really efficient but compact. I've put this example in the Go playground here: http://play.golang.org/p/0MCbDfUSHO
package main
import "fmt"
import "strconv"
func Num64(n interface{}) int64 {
s := fmt.Sprintf("%d", n)
i,err := strconv.ParseInt(s,10,64)
if (err != nil) {
return 0
} else {
return i
}
}
func main() {
fmt.Println(Num64(int8(100)))
fmt.Println(Num64(int16(10000)))
fmt.Println(Num64(int32(100000)))
fmt.Println(Num64(int64(10000000000)))
fmt.Println(Num64("hello"))
}
// Outputs:
// 100
// 10000
// 100000
// 10000000000
// 0