# How to use Cshort in Julia

**URL:** <https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 21, 2024, 11:57am UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506 "2024-05-21T11:57:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [May 21, 2024, 11:57am UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/1 "2024-05-21T11:57:57Z")

</div>

I have a 16-bit hex string `a = "4E1A"`. How to convert `a` to signed short integer using Cshort. No example usage is found for Cshort.

julia\> reinterpret(Int16,[0x4E1A])  
1-element reinterpret(Int16, ::Vector{UInt16}):  
19994

julia\> 10 + 1\*16 + 14 \* 16^2 + 4 \* 16^3  
19994

How to convert a = “4E1A” to 0x4E1A

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [May 21, 2024, 12:04pm UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/2 "2024-05-21T12:04:11Z")

</div>

```julia
julia> parse(Int, "4E1A", base=16)
19994
julia> parse(Int16, "4E1A", base=16)
19994

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 21, 2024, 12:04pm UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/3 "2024-05-21T12:04:25Z")

</div>

Do you want to parse it?

```julia
julia> parse(UInt16, "4E1A"; base=16)
0x4e1a

```

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [May 21, 2024, 12:10pm UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/4 "2024-05-21T12:10:45Z")

</div>

julia\> parse(Int16, “F1CF”, base=16)  
ERROR: OverflowError: overflow parsing “F1CF”  
Stacktrace:  
[1] tryparse\_internal(#unused#::Type{Int16}, s::String, startpos::Int64, endpos::Int64, base\_::Int64, raise::Bool)  
@ Base .\parse.jl:163  
[2] parse(::Type{Int16}, s::String; base::Int64)  
@ Base .\parse.jl:241  
[3] top-level scope  
@ REPL[4]:1

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 21, 2024, 12:14pm UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/5 "2024-05-21T12:14:13Z")

</div>

> [@VinodV](#):
>
> julia\> parse(Int16, “F1CF”, base=16)  
> ERROR: OverflowError: overflow parsing “F1CF”

That’s because this is larger than the largest _positive_ `Int16`. If you want to “wrap it around” to a negative `Int16` value with those bytes via [2’s complement](https://en.wikipedia.org/wiki/Two%27s_complement), do:

```julia
julia> parse(UInt16, "F1CF"; base=16) % Int16
-3633

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 21, 2024, 1:21pm UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/6 "2024-05-21T13:21:22Z")

</div>

There’s nothing special or different about `Cshort`. The only reason you’d ever want to use it is for interop with a C program that uses the definition of `short`. Otherwise, it’s much better to simply use `Int16` directly as folks here are assuming because it seems you know that it’s two bytes (or four hex digits).

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [May 22, 2024, 10:23am UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/7 "2024-05-22T10:23:31Z")

</div>

Why “F1CF” is larger than largest _positive_ `Int16` (32767). Is it by convention. Can you please explain. There are total 2^16 different values possible with `Int16`. Of which many are causing overflow error. How can I find out which possible values are causing overflow error.

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [May 22, 2024, 10:31am UTC](https://discourse.julialang.org/t/how-to-use-cshort-in-julia/114506/8 "2024-05-22T10:31:36Z")

</div>

C short is a 16-bit _signed_ integer (as is `Int16`) and so it can store values in the range -2^15 to 2^15-1. That is, the largest positive value is `7FFF`.

A C unsigned short is equivalent to a `UInt16` and can store values from 0 to 2^16-1 (ie up to `FFFF`).
