Skip to content

Instantly share code, notes, and snippets.

View yuchung-chuang's full-sized avatar
🔬

Yu-Chung Chuang yuchung-chuang

🔬
View GitHub Profile
@yuchung-chuang
yuchung-chuang / binary2decimal.m
Created May 6, 2021 00:54
Convert a 32-bit binary number to decimal.
input = '1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0'; %輸入32-bit二進位整數
x = str2num(input); %將輸入轉成向量
n = length(x);
y = [0, 2 .^ (n-2:-1:0)]; %2的整數冪
if ~x(1) %如果是正數
result = dot(x,y); %各位元與2的整數冪內積即為十進位
else %如果是負數
x = ~x; %取補數
result = -dot(x,y)-1; %內積後取負減一
end
GraphManager.instance.getMe { completion:
(user: MSGraphUser?, error: Error?) in
DispatchQueue.main.async {
guard let currentUser = user, error == nil else {
// Failed to get user
print("Error getting user: \(String(describing: error))")
return
}
@yuchung-chuang
yuchung-chuang / getMe.swift
Created April 25, 2020 22:17
GraphManager method, to get user information
public func getMe(completion: @escaping(MSGraphUser?, Error?) -> Void) {
let meRequest = NSMutableURLRequest(url: URL(string: "\(MSGraphBaseURL)/me")!)
let meDataTask = MSURLSessionDataTask(request: meRequest, client: self.client, completion: {
(data: Data?, response: URLResponse?, graphError: Error?) in
guard let meData = data, graphError == nil else {
completion(nil, graphError)
return
}
do {
// Deserialize response as a user
@yuchung-chuang
yuchung-chuang / AppDelegate.Swift
Created April 25, 2020 22:16
For authentication
import MSAL
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
guard let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String else {
return false
}
return MSALPublicClientApplication.handleMSALResponse(url, sourceApplication: sourceApplication)
}
@yuchung-chuang
yuchung-chuang / Info.plist.xml
Last active April 25, 2020 22:14
for authentication
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>msauth.$(PRODUCT_BUNDLE_IDENTIFIER)</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
@yuchung-chuang
yuchung-chuang / IfSignedIn.swift
Created April 25, 2020 22:11
To see if a user is already signed in
AuthenticationManager.instance.getTokenSilently {
(token: String?, error: Error?) in
DispatchQueue.main.async {
guard let _ = token, error == nil else {
// If there is no token or if there's an error,
// no user is signed in, so stay here
return
}
// Since we got a token, a user is signed in
// Go to welcome page
AuthenticationManager.instance.getTokenInteractively(parentView: self) {
(token: String?, error: Error?) in
DispatchQueue.main.async {
guard let _ = token, error == nil else {
// Show the error and stay on the sign-in page
let alert = UIAlertController(title: "Error signing in",
message: error.debugDescription,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
@yuchung-chuang
yuchung-chuang / M_det.m
Created February 22, 2019 06:42
Inverse matrix
function B=M_det(A) %Handwrited n*n matrix determinant function
[n,n1]=size(A);
if n~=n1
fprintf('Error using det\nMatrix must be square.\n')
elseif n==1
B=A;
elseif n==2
B=det2(A);
else
B=det3(A);
function obj = varassign( obj, var )
if nargin < 2 || isempty(var)
return
end
nvarin = numel(var);
assert( ~mod( nvarin, 2 ), 'Field and value input arguments must come in pairs.' );
for i = 1:2:nvarin-1
mc = metaclass(obj);
[~, LocB] = ismember(lower(var{i}),lower({mc.PropertyList.Name}));
obj.(mc.PropertyList(LocB).Name) = var{i+1};