λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ–₯️/C#

[C#] 데이터 νƒ€μž…κ³Ό 데이터 νƒ€μž… κ°„μ˜ λ³€ν™˜

by HanaV 2023. 8. 15.
728x90

μ •μˆ˜ 데이터 νƒ€μž…

데이터 ν˜•μ‹ μ„€λͺ… 크기(byte) 담을 수 μžˆλŠ” κ°’μ˜ λ²”μœ„ 
byte λΆ€ν˜Έ μ—†λŠ” μ •μˆ˜ 1(8bit) 0 ~ 255 (2^8)
sbyte μ •μˆ˜ 1(8bit) -128 ~ 127 (2^8)
short μ •μˆ˜ 2(16bit) -32,768 ~ 32,767 (2^16)
ushort λΆ€ν˜Έ μ—†λŠ” μ •μˆ˜ 2(16bit) 0 ~ 65,535 (2^16)
int μ •μˆ˜ 4(32bit) -2^31 ~ 2^31-1
uint λΆ€ν˜Έ μ—†λŠ” μ •μˆ˜ 4(32bit) 0 ~ 2^32-1
long μ •μˆ˜ 8(64bit) -2^63 ~ 2^63-1
ulog λΆ€ν˜Έ μ—†λŠ” μ •μˆ˜ 8(64bit) 0 ~ 2^64-1
char μœ λ‹ˆμ½”λ“œ 문자 2(16bit)  
  • λΆ€ν˜Έκ°€ μžˆλŠ” ν˜•μ‹κ³Ό μ—†λŠ” ν˜•μ‹ μ‚¬μ΄μ—μ„œ λ³€ν™˜μ„ 잘λͺ»ν•˜λ©΄ μ˜€λ²„ν”Œλ‘œμš°/μ–Έλ”ν”Œλ‘œμš°κ°€ λ°œμƒν•  수 μžˆλ‹€.

(μ˜ˆμ‹œμ½”λ“œ)

namespace SignedUnsignedConversion
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int a = -30;
            Console.WriteLine("a is " + a);

            uint b = (uint)a;
            Console.WriteLine("b is " + b);
        }
    }
}
Console>>
a is -30
b is 4294967266

 

μ‹€μˆ˜ 데이터 νƒ€μž…

데이터 ν˜•μ‹ μ„€λͺ… 크기(byte) 담을 수 μžˆλŠ” κ°’μ˜ λ²”μœ„
float 단일 정밀도 뢀동 μ†Œμˆ˜μ  ν˜•μ‹
(7μžλ¦¬κΉŒμ§€)
4(32bit) -3.402823e38 ~ 3.402823e38
double 볡수 정밀도 뢀동 μ†Œμˆ˜μ  ν˜•μ‹
(15~16μžλ¦¬κΉŒμ§€)
8(64bit) -1.79769313486232e308 ~ 1.79769313486232e308
decimal 29자리 데이터λ₯Ό ν‘œν˜„ν•  수 μžˆλŠ” μ†Œμˆ˜ ν˜•μ‹ 16(128bit)  
  • λΆ€λ™μ†Œμˆ˜μ μ˜ μ •λ°€λ„μ˜ 문제 λ•Œλ¬Έμ—, μ†Œμˆ˜μ  κ³„μ‚°μ—λŠ” λ³€ν™˜μ„ 쀄 λ•Œ 항상 μ‘°μ‹¬ν•΄μ•Όν•œλ‹€.

(μ˜ˆμ‹œ μ½”λ“œ)

using System;

