Trong bài viết này, chúng ta sẽ tìm hiểu cách dễ dàng chuyển đổi một số thập phân thành số thập lục phân tương đương trong JavaScript. Và chúng ta sẽ xem xét một số tình huống trong thế giới thực mà chúng ta sẽ cần thực hiện điều này
Đăng ký bản tin Coding Beauty
Có được những hiểu biết hữu ích và nâng cao kiến thức phát triển web của bạn với các mẹo và hướng dẫn hàng tuần từ Coding Beauty. Hơn 1.400 nhà phát triển đăng ký
Phương pháp const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
7 const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
Để chuyển đổi một số thập phân thành hex trong JavaScript, hãy gọi phương thức
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8 trên số thập phân, chuyển // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
1 làm đối số // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
2, i. e. , // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
3. Phương thức const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8 sẽ trả về biểu diễn chuỗi của số ở dạng thập lục phânVí dụ
JavaScriptĐã sao chép.
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
Phương thức
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
7 const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8 trả về biểu diễn chuỗi của một số. Nếu một cơ sở được chỉ định với đối số đầu tiên, thì số được biểu thị trong cơ sở đó. Chúng tôi vượt qua // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
1 để sử dụng cơ số 16, là cơ số thập lục phânCơ số thập lục phân sử dụng 16 ký hiệu để biểu diễn các số
8 đến// Use parentheses const hex2 = [60].toString[16]; console.log[hex2]; // 3c // Use double dots const hex3 = 50..toString[16]; console.log[hex3]; // 32
9 để đại diện cho các giá trị// Use parentheses const hex2 = [60].toString[16]; console.log[hex2]; // 3c // Use double dots const hex3 = 50..toString[16]; console.log[hex3]; // 32
8 đến// Use parentheses const hex2 = [60].toString[16]; console.log[hex2]; // 3c // Use double dots const hex3 = 50..toString[16]; console.log[hex3]; // 32
9// Use parentheses const hex2 = [60].toString[16]; console.log[hex2]; // 3c // Use double dots const hex3 = 50..toString[16]; console.log[hex3]; // 32
2 đếnconsole.log[40.]; // 40 console.log[20.]; // 20
3 [console.log[40.]; // 40 console.log[20.]; // 20
4 đếnconsole.log[40.]; // 40 console.log[20.]; // 20
5] để biểu thị các giá trịconsole.log[40.]; // 40 console.log[20.]; // 20
6 đếnconsole.log[40.]; // 40 console.log[20.]; // 20
1. Các chữ cái không phân biệt chữ hoa chữ thường, vì vậy// Use parentheses const hex2 = [60].toString[16]; console.log[hex2]; // 3c // Use double dots const hex3 = 50..toString[16]; console.log[hex3]; // 32
8 chính xác có cùng giá trị vớiconsole.log[40.]; // 40 console.log[20.]; // 20
9console.log[40.]; // 40 console.log[20.]; // 20
Gọi const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8 trên số chữ
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
Nếu bạn gọi trực tiếp
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8 trên một số bằng chữ, hãy đảm bảo bạn đặt nó trong ngoặc đơn [// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
72] hoặc sử dụng hai dấu chấm [// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
73 trước const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
8JavaScriptĐã sao chép.
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
Nếu bạn chỉ sử dụng một dấu chấm mà không có dấu ngoặc đơn, thì trình phân tích cú pháp JavaScript sẽ coi dấu chấm đó là một phần của chữ số – dấu thập phân – thay vì toán tử truy cập thành viên
JavaScriptĐã sao chép.
console.log[40.]; // 40
console.log[20.]; // 20
Như vậy sẽ xảy ra lỗi, vì sẽ không có toán tử truy cập thành viên trước tên thành viên
JavaScriptĐã sao chép.
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
7Vì vậy, bạn bọc số trong dấu ngoặc đơn để mọi thứ bên ngoài chúng được xem là tách biệt với số
JavaScriptĐã sao chép.
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
2Hoặc bạn thêm dấu chấm thứ hai sẽ được coi là toán tử truy cập thành viên
JavaScriptĐã sao chép.
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
3Trường hợp sử dụng. Chuyển đổi RGB[A] sang Hex
Một cách sử dụng phổ biến để chuyển đổi các giá trị thập phân thành hex để chuyển đổi mã màu RGB thành mã màu hex tương đương của nó. Đây là cách chúng ta có thể làm điều đó
JavaScriptĐã sao chép.
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
4Chúng tôi tạo một hàm
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
75 có thể tái sử dụng để chuyển đổi mã RGB thành mã hex tương đươngChúng tôi sử dụng hàm
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
76 để đệm mã hex thành hai chữ số, e. g, // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
77Sau khi chuyển đổi các giá trị thập phân
// Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
78, // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
79 và // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
20 thành biểu diễn thập lục phân của chúng, chúng tôi nối chúng lại với nhau thành một chuỗi có tiền tố là ký tự // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
21 để tạo thành mã màu hexChúng tôi có thể sửa đổi chức năng để cũng chấp nhận các giá trị RGBA, trong đó
console.log[40.]; // 40
console.log[20.]; // 20
4 là giá trị phần trăm [từ 0 đến 1] được sử dụng để chỉ định độ mờ của màu. console.log[40.]; // 40
console.log[20.]; // 20
4 sẽ là hai ký tự đầu tiên của mã màu hex, có giá trị trong khoảng từ // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
24 [0 hoặc 0%] đến // Use parentheses
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
// Use double dots
const hex3 = 50..toString[16];
console.log[hex3]; // 32
25 [255 hoặc 100%]JavaScriptĐã sao chép.
const num = 60;
const hex = num.toString[16];
console.log[hex]; // 3c
// Use parentheses when calling toString[] directly
const hex2 = [60].toString[16];
console.log[hex2]; // 3c
6Mọi điều điên rồ mà JavaScript làm
Hướng dẫn hấp dẫn về những cảnh báo tinh tế và những phần ít được biết đến của JavaScript
Đăng ký và nhận ngay một bản sao miễn phí
Ayibatari Ibaba
Ayibatari Ibaba là nhà phát triển phần mềm có nhiều năm kinh nghiệm xây dựng trang web và ứng dụng. Anh ấy đã viết rất nhiều về nhiều chủ đề lập trình và đã tạo ra hàng chục ứng dụng và thư viện mã nguồn mở