Just thought id post here while i search google at the same time because well two brains are better than one :)

The Asc function converts the first letter in a string to ANSI code, and returns the result.
Syntax

Asc(string)

Thats the VB version of it, i need something similar in c++
ok back to searching google

Dani AI

Generated

OP asked for the VB Asc equivalent in C++. ’s cast-to-int example is a good starting point and showed the same idea with a C-style cast; rightly pointed out the ASCII vs “ANSI” confusion. The correct implementation depends on what is meant by “ANSI” and what the input encoding actually is (plain ASCII, a Windows code page, or UTF-8/Unicode).

A safe, minimal way to get the numeric value of the first byte of a single-byte (ANSI/ASCII) string is to avoid sign-extension by converting through unsigned char:

int asc_byte(char c)
{
    return static_cast<int>(static_cast<unsigned char>(c)); // yields 0..255
}

If “ANSI” means the legacy Windows code page (what classic VB used), convert the first Unicode code unit to that code page and inspect the resulting bytes. On Windows, WideCharToMultiByte can be used; the first output byte is what old code commonly treated as the character value (note: some characters map to multibyte sequences, so behavior can vary):

#include <windows.h>

int asc_vb_like(wchar_t wc, UINT codePage = CP_ACP)
{
    char buf[8] = {0};
    int len = WideCharToMultiByte(codePage, 0, &wc, 1, buf, sizeof(buf), NULL, NULL);
    if (len <= 0) return -1; // conversion failed
    return static_cast<unsigned char>(buf[0]);
}

For UTF-8 input the concept of “the code” should be the Unicode code point, not a single byte. A small, validated UTF-8 decoder for the first code point avoids misinterpreting multi-byte characters (example below returns the Unicode code point):

#include <cstdint>
#include <string>

uint32_t utf8_first_codepoint(const std::string& s)
{
    if (s.empty()) return 0;
    const unsigned char* b = reinterpret_cast<const unsigned char*>(s.data());
    size_t n = s.size();
    if (b[0] < 0x80) return b[0];
    if ((b[0] >> 5) == 0x6 && n >= 2 && (b[1] >> 6) == 0x2)
        return ((b[0] & 0x1F) << 6) | (b[1] & 0x3F);
    if ((b[0] >> 4) == 0xE && n >= 3 && (b[1] >> 6) == 0x2 && (b[2] >> 6) == 0x2)
        return ((b[0] & 0x0F) << 12) | ((b[1] & 0x3F) << 6) | (b[2] & 0x3F);
    if ((b[0] >> 3) == 0x1E && n >= 4 && (b[1] >> 6) == 0x2 && (b[2] >> 6) == 0x2 && (b[3] >> 6) == 0x2)
        return ((b[0] & 0x07) << 18) | ((b[1] & 0x3F) << 12) | ((b[2] & 0x3F) << 6) | (b[3] & 0x3F);
    return 0;
}

Practical notes: prefer Unicode-aware handling for modern code, use std::string/std::wstring and safe input routines, and be explicit about the intended encoding when interoperating with legacy APIs or VB code.

Recommended Answers

All 7 Replies

Like this?

#include <iostream>

int main()
{
   char text[] = "hello world";
   std::cout << "text = \"" << text << "\"\n";
   std::cout << "text[0] = '" << text[0] << "'\n";
   std::cout << "text[0] = " << static_cast<int>(text[0]) << '\n';
   int value = text[0];
   std::cout << "value = " << value << '\n';
   return 0;
}

/* my output
text = "hello world"
text[0] = 'h'
text[0] = 104
value = 104
*/

Perfect i love you :d

That would be ASCII, not ANSI ;)

you can mark the thread solved now.

Well i needed a function for this so i made

#include <iostream>

int Crypt(char szChar[]);

int main()
{
   char text[256];

   printf("Enter a string: ");
   scanf("%s", text);
   
   int final = Crypt(text);

   printf("value = %d \n\n", final);

   system("PAUSE");

   return 0;
}

// Credits to Dave Sinkula - Daniweb.com
int Crypt(char szChar[])
{
	int value = static_cast<int>(szChar[0]);
	return value;
}

incase anyone else needs it lol

std::cout << "text[0] = " << (int)text[0] << '\n';

haha, you use standard C code for the first part then a C++-style cast for the function. You should just make it C,

int Crypt(char *szChar)
{
return (int)szChar[0];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.