namespace FloatConversion
{
    class MainApp
    {
        static void Main(string[] args)
        {
            float a = 12.3456f;
            Console.WriteLine("a is {0}", a);

            double b = (double)a;
            Console.WriteLine("b is {0}", b);

            Console.WriteLine("두 κ°œκ°€ κ°™λ‚˜μš”? {0}" + (a == b));
            Console.WriteLine("12.3456μ΄λž‘ aκ°€ κ°™λ‚˜μš”? " + (12.3456 == a));
            Console.WriteLine("12.3456μ΄λž‘ bκ°€ κ°™λ‚˜μš”? " + (12.3456 == b));

            float a1 = 0.1f;
            Console.WriteLine("a1 is {0}", a1);

            double b1 = (double)a1;
            Console.WriteLine("b1 is {0}", b1);

            Console.WriteLine("두 κ°œκ°€ κ°™λ‚˜μš”? " + (a1 == b1));
            Console.WriteLine("0.1μ΄λž‘ a1κ°€ κ°™λ‚˜μš”? " + (0.1 == a1));
            Console.WriteLine("0.1μ΄λž‘ b1κ°€ κ°™λ‚˜μš”? " + (0.1 == b1));
        }
    }
}
Consol >>
a is 12.3456
b is 12.345600128173828
두 κ°œκ°€ κ°™λ‚˜μš”? {0}True
12.3456μ΄λž‘ aκ°€ κ°™λ‚˜μš”? False
12.3456μ΄λž‘ bκ°€ κ°™λ‚˜μš”? False
a1 is 0.1
b1 is 0.10000000149011612
두 κ°œκ°€ κ°™λ‚˜μš”? True
0.1μ΄λž‘ a1κ°€ κ°™λ‚˜μš”? False
0.1μ΄λž‘ b1κ°€ κ°™λ‚˜μš”? False
  •  μ‹€μˆ˜μ™€ μ •μˆ˜ μ‚¬μ΄μ˜ λ³€ν™˜μ—μ„œλŠ” μ†Œμˆ˜μ  μ΄ν•˜λ₯Ό 버린닀.
  • 숫자λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•  λ•ŒλŠ” 숫자.ToString() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
  • λ¬Έμžμ—΄μ„ 숫자둜 λ³€ν™˜ν•  λ•ŒλŠ” 두 가지 방법이 μžˆλ‹€.

1. int.Parse(): null값이 λ“€μ–΄κ°€λ©΄ ArgumentNullException
2. Convert.ToInt32(): null값이 λ“€μ–΄κ°€λ©΄ 0

(μ˜ˆμ‹œ μ½”λ“œ)

string str = "123";
string str2 = null;

Console.WriteLine("using int.Parse: " + int.Parse(str));
Console.WriteLine("using Convert.ToInt32: " + Convert.ToInt32(str));

// null
try
{
    int result1 = int.Parse(str2);
    Console.WriteLine("using int.Parse: " + result1);
}
catch (Exception e)
{
    Console.WriteLine("Exception using int.Parse: " + e.Message);
}

try
{
    int result2 = Convert.ToInt32(str2);
    Console.WriteLine("using Convert.ToInt32: " + result2);
}
catch (Exception e)
{
    Console.WriteLine("Exception using Convert.ToInt32: " + e.Message);
}
Console >>
using int.Parse: 123
using Convert.ToInt32: 123
Exception using int.Parse: Value cannot be null. (Parameter 's')
using Convert.ToInt32: 0

 

  • μ—΄κ±° ν˜•μ‹: μ’…λ₯˜λŠ” κ°™μ§€λ§Œ λ‹€λ₯Έ 값을 가진 μƒμˆ˜λ“€μ„ μ„ μ–Έν•  λ•Œ μ‚¬μš©ν•˜λ©΄ νŽΈλ¦¬ν•˜λ‹€.
    class MainApp
    {

        enum Status { YES = 10, NO = 20, DELETE = 30}
        static void Main(string[] args)
        {

            Console.WriteLine(Status.YES);
            Console.WriteLine((int)Status.YES);
            Console.WriteLine(Status.NO);
            Console.WriteLine((int)Status.NO);
            Console.WriteLine(Status.DELETE);
            Console.WriteLine((int)Status.DELETE);
        }
   }
Console >> 
YES
10
NO
20
DELETE
30

 

  • nullable ν˜•μ‹μœΌλ‘œ λ§Œλ“œλ €λ©΄ ν˜•μ‹ 뒀에 ?λ₯Ό 뢙이면 λœλ‹€.
int nullTest = null; //int κ°’μ—λŠ” null이 λ“€μ–΄κ°ˆ 수 μ—†μŒ
int? nullTest1 = null; //nullable둜 λ§Œλ“€ 수 있음
Console.WriteLine(nullTest1.HasValue); //nullμ΄λ―€λ‘œ False 좜λ ₯

 

  • var은 μ•Œμ•„μ„œ ν•΄λ‹Ή νƒ€μž…μ„ μ„€μ •ν•΄μ€€λ‹€.
var a = 3; //aλŠ” intνƒ€μž…μœΌλ‘œ μžλ™ μ„€μ •
var b = "test"; //bλŠ” stringνƒ€μž…μœΌλ‘œ μžλ™ μ„€μ •
728x90

"); wcs_do();