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

Viewed 187

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; 
}
3 Answers

1) How does this code work?

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

that just return the integer value 0 .. 15 from an hexadecimal digit '0' .. '9' or 'a' .. 'f'

  • if c is the character '3' the first test is true so it returns '3' - '0' = 3

  • if c is the character 'b' the test is false and the code returns 'b' - 'a' + 10 = 11

2) Any rough equivalence of this in JavaScript?

in javascript just use parseInt(hexString, 16);


If I complete the given code to be able to use it :

#include <iostream>

typedef unsigned uint32_t;
uint32_t char_to_int(char c) {
  if(c >='0' && c<='9')
    return c -'0';
  else
    return c - 'a' + 10;
 } 
int main()
{
  for (char c = '0'; c <= '9'; ++c)
    std::cout << c << " -> " << char_to_int(c) << std::endl;

  for (char c = 'a'; c <= 'f'; ++c)
    std::cout << c << " -> " << char_to_int(c) << std::endl;

  return 0;
}

The execution is :

0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
6 -> 6
7 -> 7
8 -> 8
9 -> 9
a -> 10
b -> 11
c -> 12
d -> 13
e -> 14
f -> 15
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.

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.

Related