2011-04-20 5 views
2

자바 스크립트에서 간단한 ceasar 암호를 만들었고 f #에서 다시 만들 필요가 있습니다. 자바 스크립트에서 charCodeAt() 메서드를 사용하여 문자열의 개별 문자를 유니 코드 값으로 변환했습니다. 누구든지 F #에서 이것을 수행하는 방법을 알고 있거나 charCodeAt와 유사한 명령이 있다면? 나는 벤을 수 시간 동안 찾고 시간이 없어.char에서 유니 코드 값으로 변환하는 방법 #

감사

답변

5

당신은 문자를 가지고 있다면, 단지 정수로 변환 : 당신이 문자열이있는 경우

> int 'a';; 
val it : int = 97 

는, 인덱서와 함께이 결합 :

> int ("xyza".[3]);; 
val it : int = 97 
7

으로 기본 다국어 평면 밖에서 코드 포인트를 처리하면 System.Char.ConvertToUtf32을 사용하려고합니다.

let input = "\uD800\uDC00\u0061\u0300\u00C6" 
System.Char.ConvertToUtf32(input, 0) //gives 65536, which is good 
int input.[0] //gives 55296, not what you want 

는 위의 예에서 System.Char.ConvertToUtf32(input, 1)을 시도하는 경우가 그렇지 않은 경우, 예를 들어, 당신은 예외를 얻을 것이다, System.Globalization.StringInfo에서 도움이 결합해야합니다, 효과적으로를 사용합니다. 예 :

open System.Globalization 
let si = StringInfo(input) 
let teArr = Array.init si.LengthInTextElements (fun i -> si.SubstringByTextElements(i,1)) 

System.Char.ConvertToUtf32(teArr.[0], 0) //65536 
System.Char.ConvertToUtf32(teArr.[1], 0) //97 
System.Char.ConvertToUtf32(teArr.[2], 0) //198 
관련 문제