cconvert

cconvert(T,x)

Convert x to a value of type T, typically by calling convert(T,x)

In cases where x cannot be safely converted to T, unlike convert, cconvert may return an object of a type different from T, which however is suitable for unsafe_convert to handle.

Neither convert nor cconvert should take a Julia object and turn it into a Ptr.

Examples

  1. Convert an integer to a float:

    julia> cconvert(Float64, 10)
    10.0

    This example converts the integer value 10 to a Float64.

  2. Convert a string to an integer:

    julia> cconvert(Int, "123")
    123

    It converts the string "123" to an integer value.

  3. Handle unsafe conversions:
    julia> cconvert(Float32, -1)
    -1.0f0

    In cases where the conversion from x to T is not safe, cconvert can return a value of a different type that can be handled by unsafe_convert.

Common mistake example:

julia> cconvert(Float64, "abc")
ERROR: MethodError: Cannot `cconvert` "abc" to Float64

In this example, the conversion from the string "abc" to Float64 is not possible, resulting in a MethodError. It's important to ensure that the conversion is valid and supported for the given types.

See Also

base, big, bytes2hex, cconvert, complex, convert, dec, hex, hex2bytes, hex2num, oct, oftype, parse, promote, signed, unsafe_convert, unsigned, widen,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: