Get Website code by store id programmatically magento 2.

Magento 2, You can fetch website code by just passing store id value.

You required store id to get website code from the passed store id.

You can fetch website code using the below code snippet,

<?php declare(strict_types=1);

namespace Rbj\MyWebsite\Model;

use Magento\Store\Model\StoreManagerInterface;

class WebsiteCodeByStoreId
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
    }

    /**
     * Get Website code by store id
     *
     * @param int $storeId
     * @return string|null
     */
    public function getWebsiteCodeByStoreId(int $storeId): ?string
    {
        $websiteCode = null;
        try {
            $websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
            $websiteCode = $this->storeManager->getWebsite($websiteId)->getCode();
        } catch (NoSuchEntityException $entityException) {
            //Log the exception
            //$entityException->getMessage();
        }

        return $websiteCode;
    }
}

Call function,
echo $this->getWebsiteCodeByStoreId(1);

Output: base