GVKun编程网logo

swift 类型转换(swift类型转换)

2

在本文中,我们将带你了解swift类型转换在这篇文章中,我们将为您详细介绍swift类型转换的方方面面,并解答swift类型转换常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的12_c++类

在本文中,我们将带你了解swift 类型转换在这篇文章中,我们将为您详细介绍swift 类型转换的方方面面,并解答swift类型转换常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的12_c++类型转换、4. 类型转换、android 类型转换 工具函数、C# 语法练习(4): 类型转换

本文目录一览:

swift 类型转换(swift类型转换)

swift 类型转换(swift类型转换)

1.获取当前时间
var curDate = NSDate()
var dateFormatter = NSDateFormatter()
var timeFormatter = NSDateFormatter()
dateFormatter.dateFormat = “yyy-MM-dd”
timeFormatter.dateFormat = “HH:mm:ss SSS”
var dateStr = dateFormatter.stringFromDate(curDate)
var timeStr = timeFormatter.stringFromDate(curDate)

2.时间转换
func dateFromString(dateStr: String) -> NSDate? {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = “yyyy-MM-dd”
let date = dateFormatter.dateFromString(dateStr)
return date
}

func stringFromDate(format: String,date: NSDate) -> String {
let locale = NSLocale.currentLocale()
let dateFormat = NSDateFormatter.dateFormatFromTemplate(format,options: 0,locale: locale)
let dateformatter = NSDateFormatter()
dateformatter.dateFormat = dateFormat
return dateformatter.stringFromDate(date)
}

3.String转换为Double
NSNumberFormatter().numberFromString(display.text!)!.doubleValue
或者
(display.text! as Nsstring).doubleValue

4.Double转换为String,小数为0时不显示后面的.0 let intValue = Int(newValue) if (Double(intValue) == newValue) { display.text = “(intValue)” } else { display.text = “(newValue)” }

12_c++类型转换

12_c++类型转换

c++类型转换

参考文章:http://www.cnblogs.com/TenosDoIt/p/3175217.html

c++风格的类型转换有4种:

const_cast,去const,volatile属性。

static_cast,静态类型转换,如int转换为char。

reinterpret_cast,仅仅是重新解释类型,但没有进行二进制的转换,相当于c语言中的圆括号强制类型转换。

dynamic_cast,动态类型转换,用于子类和父类之间的多态类型转换。

4中类型转换的格式,如:TYPE B = static_case(A);从类型转换的形式看,类型转换是一个函数,其中<>表示c++的模板函数。

1、reinterpret_cast

强制类型转换,简单粗暴,不推荐使用(名字这么长,估计就是这个原因)

