How does this work in C++ and any rough equiavalnce of this in JavaScript?

type of this pointer in c++
this-> c++
c++ this in constructor
pointer to object in c++ pdf
this pointer in c++ in hindi
advantage of this pointer in c++
pointer to object in c++ - geeksforgeeks
pointer to derived class in c++

I am currently working on an encryption/decryption program written in C++. There is a snippet in the code which I have not understood. I have been trying to convert entire source into JavaScript.

I have two questions:

  1. How does this code work? I am very new to C++ programming.
  2. Any rough equivalence of this in JavaScript?
uint32_t char_to_int(char c){
  if(c >='0' && c<='9')
    return c -'0'; 
  else 
    return c - 'a' + 10; 
}

How exactly does the ?: operator work in C?, In int d = (b == c++) ? (c+1) : (c-1); : The value of c++ is the current value of c , 11. Separately, c is incremented to 12. b == 11 is false, since b is  How often does that happen? Exactly 6 lines apart (excercise : write numbers 1..30 and underline the ones that satisfy this condition), starting at 6-th line (count = 5). To get desired behaviour from your code, you should change condition to count % 5 == 4 , what will give you newline every 5 lines, starting at 5-th line (count = 4).

In C++, char is an integral type like int. A character literal, like 'a' or '0', is of an integral type too, and not a string like in JavaScript.

In JavaScript, you would get such an integral value (character code) by using s.charCodeAt(0) to your one-character string s. For longer strings, it will return the character code of the first character.

The table of codes for the base character set for the predominantly using encodings can be found here.

Directly converting your C++ code to JavaScript can be pretty straightforward just by adding the missing .charCodeAt(0) to the variable and to the string literals.

Also bear in mind that '1' - '0' in C++ is not the same as '1' - '0' in JavaScript, although the results are the same. The former subtracts a character code of '0' from a character code of '1'. The latter converts '1' to 1, '0' to 0, and calculates the difference. That's why you are receiving NaN with c - 'a', where 'a' (and probably c) is not convertible to a number.

How does a “#” work in C programming?, This is the symbol called hashtag (#). It is basically used in C programming to include the preprocessor directives into the program and it is also  Researchers have generally suggested quantities between three and 50. The bigger the pool, the more likely a positive case with a low viral load will be too diluted to trigger detection of the virus.

uint32_t char_to_int(char c){
  if(c >='0' && c<='9')
      return c -'0'; 
  else 
      return c - 'a' + 10; 
  }

This function converts digits ('0' to '9'), to their numeric equivalent i.e. '0' is converted to 0, '1' is converted to 1, .... '9' is converted to 9.

The c - 'a' + 10, converts lower case letters to a numeric equivalent viz 'a' becomes 10, 'b' becomes 11, ..... 'z' becomes 35. However, this ONLY works if the implementation (i.e. compiler) uses a character set with a contiguous set of lower-case letters (like ASCII). There are real-world character sets that do not have a contiguous set of lower-case letters though.

The problem is that conversion is also done for all characters, other than digits, and the result is then probably meaningless. For example, it will not correctly handle uppercase letters (if one expects uppercase letters to be converted to the same as their lowercase equivalents).

The function operates on one character at a time. It attempts to do the same thing as the standard function strtoul() except, except that strtoul() (1) works on a string (not a single character), (2) works correctly for both upper and lower case letters, (3) will work correctly regardless of what character set the implementation supports, and (4) does error checking (e.g. returning zero if an invalid character is detected). strtoul() is not limited to hex conversion - it works up to base 35.

As to an equivalent in JavaScript - essentially the same code will probably work, but with flaws similar to those in the C/C++ version.

C Functions, A function is a block of code that performs a specific task. In this tutorial, you will be introduced to functions (both user-defined and standard library functions) in C​  So, how does mail-in voting work? Who qualifies for it? Residents have to meet one of four requirements in order to vote by mail. Be 65 years old or older. Have a disability.

How do malloc() and free() work in C/C++?, malloc() The function malloc() is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. The 2019-20 NHL season going on pause back on March 12 brought the uncertainty of what it would mean for the 2020 NHL Draft and the lottery. While the timing for the NHL Draft is still technically

C - Unions, A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members,​  Does it work? The steroid, dexamethasone, is the first treatment shown to reduce mortality in severely ill patients, according to scientists in Britain. The drug appears to reduce inflammation

How to Work with Character Variables in C Programming, The char is still a variable type in C. When you work with characters, you use the char variable type to store them. CHARACTER VARIABLE MADNESS #include  A Russian bank with ties to Putin, a Lititz marketing firm that does work for Trump and the international intrigue that's landed in Lancaster County court

Comments
  • The javascript equivalent would be roughly the same... What exactly don't you understand in this line of code?
  • What I have understood is this code converts the character into integer. If it's 0 then it 0 , if it's 1 then 1 .. However if it's an 'a' then it outputs 10 and so on. When trying to implement the same code in JS it says NaN.
  • Also this code looks a bit unsafe, because when c < ('a' - 10) then you'll get a integer underflow.
  • So you understood what the function does, but not why it is doing so ;) And you're not to blame, it is undocummented and poorly named. What if the function was instead hex_digit_to_integer(char c)?
  • Pretty much yes. How does that minus(-) operator work ? Can we do the same thing with JS? When i tried to do it always throws NaN.
  • What happens if c = 'A' :)?
  • @Neijwiert the code only manages the lower characters from 'a', so with 'A' the value will be 'A' - 'a' + 10 = -22
  • I got that but how do we convert it into JS?
  • I edited my answer, just use parseInt for the conversion
  • This answer is incomplete even in C and C++. It does not ONLY convert to hex. It is actually closer to the conversion done done by strtoul(), except that it acts on a single character, not a string. strtoul() can convert values for bases up to 36, not just hex. strtoul() also handles uppercase letters sensibly, which the OPs code does not - and also works on systems with character sets with a non-contiguous set of lower-case letters.