int main(int argc, char **argv)
{
    double d = 100.1;
    int i = d;  // 隐式类型转换
    char *str = "100ask.taobao.com";
    int *p = reinterpret_cast<int *>(str); //强制转换

    printf("i = %d, str = 0x%x, p = 0x%x\n", i, 
          reinterpret_cast<unsigned int>(str), reinterpret_cast<unsigned int>(p));

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2、const_cast

去const,volatile属性,被const或volatile修饰的对象如果直接进行类型转换编译器将会报错。

int main(int argc, char **argv)
{
    const char *str = "100ask.taobao.com";
    //int *p = reinterpret_cast<int *>(str); 错误
    char *str2 = const_cast<char *>(str);
    int *p = reinterpret_cast<int *>(str2);
    printf("str = 0x%x, p = 0x%x\n", 
          reinterpret_cast<unsigned int>(str), reinterpret_cast<unsigned int>(p));
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3、static_cast

用于基类和子类之间的转换,但不能进行无关类型指针之间的转换(如非基类和子类)。其中从父类到子类的转换是不安全的。

用于基本数据类型转换,如:enum, struct, int, char, float等。

int main(int argc, char **argv)
{
    Human h;

    Guangximan g;

    Englishman *pe;

    pe = static_cast<Englishman *>(&h);/* 父类转换为子类不安全,人不一定是英国人啊 */

    Chinese *pc = static_cast<Chinese *>(&g);/*从子类到父类转换安全,广西人一定是中国人*/

    return 0;
}

4、dynamic_cast

有条件转换,动态类型转换,运行时类型安全检查(转换失败返回NULL):

a. 安全的基类和子类之间转换。在从基类转换为父类时,dynamic_cast和static_cast一样

b. 必须要有虚函数。

c. 相同基类不同子类之间的交叉转换。但结果是NULL。

4. 类型转换

4. 类型转换

4. 类型转换

4.1 在执行算术运算时,通常要求两个操作数有相同的大小(位数相同),并且存储方式也相同。例如:计算机可以直接将两个 16 为整数相加,但是不能直接将 16 位整数和 32 位整数相加,也不能直接将 32 位整数和 32 位浮点数相加。
4.2 如果我们一个表达式中混合使用了不同的数据类型,编译器会自动进行类型转换,这种转换叫做隐式转换。
4.3 程序员也可以进行强制类型转换,这种转换称为显示转换。
4.4 下面情况下,可以进行隐式转换:
当算术表达式或逻辑表达式中操作数类型不相同时
赋值运算中两端类型不相同时
函数调用中实参和形参类型不相同时
return 语句和函数的返回值不相同时
4.5 转换原则,小类型自动转换为大类型,整型转换为浮点型
如果任何一个操作数是浮点数的情况:

如果都不是浮点数的情况:

4.6 强制类型转换语法格式
(类型)表达式 例如:float f; int i = (int) f;
#include <stdio.h>
// 算术运算转换
void test1() {
            char c;
            short int s;
            int i;
            unsigned int u;
            long int l;
            unsigned long int ul;
            float f;
            double d;
            long double ld;
            //c 自动转换为 int 类型
            i = i + c;
            //s 自动转换为 int 类型
            i = i + s;
            //i 自动转换为 unsinged int 类型
            u = u + i;
            //u 自动转换为 long int 类型
            l = l + u;
            //l 自动转换为 unsinged long int
            ul = ul + l;
            //ul 自动转换为 float 类型
            f = f + ul;
            //f 自动转换为 double 类型
            d = d + f;
            //d 自动转换为 long double
            ld = ld + d;
}
// 赋值转换
void test2(){
            char c;
            int i;
            float f;
            double d;
            //c 自动转换为 int
            i = c;
            //i 自动转换为 float
            f = i;
            //f 自动转换为 double
            d = f;
}
// 强制类型转换
void test3(){
            float f = 3.14;
            int i = (int)f;
            printf("i=%d",i);
}
int main(void) {
            //test1();
            //test2();
            test3();
}

该博客教程视频地址:http://geek99.com/node/968

android 类型转换 工具函数

android 类型转换 工具函数

android中类型转换好像支持不是太好,备份下有用的类型转换函数和工具函数.

一、类型转换

1.byte 转 int   ( int 可以再转成long double float )

[java] 
public static int byteToInt(byte[] data, int offset) 

        int result = 0; 
    int n1, n2, n3, n4; 
     
    n1 = data[offset + 3] & 0xFF; 
    n2 = data[offset + 2] & 0xFF; 
    n3 = data[offset + 1] & 0xFF; 
    n4 = data[offset] & 0xFF; 
         
    result = n1 << 24  | n2 << 16 | n3 << 8 | n4; 
         
    return result; 


2. byte 转 Hex
[java] www.2cto.com
public static String toHexString(byte[] b)  

    char HEX_DIGITS[] = { ''0'', ''1'', ''2'', ''3'', ''4'', ''5'', ''6'', ''7'', ''8'', ''9'', 
                  ''A'', ''B'', ''C'', ''D'', ''E'', ''F'' }; 
         
        StringBuilder sb = new StringBuilder(b.length * 2); 
        for (int i = 0; i < b.length; i++) 
        { 
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >> 4]); 
            sb.append(HEX_DIGITS[b[i] & 0x0f]); 
        } 
        return sb.toString(); 

3. HexString 转 long

[java] 
public static long hexStringToLong(String s) 

    String serial = "0123456789ABCDEF"; 
                 
    s = s.trim().toUpperCase(); 
    if( s.length() > 8 ) return 0; 
         
    long num = 0; 
    int  len = s.length(); 
         
    for( int i = 0; i < len; i++) 
    { 
        num += (serial.indexOf( s.charAt(i) ) << (4 * (len - i - 1))); 
    } 
         
    return num; 

二、 工具函数
1. md5sum

[java] 
public static String getMd5sum(String filename)  

        InputStream fis; 
        byte[] buffer = new byte[1024]; 
        int numRead = 0; 
        MessageDigest md5; 
        try{ 
            fis = new FileInputStream(filename); 
            md5 = MessageDigest.getInstance("MD5"); 
            while((numRead=fis.read(buffer)) > 0) { 
                md5.update(buffer,0,numRead); 
            } 
            fis.close(); 
            return toHexString(md5.digest());   
        } catch (Exception e) { 
            return null; 
        } 


C# 语法练习(4): 类型转换

C# 语法练习(4): 类型转换


使用 Convert 类:
ToBoolean  -> bool
ToByte     -> byte
ToChar     -> char
ToDateTime -> DateTime
ToDecimal  -> decimal
ToDouble   -> double
ToInt16    -> short
ToInt32    -> int
ToInt64    -> long
ToSByte    -> sbyte
ToSingle   -> float
ToString   -> string
ToUInt16   -> ushort
ToUInt32   -> uint
ToUInt64   -> ulong


using System;

class MyClass
{
    static void Main()
    {
        int num;
        string str;

        num = 99;
        str = Convert.ToString(num);
        Console.WriteLine(str);

        str = "123";
        num = Convert.ToInt32(str);
        Console.WriteLine(num);

        Console.ReadKey();
    }
}

 
 
 
 
 

 

 

  
  

隐式转换, 只要容得下就行:

using System;

class MyClass
{
    static void Main()
    {
        byte n = byte.MaxValue;

        short n1 = n;
        int   n2 = n;
        long  n3 = n;

        Console.WriteLine("{0},{1},{2}", n1, n2, n3); //255,255,255

        Console.ReadKey();
    }
}

 
 
 
 
 

 

 

  
  

显示转换, 可能会因溢出而丢失数据:

using System;

class MyClass
{
    static void Main()
    {
        ulong n = ulong.MaxValue;

        byte   n1 = (byte)n;
        ushort n2 = (ushort)n;
        uint   n3 = (uint)n;

        Console.WriteLine("{0},{1},{2}", n1, n2, n3); //255,65535,4294967295

        Console.ReadKey();
    }
}

 
 
 
 
 

 

 

  
  

溢出检查:

using System;

class MyClass
{
    static void Main()
    {
        int i;
        byte b;

        i = 255;
        b = (byte)i;             /* i 在 byte 的范围内, 不会溢出 */
        b = unchecked((byte)i);  /* 同上一行, 不做溢出检查 */
        Console.WriteLine(b);    // 255

        i++;
        b = (byte)i;             /* i 超出 byte 的范围内, 会溢出 */
        b = unchecked((byte)i);  /* 同上一行, 不做溢出检查 */
        Console.WriteLine(b);    // 0

        i = 255;
        b = checked((byte)i);    /* 可以通过溢出检测 */
        Console.WriteLine(b);    // 255

        i++;
        b = checked((byte)i);    /* 不能通过溢出检测, 会报错 */
        Console.WriteLine(b);

        Console.ReadKey();
    }
}

 
 
 
 
 

 

 

  
  

关于swift 类型转换swift类型转换的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于12_c++类型转换、4. 类型转换、android 类型转换 工具函数、C# 语法练习(4): 类型转换的相关信息,请在本站寻找。

本文标签:

上一篇Swift 实现图片转字符画的功能(翻译)(swift 图片处理)

下一篇Swift 2 有哪些新特性(swift的特性)